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 MobPickupMDViewEngine from 'src/engine/view/mob-pickup-mdview-engine';
import { Mdctrl } from '@widget/xm/mob-mobmdctrl/mob-mobmdctrl';

@Component({
  selector: 'view-xmmob-pickup-mdview',
  templateUrl: './xmmob-pickup-mdview.page.html',
  styleUrls: ['./xmmob-pickup-mdview.page.scss']
})
export class XMMobPickupMDViewPage implements OnInit,AfterViewInit {

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * 引擎初始化
     *
     * @private
     * @memberof XMMobPickupMDView
     */
    private engineInit(): void {
        this.engine.init({
            view: this,
            mdctrl: this.mdctrl,
        });
    }


     /**
     * angular生命周期(在ngOnInit之后)
     *
     * @memberof XMMobPickupMDView
     */
    public ngAfterViewInit(){
        this.engineInit();
            if (this.viewState) {
        this.viewState.subscribe(({ tag, action, data }) => {
            if (!Object.is(tag, this.name)) {
                return;
            }
            this.viewState.next({tag:tag, action:action, data:data})
        });
    }

        this.registerRefView();
    }


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

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



    /**
     * 返回
     *
     * @memberof XMMobPickupMDView
     */
    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 XMMobPickupMDView
     */
    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 XMMobPickupMDView
     */
    public  async openActionSheet(data:any){

        
    }

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

    
    /**
     * 是否多选
     *
     * @memberof XMMobPickupMDView
     */
    @Input() public isMulti:boolean;

    /**
     * 是否多选
     *
     * @memberof XMMobPickupMDView
     */    
    @Input() public name:string;

    /**
     * 已选择值
     *
     * @memberof XMMobPickupMDView
     */ 
    @Input() public oldSelected:Array<any>;

    /**
     * 搜索值变化 quicksearch
     *
     * @memberof XMMobPickupMDView
     */ 
    @Input() set searchtext(val: any) {
        if(val){
            this.viewState.next({ tag: 'mdctrl', action: 'quicksearch', data: val });
        }
    }

    /**
     * 搜索值变化 search
     *
     * @memberof BGYPMobPickupMDView
     */ 
    @Input() set loadText(val: any) {
        if(val){
            setTimeout(() => {
                this.viewState.next({ tag: 'mdctrl', action: 'search', data: val });
            }, 500);
        }
    }

    /**
     * 实例化对象
     *
     * @memberof XMMobPickupMDView
     */
    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);
        }
        })
    }

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

}