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 {QJMobMDViewPage} from '@pages/demo/qjmob-mdview/qjmob-mdview.page';


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

	  /**
     * 菜单数据
	   *
     * @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 '_17': 
                    this.click_17();
                    return;
                default:
                    console.warn('未指定应用功能');
            }
        }
    }
/**
 * 请假
 *
 * @param {*} [item={}]
 * @memberof WDSQMenu
 */
    public async click_17(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_qjmobmdview');

    }


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


}