import { Component, OnInit,EventEmitter, AfterViewInit,Input,Output,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';
import {BGYPMobPickupMDViewPage} from '@pages/demo/bgypmob-pickup-mdview/bgypmob-pickup-mdview.page';


@Component({
  selector: 'ctrl-tree-exp-viewtreeexpbar-treeexpbar',
  templateUrl: './tree-exp-viewtreeexpbar-treeexpbar.html',
  styleUrls: ['./tree-exp-viewtreeexpbar-treeexpbar.scss']
})
export class TreeExpBar implements OnInit {

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

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


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

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

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

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

    }


    /**
     * treeexpbar_tree 部件 selectionchange 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public treeexpbar_tree_selectionchange($event: any, $event2?: any) {
        this.treeexpbar_selectionchange($event, 'treeexpbar_tree', $event2);
    }

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

    /**
     * 返回
     *
     * @memberof TreeExpBar
     */
    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();
        }
    }

    /**
     * 树视图导航当前选中项
     *
     * @memberof  TreeExpBar
     */
    public selectedItem = "";

    /**
     * 导航参数搜索值
     */
    public searchtext:any;

    /**
     * 树视图导航所有关联视图
     *
     * @memberof  TreeExpBar
     */
    public appViewRefs = {
        'yplx':'BGYPMobPickupMDView'
    };

	ngOnInit(): void {
		this.viewState.subscribe(({ tag, action, data }) => {
			if (!Object.is(tag, this.name)) {
				return;
			}
			if (Object.is('load', action)) {
				this.load(data);
			}
		});
	}

    /**
     * 树视图导航选中
     *
     * @memberof  TreeExpBar
     */
    public treeexpbar_selectionchange($event:any,name:string,$event2:any){
      if($event.srfnodetype){
        this.selectedItem = this.appViewRefs[$event.srfnodetype];
        this.searchtext = $event.param;
      }
    }

    /**
     * 树视图加载完成
     *
     * @memberof  TreeExpBar
     */
    public treeexpbar_load($event:any,name:string,$event2:any){
      if($event.srfnodetype){
        this.selectedItem = this.appViewRefs[$event.srfnodetype];
        this.searchtext = $event.param;
      }
    }

    /**
     * 返回
     *
     * @memberof  TreeExpBar
     */
    public load(data: any) {
      this.viewState.next({ tag: 'treeexpbar_tree', action: 'load', data:data })
    }

    /**
     * 当前选中数组
     *
     * @memberof  TreeExpBar
     */    
    public selectedDatas:Array<any> = [];

    /**
     * 视图数据变化事件
     *
     * @memberof  TreeExpBar
     */    
    public viewDatsChange(data:any){
        if (this.selectedDatas.length === 0) {
            data.count += 1;
            this.selectedDatas.push(data);
          } else {
            let tempFlag = true;
            this.selectedDatas.forEach((item: any) => {
              if (item.srfkey === data.srfkey) {
                item.count += 1;
                tempFlag = false;
              }
            })
            if (tempFlag) {
              data.count += 1;
              this.selectedDatas.push(data);
            }
          }
          this.getCount();
    }

    /**
     * 值变化事件
     * 
     * @memberof  TreeExpBar
     */
    public change($event,data){
        if($event === 0){
            if(data && data.srfkey){
                this.selectedDatas.forEach((item:any,index:number) =>{
                    if(Object.is(item.srfkey,data.srfkey)){
                        this.selectedDatas.splice(index,1);
                    }
                })
            }
        }
        this.getCount();
    }

    public totalCount:number = 0;
    
    public getCount(){
      if(this.selectedDatas.length >0){
        this.totalCount =0;
        this.selectedDatas.forEach((item:any) =>{
          this.totalCount += item.count;
        });
      }else{
        this.totalCount = 0;
      }
    }

    /**
     * 提交申请
     * 
     * @memberof  TreeExpBar
     */
    public submitApply(){
        console.log('提交申请');
    }




}