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

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

    tabParams = {
        test: 'test'
    };

    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: "5"
        },
        {
            root: NewsPage,
            tabTitle: '提醒',
            tabIconOn: 'custom-tztx-on',
            tabIconOff: 'custom-tztx-off',
            index: 2,
            badge: "8"
        },
        {
            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 events: Events, private nav: NavController, private tabSer: TabService) {
        this.platform.ready().then(() => {
            this.backButtonService.registerBackButtonAction(this.myTabs);
        });
        this.tabSer.tabChange.subscribe((value) => {
            this.tabParams = value;
            console.log(this.tabParams);
            this.myTabs.select(value.index)
        })
        this.listenEvents();
    }

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

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