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-mob-pickup-viewpickupviewpanel-pickupviewpanel',
  templateUrl: './mob-pickup-viewpickupviewpanel-pickupviewpanel.html',
  styleUrls: ['./mob-pickup-viewpickupviewpanel-pickupviewpanel.scss']
})
export class Pickupviewpanel implements OnInit{

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

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


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

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

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

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

    }


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

    /**
     * 是否为多选
     *
     * @type {Subject<ViewState>}
     * @memberof Pickupviewpanel
     */
    @Input() public isMulti:boolean;

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

    /**
     * 搜索文本
     *
     * @memberof Pickupviewpanel
     */    
    public quicksearchText:String ='';

    /**
     * 部件事件
     *
     * @memberof Pickupviewpanel
     */      
    @Output() public dataschangeemit =new EventEmitter();

    /**
     * 部件事件
     *
     * @memberof Pickupviewpanel
     */      
    @Output() public loademit =new EventEmitter();
    

    /**
     * angular 生命周期
     *
     * @memberof Pickupviewpanel
     */
    ngOnInit(): void {
    if (this.viewState) {
            this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                 if(Object.is(action, 'quicksearch')){
                  this.quicksearchText =data;
                }
            });
        }
  }

  /**
    * 部件数据变化抛出事件
    *
    * @memberof Pickupviewpanel
    */
  public ctrlDataChange($event){
    this.dataschangeemit.emit($event);
  }

  /**
    * 部件数据加载完成事件
    *
    * @memberof Pickupviewpanel
    */
  public ctrlLoad($event){
    this.loademit.emit($event);
  }

}