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


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

    id;

    isans = null; //匿名
    isopen; //公开
    desrc = {
        title: '',   //问卷标题
        explai: '',   //问卷说明
        usergroup: null,  //用户组id
        isopen: 1,  //是否公开
        isans: 1,   //是否匿名
        state: 1   //状态
    };
    groupName = '全体用户';

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

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

    //新建
    submitOrder() {
        const toast = this.toastCtrl.create(message);
        if (!this.desrc.title) {
            toast.setMessage('请填写标题');
            toast.present();
            return false
        }
        if (this.desrc.usergroup == null) {
            this.desrc.usergroup = 'all';
        }
        if (this.desrc.isopen) {
            this.desrc.isopen = 1;
        } else {
            this.desrc.isopen = 0;
        }
        this.appService.ObserverHttpPost('/wisdomgroup/modules/question/create', this.desrc)
            .subscribe(
                (res) => {
                    if (res) {
                        toast.setMessage('创建成功');
                        toast.present();
                        this.navCtrl.push('EditPage', {temp: res.json()});
                    }
                }
            )
    }

    //编辑
    submitEdit() {
        //是否匿名
        if (this.isans == true || this.isans == 1) {
            this.desrc.isans = 1;
        } else {
            this.desrc.isans = 2;
        }
        //是否公开问卷结果
        if (this.isopen == true || this.isopen == 1) {
            this.desrc.isopen = 1;
        } else {
            this.desrc.isopen = 0;
        }
        const data = {
            id: this.id,
            title: this.desrc.title,   //问卷标题
            explai: this.desrc.explai,   //问卷说明
            usergroup: this.desrc.usergroup,  //用户组id
            isopen: this.desrc.isopen,  //是否公开
            isans: this.desrc.isans,   //是否匿名
            state: this.desrc.state   //状态
        };
        console.log(data);
        this.appService.ObserverHttpPost('/wisdomgroup/modules/question/updateOnApp', data)
            .subscribe((res) => {
                if (res) {
                    this.navCtrl.pop();
                }
            })
    }

    selectPerson() {
        let modal = this.modalCtrl.create(PersonPage);
        modal.onDidDismiss(data => {
            if (data) {
                this.desrc.usergroup = data.id;
                this.groupName = data.groupName;
            } else {
                this.desrc.usergroup = 'all';
                this.groupName = '全体用户';
            }

        });
        modal.present();
    }

    getDetail() {
        this.appService.ObserverHttpPostAdd('/wisdomgroup/modules/question/edit/', this.id).subscribe((res) => {
            this.desrc = res.json().data;
            if (this.desrc.isans == 1) {
                this.isans = true;
            } else {
                this.isans = false;
            }
            if (this.desrc.isopen == 1) {
                this.isopen = true;
            } else {
                this.isopen = false;
            }
        });
    }

}
