import { Component, OnInit,Input,Output,EventEmitter,AfterViewInit,OnDestroy,ViewContainerRef, ViewChild} from '@angular/core';
import { Subject } from 'rxjs';
import { Http } from '@global/service/Http';
import { AppNotification } from '@global/service/Notification';
import { NavController, ModalController } from '@ionic/angular';
import { App } from '@global/service/App';
import { ComUtil } from '@global/util/ComUtil';
import { AppEnvironment } from 'src/environments/AppEnvironment';
import {DFMobMDViewPage} from '@pages/demo/dfmob-mdview/dfmob-mdview.page';
import {XMMobMDViewPage} from '@pages/demo/xmmob-mdview/xmmob-mdview.page';
import {WFWorkListMobMDViewPage} from '@pages/demo/wfwork-list-mob-mdview/wfwork-list-mob-mdview.page';
import {ZBMobCalendarViewPage} from '@pages/demo/zbmob-calendar-view/zbmob-calendar-view.page';
import {AppPortalView2Page} from '@pages/demo/app-portal-view2/app-portal-view2.page';


@Component({
  selector: 'ctrl-db-appmenu1-portlet',
  templateUrl: './grzxmenu-portlet.html',
  styleUrls: ['./grzxmenu-portlet.scss']
})
export class Db_appmenu1 implements OnInit,AfterViewInit,OnDestroy{

    /**
     * 名称
     *
     * @type {string}
     * @memberof Db_appmenu1
     */
    @Input() public name: string;

    /**
     * 视图通讯对象
     *
     * @type {Subject<ViewState>}
     * @memberof Db_appmenu1
     */
    @Input() public viewState: Subject<ViewState>;


    /**
     * 是否为内嵌视图
     *
     * @type {boolean}
     * @memberof Db_appmenu1
     */
    @Input() public isembeddedView: boolean;

    /**
     * 是否为模态模式
     *
     * @type {boolean}
     * @memberof Db_appmenu1
     */
    @Input() public isModalMode: boolean;

    /**
     * 当前部件激活数据
     *
     * @public
     * @type {void}
     * @memberof Db_appmenu1
     */
    public $activeData: Array<any> = [];

    constructor(private $http: Http, private $notification: AppNotification,private $navCtrl:NavController,private $app:App,private $modalCtrl: ModalController) {

    }


    /**
     * 返回
     *
     * @memberof Db_appmenu1
     */
    public backView() {
        if (this.isModalMode) {
            let res: any;
            if (this.$activeData && this.$activeData.length > 0) {
                res = { ret: 'OK', result: this.$activeData };
            } else {
                res = { ret: 'NO', result: [] };
            }
            this.$modalCtrl.dismiss(res, 'close');
        } else {
            this.$navCtrl.back();
        }
    }

    public getDatas(): any[] {
        return [];
    }

    public getData(): any {
        return {};
    }

    /**
     * 监听当前列表数据变化
     *
     * @type {Subject}
     * @memberof Db_appmenu1
     */  
    public curValueSubject = new Subject();

    /**
     * api地址
     *
     * @public
     * @type {string}
     * @memberof Db_appmenu1
     */
    public url: string = 'mob/ctrl/grzxmenuappmenu';

	  /**
     * 菜单数据
	   *
     * @public
     * @type {string}
     * @memberof Db_appmenu1
     */
    public items:Array<any>;

	  /**
     * 加载菜单
     *
     * @param {*} data
     * @memberof Db_appmenu1
     */
    public load(data:any) {
        this.$http.get(this.url + '/get').then((response: any) => {
            if (response) {
                this.items = response.items;
                this.curValueSubject.next(this.items);
            }
        }).catch((error: any) => {
            this.$notification.error(error.error.message);
        });
    }

	  /**
     * 菜单点击事件
     *
     * @param {*} data
     * @memberof Db_appmenu1
     */
    public menuClick($event){
      this.click($event);
    }

	  /**
     * 打开功能页
     *
     * @param {*} data
     * @memberof Db_appmenu1
     */
    public openAppFuncPickupView($event){
      this.$navCtrl.navigateRoot($event);
    }

    /**
     * 菜单点击
     *
     * @public
     * @param {*} item 菜单数据
     * @memberof Db_appmenu1
     */
    public click(item: any) {
        if (item) {
            switch (item.appfunctag) {
                case '_5': 
                    this.click_5();
                    return;
                case '_15': 
                    this.click_15();
                    return;
                case '_7': 
                    this.click_7();
                    return;
                case '_4': 
                    this.click_4();
                    return;
                case '_8': 
                    this.click_8();
                    return;
                default:
                    console.warn('未指定应用功能');
            }
        }
    }
/**
 * 工作申请
 *
 * @param {*} [item={}]
 * @memberof GRZXMenu
 */
    public async click_5(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_appportalview2');

    }

/**
 * 值班信息
 *
 * @param {*} [item={}]
 * @memberof GRZXMenu
 */
    public async click_15(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_zbmobcalendarview');

    }

/**
 * 待审批清单
 *
 * @param {*} [item={}]
 * @memberof GRZXMenu
 */
    public async click_7(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_wfworklistmobmdview');

    }

/**
 * 项目管理
 *
 * @param {*} [item={}]
 * @memberof GRZXMenu
 */
    public async click_4(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_xmmobmdview');

    }

/**
 * 电费信息
 *
 * @param {*} [item={}]
 * @memberof GRZXMenu
 */
    public async click_8(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_dfmobmdview');

    }


    ngOnInit() {
        if (this.viewState) {
            this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                this.load(data);
            });
        }
    }

    public ngAfterViewInit(): void {}

    public ngOnDestroy(): void {
        this.curValueSubject.unsubscribe();
    }


}