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/bgyp/mob-mobmdctrl/mob-mobmdctrl';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


     /**
     * angular生命周期(在ngOnInit之后)
     *
     * @memberof BGYPMobPickupMDView
     */
    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 BGYPMobPickupMDView
     */
    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 BGYPMobPickupMDView
     */
    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 BGYPMobPickupMDView
     */
    public  async openActionSheet(data:any){

        
    }

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

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

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

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

    /**
     * 搜索值变化 quicksearch
     *
     * @memberof BGYPMobPickupMDView
     */ 
    @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 BGYPMobPickupMDView
     */
    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 BGYPMobPickupMDView
     */
    public ngOnInit() {
        
    }

}