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 {

    isans;
    isansName;
    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/edit/', this.item.id)
            .subscribe((res) => {
                this.temp = res.json().data.ques.datalist;
                this.isansName = res.json().data.isansName;
                if(res.json().isans == 1){
                    this.isans = true
                }else{
                    this.isans = false;
                }

                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': this.temp[i].isAns
                    };
                    this.ansArr.push(data);
                }
            })
    }


    //多选
    mutiSelect(i, index) {
        if (this.ansArr[i].answerdesc.indexOf(index) != -1) {
            this.ansArr[i].answerdesc = this.ansArr[i].answerdesc.replace(',' + index, '');
        } else {
            this.ansArr[i].answerdesc += "," + index;
        }
    }

    confirm(){
        this.appService.alert('确定提交问卷？',res=>{
            this.submit();
        });
    }

    submit() {

        if (!this.check()) {
            return false;
        }

        let arrData = [];
        if(this.isans){
            this.isans = 1;
        }else{
            this.isans = 2;
        }
        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.isans
            };
            arrData.push(data);
        }
        const qData = {
            'array': JSON.stringify(arrData)
        };
        this.appService.ObserverHttpPost('/wisdomgroup/modules/question/create/quesuser', qData)
            .subscribe((res) => {
                this.navCtrl.pop();
            })
    }

    check() {
        let count = 0;
        for (let i = 0; i < this.ansArr.length; i++) {
            if (this.ansArr[i].isAns == 1) {
                if (this.ansArr[i].answerdesc == "") {
                    this.appService.popToastView('有必答题未答', 'middle', 1500);
                    count++;
                    break;
                }
            }
        }

        if (count > 0) {
            return false;
        }

        return true;
    }

}
