import {Component} from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {DealService} from "../deal.service";
import {Detail5Page} from "../detail5/detail5";
import {Detail1Page} from "../detail1/detail1";
import {Detail2Page} from "../detail2/detail2";
import {Detail3Page} from "../detail3/detail3";
import {Detail4Page} from "../detail4/detail4";
import {HistoryPage} from "../history/history";
import {ApprovalsPage} from "../approvals/approvals";
import {FileListPage} from "../file-list/file-list";
import {SearchPersonComponent} from "../../../components/search-person/search-person";
import {GlobalData} from "../../../core/GlobleData";
import {CommonService} from "../../../core/common.service";
import {AppService} from "../../../app/app.service";
import {institutionalNature} from "../../../app/app.constants";
import {Detail6Page} from "../detail6/detail6";
import {Storage} from "@ionic/storage";

@Component({
    selector: 'page-deal-detail',
    templateUrl: 'deal-detail.html',
})
export class DealDetailPage {

    institutionalNatureList = institutionalNature;  //机构性质
    /**
     * type: String (1-事业单位，2-机关群团，3-其他)
     * flow : String (1-设立，2-变更 ,3-注销，4-补领，0-全部)
     */
    itemInfo;
    roles = [];
    obj = {
        opinion: "",
        flag: 0, //0-全部 1-督办 2-审核
    };

    submitObj = {
        flag: null,  //是否符合条件
        djflag: "0", //是否委托登记
        djtype: null, //委托登记类型。是否委托登记选是则出现这个选项
        opinion: null,  //回复
        smgflag: "0",  //是否发送短信
        jglb: null
    };

    detailObj = {
        renewalVO: null,   //补领
        rqeplyAccepted: null,  //默认受理回复
        changeMatters: null,   //变更登记
        changeRecordVO: null,  //变更记录
        listApprovalsVO: null,   //当前业务办理对应批文列表
        changeBusiness: null,  //机关群体业务
        acceptanceVO: null,   //受理信息
        changeBusinessVO: null,   //变更业务信息
        accessoryMaterials: null,   //附件材料
        certificateRenewalVO: null,   //事业单位证书补领申请书
        listAuditRecordVO: null,   //审核历史记录列表
        representativePersonVO: null,  //事业单位法定代表人（负责人）履历表
        revokeVO: null,  //撤销
        cancellationRegisterVO: null,  //事业单位法人注销登记（备案）申请书
        unitVO: null,  //单位信息
        companyVO: null,  //单位信息
        foundRegisterVO: null,  //事业单位法人设立登记（备案）申请书
        replacementVO: null,  //初领（无组织机构代码）
    };

    area;  //区属划分
    isShow = true;   //是否显示操作按钮

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private globle: GlobalData,
                private appSer: AppService,
                private commonSer: CommonService, private storage: Storage,
                private dealSer: DealService, private modalCtrl: ModalController) {
        this.itemInfo = this.navParams.get("item");
        this.storage.get('roles').then(value => this.roles = value || [])
        this.area = this.globle.area;
        if (this.itemInfo.status == '4' || this.itemInfo.status == '5') {
            this.isShow = false;
        }
        if (this.itemInfo.type == '2' && this.itemInfo.status == '1') {
            this.isShow = false;
        }
    }

    //查询详情
    ionViewDidLoad() {
        const data = {
            id: this.itemInfo.id,
            type: this.itemInfo.type,
        };
        this.dealSer.scheduleDetail(data).subscribe(
            (res) => {
                if (res.code == "0" && res.data) {
                    this.detailObj = res.data;
                    if (this.itemInfo.status == "1") {
                        this.submitObj.opinion = res.data.rqeplyAccepted;
                    }
                }
            }
        )
    }

    //打开审核历史
    openHistory() {
        let modalCtrl = this.modalCtrl.create(HistoryPage, {
            "listAuditRecordVO": this.detailObj.listAuditRecordVO
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    //详细数据
    openTypeDetail() {

    }

    //事业单位法人设立登记(备案)申请书
    openDetail1() {
        let modalCtrl = this.modalCtrl.create(Detail1Page, {
            "foundRegisterVO": this.detailObj.foundRegisterVO
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    //事业单位法定代表人(负责人)履历表
    openDetail2() {
        let modalCtrl = this.modalCtrl.create(Detail2Page, {
            "representativePersonVO": this.detailObj.representativePersonVO
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    //变更登记
    openDetail3() {
        let modalCtrl = this.modalCtrl.create(Detail3Page, {
            "changeMatters": this.detailObj.changeMatters,
            "changeRecordVO": this.detailObj.changeRecordVO,
            "changeBusiness": this.detailObj.changeBusiness,
            "changeBusinessVO": this.detailObj.changeBusinessVO,
            "type": this.itemInfo.type,
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    //事业单位法人注销登记(备案)申请书
    openDetail4() {
        let modalCtrl = this.modalCtrl.create(Detail4Page, {
            "cancellationRegisterVO": this.detailObj.cancellationRegisterVO,
            "revokeVO": this.detailObj.revokeVO,
            "type": this.itemInfo.type,
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    //事业单位法人证书补领申请书
    openDetail5() {
        let modalCtrl = this.modalCtrl.create(Detail5Page, {
            "certificateRenewalVO": this.detailObj.certificateRenewalVO
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    //机关群团 补领 设立
    openDetail6(title) {
        let vo;
        if (this.itemInfo.flow == '1') {
            vo = this.detailObj.replacementVO
        }
        if (this.itemInfo.flow == '3') {
            vo = this.detailObj.revokeVO
        }
        if (this.itemInfo.flow == '4') {
            vo = this.detailObj.revokeVO
        }
        let modalCtrl = this.modalCtrl.create(Detail6Page, {
            "revokeVO": vo,
            "title": title
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    //选择批文
    openApproval() {
        let modal = this.modalCtrl.create(ApprovalsPage, {
            "listApprovalsVO": this.detailObj.listApprovalsVO
        }, {
            enterAnimation: 'modal-scale-enter',
            leaveAnimation: 'modal-scale-leave'
        });
        modal.onDidDismiss(data => {
            if (data) {
            }
        });
        modal.present();
    }

    //查看附件
    viewFile() {
        let modalCtrl = this.modalCtrl.create(FileListPage, {
            "accessoryMaterials": this.detailObj.accessoryMaterials,
            "obj": this.itemInfo
        }, {
            enterAnimation: 'modal-from-right-enter',
            leaveAnimation: 'modal-from-right-leave'
        });
        modalCtrl.present();
    }

    sureSubmit() {
        if (!this.submitObj.opinion || !this.submitObj.flag) {
            this.commonSer.toast('请输入必填项');
            return
        }
        const data = {
            id: this.itemInfo.id,
            flag: this.submitObj.flag,
            opinion: this.submitObj.opinion,
            type: this.itemInfo.type,
            djflag: this.submitObj.djflag,
            djtype: this.submitObj.djtype,
            smgflag: this.submitObj.smgflag,
            jglb: this.submitObj.jglb
        }
        this.dealSer.scheduleAcceptdo(data).subscribe(
            (res) => {
                if (res.code == "0") {
                    this.commonSer.toast('办理成功');
                    this.appSer.setNotice('1');
                    this.appSer.setDeal(this.itemInfo.type)
                    this.navCtrl.pop();
                }
            }
        )
    }
}
