import {Component, ViewChild} from '@angular/core';

import {HomePage} from '../home/home';
import {Events, NavController, NavParams, Platform, Tabs} from "ionic-angular";
import {MinePage} from "../mine/mine";
import {BackButtonService} from "../../core/backButton.service";
import {LoginPage} from "../login/login";
import {TabService} from "../../core/tab.service";
import {DealPage} from "../deal/deal";
import {NewsPage} from "../news/news";
import {GlobalData} from "../../core/GlobleData";
import {DealService} from "../deal/deal.service";
import {AppService} from "../../app/app.service";
import {NewsService} from "../news/news.service";
import {Storage} from "@ionic/storage";

@Component({
    templateUrl: 'tabs.html'
})
export class TabsPage {
    @ViewChild('myTabs') myTabs: Tabs;

    tabParams = {
        test: 'test'
    };
    roles = new Array()

    tabRoots = [
        {
            root: HomePage,
            tabTitle: '首页',
            tabIconOn: 'custom-home-on',
            tabIconOff: 'custom-home-off',
            index: 0,
            badge: null
        },
        {
            root: DealPage,
            tabTitle: '待办',
            tabIconOn: 'custom-db-on',
            tabIconOff: 'custom-db-off',
            index: 1,
            badge: ""
        },
        {
            root: NewsPage,
            tabTitle: '提醒',
            tabIconOn: 'custom-tztx-on',
            tabIconOff: 'custom-tztx-off',
            index: 2,
            badge: ""
        },
        {
            root: MinePage,
            tabTitle: '我的',
            tabIconOn: 'custom-mine-on',
            tabIconOff: 'custom-mine-off',
            index: 3,
            badge: null
        },
    ];

    tabsIndex;

    constructor(private platform: Platform, private backButtonService: BackButtonService, private params: NavParams,
                private globle: GlobalData,
                private dealSer: DealService,
                private appSer: AppService,
                private newSer: NewsService, private storage: Storage,
                private events: Events, private nav: NavController, private tabSer: TabService) {
        this.storage.get('roles').then(value => this.roles = value || [])
        this.searchDeal("1");
        this.searchNotice();
        this.platform.ready().then(() => {
            this.backButtonService.registerBackButtonAction(this.myTabs);
        });
        this.tabSer.tabChange.subscribe((value) => {
            this.tabParams = value;
            this.myTabs.select(value.index)
        });
        this.appSer.dealInfo.subscribe(
            (value) => {
                if (value) {
                    this.searchDeal(value);
                }
            }
        );
        this.appSer.noticeInfo.subscribe(
            (value) => {
                if (value) {
                    this.searchNotice();
                }
            }
        );
        this.listenEvents();
    }

    //查询待办
    searchDeal(type) {
        const data = {
            "type": type,
            "flow": "0",
            "areaCode": this.globle.userObj.area,
            "page": "1",
            "limit": "10"
        };
        this.dealSer.searchSchedule(data).subscribe(
            (res) => {
                if (res.page) if (res.page.list) this.tabRoots[1].badge = this.roles.indexOf('7') !== -1 ? res.page.totalCount : "";
            }
        )
    }

    //查询通知提醒
    searchNotice() {
        const data = {
            page: 1,
            limit: 1000
        };
        this.newSer.noticeSearch(data).subscribe(
            (res) => {
                if (res.page.list) if (res.page.list.notice && res.page.list.notice.length > 0) {
                    this.tabRoots[2].badge = res.page.list.notice.length;
                }
            }
        )
    }

    onChange(e) {
        this.tabsIndex = e;
        this.myTabs.select(e);
    }

    // this.navCtrl.push(NoDevPage, {title: title});
    listenEvents() {
        this.events.subscribe('toLogin', () => {
            this.nav.setRoot(LoginPage);
        });
    }
}
