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

@IonicPage()
@Component({
    selector: 'page-deal',
    templateUrl: 'deal.html',
})
export class DealPage {
    option = {
        type: '1',
        list: [  //默认事业单位

        ]
    };
    type = {
        option: null,
        allList: [],
        list: [],
        setList: [],  //设立 1
        changeList: [],  //变更 2
        cancelList: [],  //注销 3
        getList: [],  //补领 4
    };
    mineInfo;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private globle: GlobalData,
                private appSer: AppService,
                private datePipe: DatePipe,
                private commonSer: CommonService,
                private homeSer: HomeService,
                private storage: Storage,
                private ModalCtrl: ModalController,
                private loadCtrl: LoadingController,
                private dealSer: DealService) {
        this.mineInfo = this.globle.userObj;
        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.option.list.push({name: "机关群团", type: "2"});
                this.option.type = '2';
            }
            if (value.indexOf('8') > -1) {
                this.option.list.push({name: "事业单位", type: "1"});
                this.option.type = '1';
            }
        })
    }

    ionViewDidEnter() {
        this.changeOption();
    }

    changeOption() {
        this.appSer.setDeal(this.option.type);
        const loading = this.loadCtrl.create();
        loading.present();
        const data = {
            type: this.option.type,
            flow: "0",
            areaCode: this.globle.userObj.area,
            "page": "1",
            "limit": "1000000"
        };
        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;
        this.type.list = [];
        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 loading = this.loadCtrl.create();
        loading.present();
        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) => {
                loading.dismiss();
                if (res.code == 0) {
                    this.commonSer.toast('督办成功');
                    this.appSer.setNotice("1")
                    this.changeOption();
                }
            }
        )
    }

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