import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {AppService} from "../../../service/appHttpService";


@IonicPage()
@Component({
  selector: 'page-survey-write',
  templateUrl: 'survey-write.html',
})
export class SurveyWritePage {

  item;
  temp;
  ansArr = [];
  constructor(public navCtrl: NavController, public navParams: NavParams,
              public appService:AppService) {
  }

  ionViewDidLoad() {
      this.item = this.navParams.get('item');
    this.getDetail();
  }

  getDetail(){
    const id = '';
    this.appService.ObserverHttpGetAdd('/wisdomgroup/modules/question/findQuesByQuestionId/',this.item.id)
        .subscribe((res)=>{
          this.temp = res.json().datalist;
          for(let i = 0;i < this.temp.length;i++){
              const data = {
                  'questionId':this.temp[i].questionId,
                  'questionQueId':this.temp[i].id,
                  'answerdesc':'',
                  'quesType':this.temp[i].quesType,
                  'title':this.temp[i].title,
                  'quesDesc':JSON.parse(this.temp[i].quesDesc),
                  'isans':'1'
              };
                this.ansArr.push(data);
          }
        })
  }


  //多选
    mutiSelect(i,index){
      this.ansArr[i].answerdesc += ","+ index;
        console.log(this.ansArr);
    }

    submit(){
      let arrData = [];
      for(let i = 0;i < this.ansArr.length;i++){
          if(this.ansArr[i].quesType == 3){
              let length = this.ansArr[i].answerdesc.length;
              this.ansArr[i].answerdesc =  this.ansArr[i].answerdesc.substr(1,length-1);
          }
          const data = {
              'questionId':this.ansArr[i].questionId,
              'questionQueId':this.ansArr[i].questionQueId,
              'answerdesc':this.ansArr[i].answerdesc,
              'isans':this.ansArr[i].isans
          };
          arrData.push(data);
      }
      console.log(arrData);
      const qData = {
          'array':JSON.stringify(arrData)
      }
      this.appService.ObserverHttpPost('/wisdomgroup/modules/question/create/quesuser',qData)
            .subscribe((res)=>{
                this.navCtrl.pop();
            })
    }

}
