import { Component, OnInit, Input,Output,EventEmitter,ViewContainerRef, ViewChild,OnDestroy} 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';
@Component({
  selector: 'ctrl-titlebar-titlebar',
  templateUrl: './titlebar-titlebar.html',
  styleUrls: ['./titlebar-titlebar.scss']
})
export class Titlebar implements OnInit,OnDestroy{

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

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


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

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

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

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

    }


    /**
     * 返回
     *
     * @memberof Titlebar
     */
    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();
        }
    }

    /**
     * 头部菜单数据
     *
     * @private
     * @type {string}
     * @memberof Test3
     */
    private $items: Array<any> = [];

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

    ngOnDestroy(): void {
            
    }

    /**
     * 加载菜单
     *
     * @param {*} data
     * @memberof Titlebar
     */
    public load() {
        this.$http.get('mob/ctrl/tcmenuappmenu' + '/get').then((response: any) => {
            if (response && response.items.length >0) {
                response.items.forEach(element => {
                    this.$items.push(element);
                });
            }
        }).catch((error: any) => {
            this.$notification.error(error.error.message);
        });
    }

    /**
     * 打开ActionSheet
     *
     * @param {*} data
     * @memberof Titlebar
     */
    public async openActionSheet(item:any){
        let itemsGrounp=this.$items.filter((data:any) =>{
            return  data.id === item;
        }) 
        const tag: string = await this.$app.openActionSheet(itemsGrounp[0]);
        this.sheetclick(tag);
    }

    /**
     * 执行单个菜单点击事件
     *
     * @param {*} data
     * @memberof Titlebar
     */
    menuClick(item:any){
        this.click(item);
    }

     /**
     * 菜单点击
     *
     * @private
     * @param {*} item 菜单数据
     * @memberof Titlebar
     */
    private click(item: any) {
        if (item) {
            switch (item) {
                case '8C17B7BF-34C6-4AF5-BDC9-60BAC95F46BB': 
                   this.click_11();         
                    return;
                default:
                    console.warn('未指定应用功能');
            }
        }
    }

     /**
     * sheet点击
     *
     * @private
     * @param {*} itemid 菜单数据
     * @memberof Titlebar
     */
    private sheetclick(item: any) {
        if (item) {
            switch (item) {
                case 'B3BD93C5-FEAD-48C6-BB12-B2BB93FDBF3E': 
                    this.click('8C17B7BF-34C6-4AF5-BDC9-60BAC95F46BB');
                    return;
                default:
                    console.warn('未指定应用功能');
            }
        }
    }


//获取视图应用功能集合并判断是否是预置视图应用功能
    /**
 * 退出
 *
 * @param {*} [item={}]
 * @memberof Titlebar
 */
public async click_11(){
    const judge: boolean = await this.$notification.confirm('退出', '确认退出当前用户?');
    if (judge) {
        this.$app.clearAccess();
        this.$app.login();
    }
}

}