import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {EditPage} from "../edit/edit";
import {AppService} from "../../../../service/appHttpService";
import {message} from "../../../../app/main";


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

    qType;  //题目类型
    temp;  //问卷信息
    titile;  //标题
    quesDescList = [   //选项
        {
            option_title: '',  //选项文字
            canFill: 2, // 1是 2否
            isNeed: 1,  // 1是 2否
            index: 0  //序号
        },
    ];
    set = {   //设置
        lastchange: '1',
        morechange: '1',
        isAns: '',
        more: '100'
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                public appService: AppService, public toastCtrl: ToastController) {
    }

    ionViewDidLoad() {
        this.qType = this.navParams.get('type');
        this.temp = this.navParams.get('temp');
    }

    addOption() {
        const option = {
            option_title: '',  //选项文字
            canFill: 2, // 1是 2否
            isNeed: 1,  // 1是 2否
            index: 0
        }; //序号
        this.quesDescList.push(option);
    }

    removeOption(i) {
        this.quesDescList.splice(i, 1);
    }

    submit() {
        // this.navCtrl.push('EditPage');
        const arr = [
            {
                questionId: this.temp.id,
                isAns: 1,
                quesType: this.qType,
                title: this.titile,
                limit: 1000,    //问答的字数限制
                quesDesc: this.quesDescList,
                minOps: 0,
                maxOps: 0,
                index: 0  //序号
            }
        ];
        const data = {
            array: JSON.stringify(arr)
        };
        console.log(this.temp);
        const toast = this.toastCtrl.create(message);
        this.appService.ObserverHttpPost('/wisdomgroup/modules/question/saveQuestion', data)
            .subscribe(
                (res) => {
                    toast.setMessage('添加成功');
                    toast.present();
                    setTimeout((res) => {
                        this.navCtrl.push('EditPage', {
                            temp: this.temp
                        });
                    }, 2000)
                }
            )
    }

}
