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";

@Component({
    selector: 'page-do-verity',
    templateUrl: 'do-verity.html',
})
export class DoVerityPage {
    option = {
        type: '0',
        list: [
            {name: "全部", type: '0'},
            {name: "事业单位", type: '1'},
            {name: "机关团体", type: '2'},
            {name: "其他", type: '3'}
        ]
    };
    type = {
        option: 1,
        list: [],
        setList: [],  //设立 1
        changeList: [],  //变更 2
        cancelList: [],  //注销 3
        getList: [],  //补领 4
    };
    isShow = false;
    searchObj = {
        areaObj: null,
        approvalObj: null
    };
    yearmonth;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private globle:GlobalData,
                private datePipe:DatePipe,
                private loadCtrl:LoadingController,
                private dealSer: DealService, private modalCtrl: ModalController) {

    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad DealPage');
        this.changeOption();
    }

    //选择
    changeOption() {
        const loading = this.loadCtrl.create();
        loading.present();
        const data = {
            type: this.option.type,
            flow: "0",
            areaCode: this.globle.userObj.area,
            page:1,
            limit:1000,
        };
        this.dealSer.searchSchedule(data).subscribe(
            (res) => {
                loading.dismiss();
                if (res.list) {
                    this.type.setList = res.list.filter(e => e.flow == 1);
                    this.type.changeList = res.list.filter(e => e.flow == 2);
                    this.type.cancelList = res.list.filter(e => e.flow == 3);
                    this.type.getList = res.list.filter(e => e.flow == 4);
                    this.type.list = this.type.list.concat(this.type.setList);
                }
            }
        )
    }

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

    handle(item) {

    }

    //详情
    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;
            }
        });
        modal.present();
    }

}
