import { Component, OnInit, Input,Output,EventEmitter,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';

@Component({
	selector: 'ctrl-tcmenu-appmenu',
	templateUrl: './tcmenu-appmenu.html',
	styleUrls: ['./tcmenu-appmenu.scss'],
})
export class Titlebar_rightappmenu implements OnInit {

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

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


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

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

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

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

    }


    /**
     * 返回
     *
     * @memberof Titlebar_rightappmenu
     */
    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 null;
    }

	/**
     * api地址
     *
     * @private
     * @type {string}
     * @memberof Titlebar_rightappmenu
     */
    private url: string = 'mob/ctrl/tcmenuappmenu';

	/**
     * 菜单数据
	 *
     * @private
     * @type {string}
     * @memberof Titlebar_rightappmenu
     */
    private $items:Array<any>;

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

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

	/**
     * 菜单点击事件
     *
     * @param {*} data
     * @memberof Titlebar_rightappmenu
     */
	public menuClick($event){

	}

	/**
     * 打开功能页
     *
     * @param {*} data
     * @memberof Titlebar_rightappmenu
     */
	public openAppFuncPickupView($event){

	}

}