import {Component} from '@angular/core';
import {IonicPage, LoadingController, NavController, NavParams} from 'ionic-angular';
import {DealService} from "./deal.service";
import {DealDetailPage} from "./deal-detail/deal-detail";
import {GlobalData} from "../../core/GlobleData";

@IonicPage()
@Component({
    selector: 'page-deal',
    templateUrl: 'deal.html',
})
export class DealPage {
    option = {
        type: '1',
        list: [  //默认事业单位
            {name: "事业单位", type: '1'},
            {name: "机关群团", type: '2'},
            {name: "其他", type: '3'}
        ]
    };
    type = {
        option: 1,
        list: [],
        setList: [],  //设立 1
        changeList: [],  //变更 2
        cancelList: [],  //注销 3
        getList: [],  //补领 4
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private globle:GlobalData,
                private loadCtrl:LoadingController,
                private dealSer: DealService) {
    }

    ionViewDidLoad() {
        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":"10000"
        };
        this.dealSer.searchSchedule(data).subscribe(
            (res) => {
                loading.dismiss();
                if (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);
                    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});
    }
}
