import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ModalController, ToastController, AlertController} from 'ionic-angular';
import {SelectTypePage} from "../select-type/select-type";
import {AppService} from "../../../../service/appHttpService";
import {message} from "../../../../app/main";

@IonicPage()
@Component({
  selector: 'page-edit',
  templateUrl: 'edit.html',
})
export class EditPage {

  editIndex;
  temp = {
      title:'',
      id:'',
  };
  contentList = [];
  constructor(public navCtrl: NavController, public navParams: NavParams,
              public modalCtrl: ModalController,public appService:AppService,
              public toastCtrl: ToastController,public alertCtrl: AlertController) {
  }
    qList = [
        {name:'测试'},
        {name:'测试'},
        {name:'测试'},
        {name:'测试'}
    ];

    ionViewDidEnter() {
      this.temp = this.navParams.get('temp');
        this.getDetail();
  }

  getDetail(){
      this.appService.ObserverHttpGet('/wisdomgroup/modules/question/findQuesByQuestionId',{id:this.temp.id})
          .subscribe(
              (res)=>{
                 this.contentList = res.json().datalist;
                  for( let i = 0;i < this.contentList.length;i++ ){
                        this.contentList[i].quesDesc = JSON.parse(this.contentList[i].quesDesc);
                  }
              }
          )
  }

  //添加题目
    add(){
     this.navCtrl.push('SelectTypePage',{
         temp:this.temp
     })
    }

    //操作
    itemEdit(index){
      this.editIndex = index;
    }

    //上移
    itemUp(item){

    }

    //下移
    itemDown(item){

    }

    //删除
    itemDelete(index){
        const confirm = this.alertCtrl.create({
            title: '确认删除该题目?',
            buttons: [
                {
                    text: '取消',
                    handler: () => {
                        console.log('Disagree clicked');
                    }
                },
                {
                    text: '确认',
                    handler: () => {
                        this.contentList.splice(index,1);
                        this.editIndex = null;
                    }
                }
            ]
        });
        confirm.present();
    }

    //查看
    look(){
        this.navCtrl.push('DetailPage',{
            temp:this.temp
        });
    }

    //设置
    set(){
      this.navCtrl.push('SetPage',{
          temp:this.temp
      });
    }

    //保存
    save(){
        let newArr = [];
        for( let i = 0;i<this.contentList.length;i++ ){
            const arr = {
                questionId: this.temp.id,
                isAns: this.contentList[i].isAns,
                quesType: this.contentList[i].quesType,
                title: this.contentList[i].title,
                limit: this.contentList[i].limit,    //问答的字数限制
                quesDesc:  this.contentList[i].quesDesc,
                minOps: this.contentList[i].minOps,
                maxOps: this.contentList[i].maxOps,
                index: this.contentList[i].index  //序号
            };
            newArr.push(arr);
        }
        console.log(newArr);
        const data = {
            array: JSON.stringify(newArr),
            id:this.temp.id
        };
        console.log(data);
        const toast = this.toastCtrl.create(message);
        this.appService.ObserverHttpPost('/wisdomgroup/modules/question/saveQuestion', data)
            .subscribe(
                (res) => {
                    toast.setMessage('编辑成功');
                    toast.present();
                    setTimeout((res) => {
                        this.getDetail();
                    }, 2000)
                }
            )
    }

}
