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 {HTMobMDViewPage} from '@pages/demo/htmob-mdview/htmob-mdview.page';
import {BGYPTreeExpViewPage} from '@pages/demo/bgyptree-exp-view/bgyptree-exp-view.page';
import {AppPortalView4Page} from '@pages/demo/app-portal-view4/app-portal-view4.page';
import {AppPortalView3Page} from '@pages/demo/app-portal-view3/app-portal-view3.page';


@Component({
  selector: 'ctrl-db-appmenu1-portlet',
  templateUrl: './gzsqmenu-portlet.html',
  styleUrls: ['./gzsqmenu-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/gzsqmenuappmenu';

	  /**
     * 菜单数据
	   *
     * @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 '_9': 
                    this.click_9();
                    return;
                case '_16': 
                    this.click_16();
                    return;
                case '_6': 
                    this.click_6();
                    return;
                case '_2': 
                    this.click_2();
                    return;
                default:
                    console.warn('未指定应用功能');
            }
        }
    }
/**
 * 车辆申请
 *
 * @param {*} [item={}]
 * @memberof GZSQMenu
 */
    public async click_9(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_appportalview4');

    }

/**
 * 合同审批
 *
 * @param {*} [item={}]
 * @memberof GZSQMenu
 */
    public async click_16(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_htmobmdview');

    }

/**
 * 办公用品
 *
 * @param {*} [item={}]
 * @memberof GZSQMenu
 */
    public async click_6(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_bgyptreeexpview');

    }

/**
 * 请假申请
 *
 * @param {*} [item={}]
 * @memberof GZSQMenu
 */
    public async click_2(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_appportalview3');

    }


        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();
        }
        
        


}