import {Component} from '@angular/core';
import {IonicPage, LoadingController, NavController, NavParams} from 'ionic-angular';
import {HomeService} from "../../home.service";
import {InfoDetailPage} from "../info-detail/info-detail";


@Component({
    selector: 'page-info-list',
    templateUrl: 'info-list.html',
})
export class InfoListPage {

    option = {
        type: '1',
        viewType: '1',  //增加用户体验的
        list: [  //默认事业单位
            {name: "事业单位", type: '1'},
            {name: "机关群团", type: '2'},
        ]
    };

    info = {
        list: [],
        page: 1,
        limit: 20,
        search: '',
        totalCount: null
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private loadCtrl: LoadingController, private homeSer: HomeService) {
    }

    ionViewDidLoad() {
        // this.getList();
    }

    changeType() {
        this.info.list = [];
        this.info.search = '';
    }

    getList(event?: any) {
        this.option.type === '1' ? this.getUnit(event) : this.getCreditor(event);
    }

    //机关群团
    getCreditor(event?: any) {
        const loadCtrl = this.loadCtrl.create();
        loadCtrl.present();
        this.info.page = 1;
        const data = {
            search: this.info.search,
            page: this.info.page,
            limit: this.info.limit,
            type: '2',
            "areacode": "",
            "openid": ""
        };
        this.homeSer.creditorgan(data).subscribe(
            (res) => {
                loadCtrl.dismiss();
                if (event) event.complete();
                this.option.viewType = '2';
                this.info.list = res.page.list.object;
                this.info.totalCount = res.page.totalCount;
            }
        )
    }

    //事业单位
    getUnit(event?: any) {
        const loadCtrl = this.loadCtrl.create();
        loadCtrl.present();
        this.info.page = 1;
        const data = {
            search: this.info.search,
            page: this.info.page,
            limit: this.info.limit,
            type: '1',
            "areacode": "",
            "openid": ""
        };
        this.homeSer.unitfind(data).subscribe(
            (res) => {
                loadCtrl.dismiss();
                if (event) event.complete();
                this.option.viewType = '1';
                this.info.list = res.page.list.object;
                this.info.totalCount = res.page.totalCount;
            }
        )
    }

    //加载更多
    doInfinite(event) {
        this.option.type === '1' ? this.getMoreUnit(event) : this.getMoreCreditor(event);
    }

    getMoreUnit(event) {
        if (this.info.totalCount == this.info.list.length) {
            event.enable(false);
            return false;
        }
        this.info.page++;
        const data = {
            search: this.info.search,
            page: this.info.page,
            limit: this.info.limit,
            type: '1',
            "areacode": "",
            "openid": ""
        };
        this.homeSer.unitfind(data).subscribe(
            (res) => {
                if (event) event.complete();
                this.info.list = this.info.list.concat(res.page.list.object);
                this.info.totalCount = res.page.totalCount;
            }
        )
    }

    getMoreCreditor(event) {
        if (this.info.totalCount == this.info.list.length) {
            event.enable(false);
            return false;
        }
        this.info.page++;
        const data = {
            search: this.info.search,
            page: this.info.page,
            limit: this.info.limit,
            type: '2',
            "areacode": "",
            "openid": ""
        };
        this.homeSer.creditorgan(data).subscribe(
            (res) => {
                if (event) event.complete();
                this.info.list = this.info.list.concat(res.page.list.object);
                this.info.totalCount = res.page.totalCount;
            }
        )
    }

    //查询详情
    goToDetail(item) {
        this.navCtrl.push(InfoDetailPage, {obj: item, type: this.option.type});
    }

}
