import { Component, OnInit,AfterViewInit,Output,Input,EventEmitter,ViewChild,ElementRef } from '@angular/core';
import { Subject,Unsubscribable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { NavController,ModalController } from '@ionic/angular';
import { AppNotification } from '@global/service/Notification';
import { App } from '@global/service/App';
import { Http } from '@global/service/Http';
import { ComUtil } from '@global/util/ComUtil';
import { AppEnvironment } from 'src/environments/AppEnvironment';


import MobMDViewEngine from 'src/engine/view/mob-mdview-engine';
import { Mdctrl } from '@widget/org-user/mob-mobmdctrl/mob-mobmdctrl';

@Component({
  selector: 'view-org-user-mob-mdview',
  templateUrl: './org-user-mob-mdview.page.html',
  styleUrls: ['./org-user-mob-mdview.page.scss']
})
export class OrgUserMobMDViewPage implements OnInit,AfterViewInit {

    /**
     * 子组件Mdctrl
     *
     * @public
     * @type {Subject}
     * @memberof OrgUserMobMDView
     */
    @ViewChild(Mdctrl)
    private mdctrl: Mdctrl;

    /**
     * 视图订阅对象
     *
     * @public
     * @type {Subject}
     * @memberof OrgUserMobMDView
     */
    public viewState: Subject<ViewState> = new Subject();

    /**
     * 路由传递参数
     *
     * @public
     * @type {void}
     * @memberof OrgUserMobMDView
     */
    public $route ={};

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

    /**
     * 是否为模态框弹出
     *
     * @memberof OrgUserMobMDView
     */
    @Input() isModalMode:boolean =false;

    /**
     * 是否嵌入分页导航视图
     *
     * @memberof TestEntity2MobPickupView
     */
    @Input() isTabexp:boolean =false;

    /**
     * 视图值变化事件
     *
     * @public
     * @type {EventEmitter}
     * @memberof OrgUserMobMDView
     */    
    @Output() public viewdataschange =new EventEmitter();

    /**
     * 视图参数
     *
     * @public
     * @type {Subject}
     * @memberof OrgUserMobMDView
     */
    @Input() public viewdata:any;

    /**
     * 父数据对象
     *
     * @protected
     * @type {*}
     * @memberof OrgUserMobMDView
     */
    protected srfparentdata: any = {};

    /**
     * 是否为内敛视图
     *
     * @public
     * @type {EventEmitter}
     * @memberof OrgUserMobMDView
     */ 
    @Input() public isembeddedView:boolean =false;

    /**
     * 视图值加载完成事件
     *
     * @public
     * @type {EventEmitter}
     * @memberof OrgUserMobMDView
     */ 
    @Output() public viewload =new EventEmitter();

    /**
     * 视图的关联试图Map
     *
     * @public
     * @type {Map}
     * @memberof OrgUserMobMDView
     */
    public RefViewMap =new Map();

    /**
     * 视图引擎
     *
     * @private
     * @type {Engine}
     * @memberof OrgUserMobMDView
     */
    private engine: MobMDViewEngine = new MobMDViewEngine();

    /**
     * 引擎初始化
     *
     * @private
     * @memberof OrgUserMobMDView
     */
    private engineInit(): void {
        this.engine.init({
            view: this,
            opendata: (params: any) => {
                this.opendata(params);
            },
            mdctrl: this.mdctrl,
            newdata: (params: any) => {
                this.newdata(params);
            },
        });
    }


     /**
     * angular生命周期(在ngOnInit之后)
     *
     * @memberof OrgUserMobMDView
     */
    public ngAfterViewInit(){
        this.engineInit();
        
        this.registerRefView();
    }


    /**
     * mdctrl 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public mdctrl_load($event: any, $event2?: any) {
        this.engine.onCtrlEvent('mdctrl', 'load', $event);
    }

    /**
     * mdctrl 部件 click 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public mdctrl_click($event: any, $event2?: any) {
        this.engine.onCtrlEvent('mdctrl', 'click', $event);
    }


    
    /**
     * 打开新建数据视图
     *
     * @param {*} [params={}]
     * @memberof OrgUserMobMDView
     */
    public async newdata(params: any = {}) {
    }

    
    /**
     * 打开编辑数据视图
     *
     * @param {*} [params={}]
     * @memberof OrgUserMobMDView
     */
    public async opendata(params: any = {}) {
    }


    /**
     * 返回
     *
     * @memberof OrgUserMobMDView
     */
    public backView() {
        let res:any;
        if(this.isModalMode){
            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();
            if(this.$activeData && this.$activeData.length >0){
                res = { ret: 'OK', result:this.$activeData };
            }else{
                res = { ret: 'NO', result:[] };
            }
            this.$app.closeCallback(res);
        }
    }

    /**
     * 嵌入视图点击更多
     *
     * @public
     * @memberof OrgUserMobMDView
     */
    public async click_more(){
        let leftopts:Array<any> =[
    ];
        let rightopts:Array<any> =[
    ];
        let opts:Array<any>=leftopts.concat(rightopts);
        const result: string = await this.$app.openActionSheet({items:opts});
        let _this =this;
    }

    /**
     * 点击toolbar的操作组事件
     *
     * @public
     * @memberof OrgUserMobMDView
     */
    public  async openActionSheet(data:any){

        
    }

    /**
     * 注册关联视图
     *
     * @public
     * @memberof OrgUserMobMDView
     */    
    public registerRefView(){
    }   

    
    /**
     * 实例化对象
     *
     * @memberof OrgUserMobMDView
     */
    constructor(public $http:Http,public $navCtrl: NavController, public $notification: AppNotification, public $activatedRoute: ActivatedRoute,public $modalCtrl:ModalController,public $app:App) {
        this.$activatedRoute.queryParams.subscribe(params => {
        this.$route ="";
        if(params){
            this.$route =JSON.stringify(params);
        }
         this.engineInit();
        })
    }

    /**
     * angular生命周期(组件初始化)
     *
     * @memberof OrgUserMobMDView
     */
    public ngOnInit() {
       
    }

    /**
     * 视图抛出值变化事件
     *
     * @public
     * @memberof OrgUserMobMDView
     */
    public viewDataChanges($event:any){
        this.viewdataschange.emit($event);
    }

    /**
     * 快速搜索
     *
     * @public
     * @memberof OrgUserMobMDView
     */
    public onQuickSearchValueChange($event:any){
        this.viewState.next({tag:'mdctrl',action:'quicksearch',data:$event.detail.value});
    }
}


