Skip to content
create.ts 6.2 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {EditPage} from "../edit/edit";
import {AppService} from "../../../../../service/http.service";
import {message} from "../../../../../app/main";

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

    //编辑
    editTemp;
    editIndex;

    qType;  //题目类型
    temp;  //问卷信息
    title;  //标题
    index = 0;  //序号
    isAns = 2;   //是否必答
    quesDescList = [];
    lastchange = [];
    morechange = [];
    set = {   //设置
        lastchange: '1',
        morechange: '1',
        isAns: '',
        more: '100'
    };

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

    ionViewDidLoad() {
        this.temp = this.navParams.get('temp');    //问卷信息
        if (this.navParams.get('edit')) {      //编辑
            this.editTemp = this.navParams.get('edit');
            this.editIndex = this.navParams.get('index');
            this.qType = this.editTemp[this.editIndex].quesType;
            this.title = this.editTemp[this.editIndex].title;
            this.isAns = this.editTemp[this.editIndex].isAns;
            this.quesDescList = this.editTemp[this.editIndex].quesDesc;
        } else {     //新增
            this.qType = this.navParams.get('type');   //题目类型
        }

    }

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

    removeOption(i) {
        if(this.quesDescList.length > 1){
            this.quesDescList.splice(i, 1);
            this.lastchange.length --;
            this.morechange.length --;
        }else{
            this.appService.popToastView('至少一个选项','middle',1500);
        }

    }

    //是否必答
    isAnsChange(type){
        this.isAns = type;
    }

    //提交编辑问卷
    submitEdit() {
        if (!this.check()) {
            return false;
        }
        let newArr = [];
        this.editTemp[this.editIndex].title = this.title;
        this.editTemp[this.editIndex].isAns = this.isAns;
        for (let i = 0; i < this.editTemp.length; i++) {
            const arr = {
                questionId: this.temp.id,
                isAns: this.editTemp[i].isAns,
                quesType: this.editTemp[i].quesType,
                title: this.editTemp[i].title,
                limit: this.editTemp[i].limit,    //问答的字数限制
                quesDesc: this.editTemp[i].quesDesc,
                minOps: this.editTemp[i].minOps,
                maxOps: this.editTemp[i].maxOps,
                index: this.editTemp[i].index  //序号
            };
            newArr.push(arr);
        }
        const data = {
            array: JSON.stringify(newArr),
            id: this.temp.id
        };
        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
                        });
                    }, 1000)
                }
            )
    }

    //提交问卷
    submit() {

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

        if (this.qType == 1) {
            const option1 = {
                option_title: '',  //选项文字
                canFill: 2, // 1是 2否
                isNeed: 1,  // 1是 2否
                index: 0
            }; //序号
            const option2 = {
                option_title: '',  //选项文字
                canFill: 2, // 1是 2否
                isNeed: 1,  // 1是 2否
                index: 1
            }; //序号
            this.quesDescList.push(option1);
            this.quesDescList.push(option2);
        }
        const arr = [
            {
                questionId: this.temp.id,
                isAns: this.isAns,
                quesType: this.qType,
                title: this.title,
                limit: 1000,    //问答的字数限制
                quesDesc: this.quesDescList,
                minOps: this.set.lastchange,
                maxOps: this.set.morechange,
                index: this.temp.quesNum  //序号
            }
        ];
        const data = {
            array: JSON.stringify(arr)
        };
        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)
                }
            )
    }

    //校验
    check() {
        if (!this.title) {
            this.appService.popToastView('请输入标题', 'middle', 1000);
            return false;
        }
        if (this.qType == 2 || this.qType == 3) {
            if (this.quesDescList.length == 0) {
                this.appService.popToastView('请输入选项', 'middle', 1000);
                return false;
            } else {
                let index = 0;
                this.quesDescList.forEach((res) => {
                    if (res.option_title.length == '') {
                        index++;
                    }
                });
                if (index > 0) {
                    this.appService.popToastView('请输入选项', 'middle', 1000);
                    return false;
                }

            }
        }
        return true;
    }

}