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 {QJUsr3MobEditViewPage} from '@pages/demo/qjusr3-mob-edit-view/qjusr3-mob-edit-view.page';
import {QJUsr2MobEditViewPage} from '@pages/demo/qjusr2-mob-edit-view/qjusr2-mob-edit-view.page';


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

	  /**
     * 菜单数据
	   *
     * @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 '_10': 
                    this.click_10();
                    return;
                case '_12': 
                    this.click_12();
                    return;
                default:
                    console.warn('未指定应用功能');
            }
        }
    }
/**
 * 事假单填写
 *
 * @param {*} [item={}]
 * @memberof QJSQMenu
 */
    public async click_10(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_qjusr3mobeditview');

    }

/**
 * 病假单填写
 *
 * @param {*} [item={}]
 * @memberof QJSQMenu
 */
    public async click_12(item?:any) {
    // 打开应用视图
    this.$navCtrl.navigateRoot('demo_qjusr2mobeditview');

    }


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


}