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 { XMMobPickupMDViewPage } from '@pages/demo/xmmob-pickup-mdview/xmmob-pickup-mdview.page';

import MobPickupViewEngine from 'src/engine/view/mob-pickup-view-engine';
import { Pickupviewpanel } from '@widget/xm/mob-pickup-viewpickupviewpanel-pickupviewpanel/mob-pickup-viewpickupviewpanel-pickupviewpanel';

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

    /**
     * 子组件Pickupviewpanel
     *
     * @public
     * @type {Subject}
     * @memberof XMMobPickupView
     */
    @ViewChild(Pickupviewpanel)
    private pickupviewpanel: Pickupviewpanel;

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

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

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

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

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

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

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

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

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

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

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

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

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


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


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

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



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

        
    }

    /**
     * 注册关联视图
     *
     * @public
     * @memberof XMMobPickupView
     */    
    public registerRefView(){
        this.RefViewMap.set('XMMobPickupMDView',XMMobPickupMDViewPage);
    }   

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

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

    /**
     * 视图抛出值事件
     *
     * @memberof XMMobPickupView
     */
    public viewDatasChange($event){
        this.viewdataschange.emit($event);
        this.closeView({selected:$event});
    }

    /**
     * 关闭模态框
     *
     * @memberof XMMobPickupView
     */
    public closeView(data: any = {}): void {
        const res = { ret: 'OK', ...data };
        this.$modalCtrl.dismiss(res, 'close');
        return;
    }

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

    /**
     * 视图加载完成事件
     *
     * @memberof XMMobPickupView
     */
    public viewLoad($event){
        this.viewload.emit($event);
    }

}
