import {Component} from '@angular/core';
import {IonicPage, LoadingController, ModalController, NavController, NavParams} from 'ionic-angular';
import {DealService} from "../../deal/deal.service";
import {DealDetailPage} from "../../deal/deal-detail/deal-detail";
import {FilterComponent} from "../../../components/filter/filter";
import {GlobalData} from "../../../core/GlobleData";
import {DatePipe} from "@angular/common";
import {SearchPersonComponent} from "../../../components/search-person/search-person";
import {HomeService} from "../home.service";
import {CommonService} from "../../../core/common.service";
import {Storage} from "@ionic/storage";

@Component({
    selector: 'page-do-verity',
    templateUrl: 'do-verity.html',
})
export class DoVerityPage {
    option = {
        type: null,
    };
    type = {
        option: null,
        list: [],
        allList: [],
        setList: [],  //设立 1
        changeList: [],  //变更 2
        cancelList: [],  //注销 3
        getList: [],  //补领 4
    };
    isShow = false;
    searchObj = {
        areaObj: null,
        approvalObj: {},
        areaCode: null
    };
    yearmonth;
    mineInfo;
    globleArea;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private globle: GlobalData,
                private datePipe: DatePipe,
                private homeSer: HomeService,
                private commonSer: CommonService,
                private loadCtrl: LoadingController,
                private storage: Storage,
                private dealSer: DealService, private modalCtrl: ModalController) {
        this.mineInfo = this.globle.userObj;
        this.searchObj.areaCode = this.globle.userObj.area;
        this.searchObj.areaObj = this.globle.area;
        this.globleArea = this.globle.area;
        this.storage.get('roles').then((value) => {
            if (value.indexOf('8') == -1 && value.indexOf('9') == -1) {
                this.commonSer.toast('没有事业单位和机关群体权限，请联系管理员!');
                return
            }
            if (value.indexOf('9') > -1) {
                this.searchObj.approvalObj = {name: "机关群团", type: "2"};
                this.option.type = '2';
            }
            if (value.indexOf('8') > -1) {
                this.searchObj.approvalObj = {name: "事业单位", type: "1"};
                this.option.type = '1';
            }
        })
    }


    ionViewDidEnter() {
        this.changeOption();
    }

    changeOption() {
        const loading = this.loadCtrl.create();
        loading.present();
        const data = {
            type: this.option.type,
            flow: "0",
            areaCode: this.searchObj.areaCode,
            "page": "1",
            "limit": "10000"
        };
        this.dealSer.searchSchedule(data).subscribe(
            (res) => {
                loading.dismiss();
                if (res.page.list) {
                    this.type.allList = res.page.list;
                    this.type.setList = res.page.list.filter(e => e.flow == 1);
                    this.type.changeList = res.page.list.filter(e => e.flow == 2);
                    this.type.cancelList = res.page.list.filter(e => e.flow == 3);
                    this.type.getList = res.page.list.filter(e => e.flow == 4);
                    if (this.type.option == 0) this.type.list = this.type.allList;
                    if (this.type.option == 1) this.type.list = this.type.setList;
                    if (this.type.option == 2) this.type.list = this.type.changeList;
                    if (this.type.option == 3) this.type.list = this.type.cancelList;
                    if (this.type.option == 4) this.type.list = this.type.getList;
                }
            }
        )
    }

    //change
    changeType(type) {
        this.type.option = type;
        if (type == 0) this.type.list = this.type.allList;
        if (type == 1) this.type.list = this.type.setList;
        if (type == 2) this.type.list = this.type.changeList;
        if (type == 3) this.type.list = this.type.cancelList;
        if (type == 4) this.type.list = this.type.getList;
    }

    handleClick(item, e) {
        e.stopPropagation();
        let modal = this.modalCtrl.create(SearchPersonComponent, {
            enterAnimation: 'modal-scale-enter',
            leaveAnimation: 'modal-scale-leave'
        });
        modal.onDidDismiss(res => {
            if (res) {
                this.handleSend(res.data, item);
            }
        });
        modal.present();
    }

    /**
     *
     * @param res 人员信息
     * @param item  督办信息
     */
    handleSend(res, item) {
        const nowDate = this.datePipe.transform(Date.now(), "yyyy年MM月dd日");
        const statusName = item.status == 1 ? "受理" : item.status == 2 ? "审核" : item.status == 3 ? "审批" : item.status == 4 ? "办证" : "发证"
        const data = {
            "dbuserid": res.id,
            "scheduleid": item.id,
            "node": item.status,
            "content": `${this.mineInfo.name}于${nowDate}向${res.name}督办${item.title}的${statusName}事项，请与3个工作日内办结并回复，谢谢`,
            "isUnit": this.option.type,
            "flow": item.flow
        };
        this.homeSer.dbSuper(data).subscribe(
            (res) => {
                if (res.code == 0) {
                    this.commonSer.toast('督办成功');
                    this.changeOption();
                }
            }
        )
    }

    goToDetail(item) {
        this.navCtrl.push(DealDetailPage, {item: item});
    }

    //打开筛选栏
    openFilter() {
        let modal = this.modalCtrl.create(FilterComponent, {
                areaObj: this.searchObj.areaObj,
                approvalObj: this.searchObj.approvalObj,
            },
            {
                enterAnimation: 'modal-from-right-enter',
                leaveAnimation: 'modal-from-right-leave'
            });
        modal.onDidDismiss(res => {
            if (res) {
                this.searchObj.areaObj = res.area;
                this.searchObj.approvalObj = res.approval;
                if (res.area) this.searchObj.areaCode = this.searchObj.areaObj.code;
                if (res.approval) this.option.type = res.approval.type;
                this.changeOption();
            }
        });
        modal.present();
    }

}
