import { Component, OnInit, ViewChild } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal/bs-modal.service';
import 'ztree';
import * as _ from 'lodash';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { FlexModalComponent } from '../../../../../shared/common/FlexModal.component';
declare var $: any;

@Component({
  selector: 'pl-subject-modal',
  templateUrl: './flex-pl-subject.component.html',
  styleUrls: ['./flex-pl-subject.component.scss']
})

export class PlSubjectModal extends FlexModalComponent implements OnInit {

  setting = {
    data: {
      simpleData: {
        enable: true,
        idKey: 'id',
        pIdKey: 'pid',
        rootPid: -1
      }
    },
    check: {
      enable: false,
      chkStyle: "checkbox",
      chkboxType : { "Y" : "ps", "N" : "ps" }
    },
    key: {
      checked: 'checked'
    },
    callback: {
      onClick: this.onClickTreeNode
    }
  };

  zNodes: any;
  nodeData: any;
  treeObj: any;
  checkAllTreeToggleFlag = true;
  expandAllTreeToggleFlag = true;

  clickTreePId
  subjectList = []
  ptreeNode
  clickedTreeNode

  constructor(
    public bsModalService: BsModalService,
  ) { 
    super(bsModalService);
  }
   
  ngOnInit() {

  }

  public show(){
    this.initQuery();
    this.radioItem = {};
    let size = this.size ?`modal-${this.size}`:"";
    this.shouModalWin(this.queryTemplateModal,size);
  }

  initQuery(){
    FrameRequest.requestArray('pl/plSubject/all.htm', {}, (array) => {
      this.nodeData = array;
      this.zNodes = $.fn.zTree.init($('#tree'), this.setting, this.nodeData);
      this.treeObj = $.fn.zTree.getZTreeObj("tree");
      window['pl_subject_this'] = this;
      //默认展开
      this.treeObj.expandAll(true)
      let rootNode = this.treeObj.getNodeByParam("id",0 );
      this.clickedTreeNode = rootNode;
      this.ptreeNode = rootNode;
      this.treeObj.selectNode(rootNode,true);//将指定ID的节点选中
    });
  }

  ngOnDestory(){
    delete window['pl_subject_this'];
  }

  public _confirm(){
    if(this.confirm.observers.length > 0){
      this.confirm.emit(this);
    }
    this.hideModal();
  }
  
  //ZTREE 事件
  onClickTreeNode(event, treeId, treeNode){
    let _pthis = window['pl_subject_this'];
    _pthis.clickedTreeNode = treeNode;

  }

  //全选切换
  public checkAllTreeToggle(){
    this.treeObj.checkAllNodes(this.checkAllTreeToggleFlag = !this.checkAllTreeToggleFlag);
  }

  //展开切换
  public expandAllTreeToggle(){
    this.treeObj.expandAll(this.expandAllTreeToggleFlag = !this.expandAllTreeToggleFlag);
  }


}
