import { Component, OnInit } from '@angular/core';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import * as _ from 'lodash';
import { AppComponent } from '../../../..';

declare var $: any;

@Component({
  selector: 'app-cf-organization-tree',
  templateUrl: './cf-organization-tree.component.html',
  styleUrls: ['./cf-organization-tree.component.scss']
})
export class CfOrganizationTreeComponent extends FlexTableComponent implements OnInit {

  query = {
    status:""
  }


  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

  QUERY_URL = "cf/cfOrganization/page.htm";
  DELETE_URL = "cf/cfOrganization/delete.htm";
  GET_URL = "cf/cfOrganization/get.htm";
  ADD_URL = "cf/cfOrganization/add.htm";
  EDIT_URL = "cf/cfOrganization/edit.htm";
  CITY_URL = "cf/cfCity/list.htm";

   org_type : any;

  examFlag = true;
  bzOrgList = [];

  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    this.formModel = fb.group({
      'id': [null],
      'pid': [null],
      'org_name': ['', Validators.required],
      'org_code': ['', Validators.compose([Validators.required, Validators.pattern("^[a-zA-Z0-9]+$")])],
      'org_short_name': ['', Validators.required],
      'org_standard_code': ['', Validators.pattern("^[a-zA-Z0-9]+$")],
      'org_type': ['', Validators.required],
      'city_code': [''],
      'contact_number': [''],
      'org_address': [''],
      'exam_flag': [''],
      'remark': [''],
      'status': ['', Validators.required],
      'org_code_bz': [''],
    });
  }

  ngOnInit() {
    this.initQuery(-1);

    FrameRequest.requestArray("cf/cfOrganization/queryBzOrg.htm", {}, (arrary)=>{
      this.bzOrgList = arrary;
    });
  }

  initQuery(org_type?: any) {
    FrameRequest.requestArray('cf/cfOrganization/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", org_type);
      this.clickedTreeNode = rootNode;
      this.ptreeNode = rootNode;
      this.treeObj.selectNode(rootNode, true);//将指定ID的节点选中
    });
  }

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

  selectBzOrg(org_code_bz) {
    
    if(null == org_code_bz){
      this.item.org_name_bz="";
      this.item.org_address="";
      this.item.contact_number="";
      return;
    }

    for (let i=0;i<this.bzOrgList.length;i++) {
      if (this.bzOrgList[i].org_code_bz == org_code_bz) {
        this.item.org_name_bz=this.bzOrgList[i].org_name_bz;
        this.item.org_address=this.bzOrgList[i].org_address;
        this.item.contact_number=this.bzOrgList[i].contact_number;
        
        break;
      }
    }
  }

  searchParam(param) {
    let paramTemp = {};
    if (!this.clickedTreeNode) {
      paramTemp['org_type'] = -1;
    } else {
      this.ptreeNode = this.clickedTreeNode;
      // if(this.clickedTreeNode.isParent){
      delete param['id']
      paramTemp['org_type'] = this.clickedTreeNode.id;
      if (!this.item) {
        this.item = {};
      }
      this.item.org_type = this.clickedTreeNode.id;
      // }else{
      //   delete param['org_type']
      //   paramTemp['id'] = this.clickedTreeNode.id;
      // }
    }
    paramTemp['status'] = this.query.status;
    _.assignIn(param, paramTemp);
  }

  //参数处理
  async preGoEditEvent(item) {
    if (!this.clickedTreeNode) {
      AppComponent.alert({ text: "请选择父科目!" })
      return false;
    }
    if (!this.item) {
      this.item = {};
    }
    let _item = item ? item : this.item;
    if (!_item['pid'])
      _item['pid'] = this.clickedTreeNode.id;
    if (!_item['id'])
      _item['id'] = "";
    if (this.item && this.item.org_type === 4) {
      this.examFlag = false;
    } else {
      this.examFlag = true;
    }
    return true;
  }

  async aftSaveEvent() {
    this.initQuery(this.item.org_type);
  }

  async aftDeleteEvent() {
    this.initQuery(this.item.org_type);
  }

  //ZTREE 事件
  onClickTreeNode(event, treeId, treeNode) {
    let _pthis = window['pl_subject_this'];
    _pthis.clickedTreeNode = treeNode;
    _pthis.framePager.reload();
  }

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

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


  chooseExam(org_type: any) {
    if (Number(org_type) === 4) {
      this.examFlag = false;
    } else {
      this.examFlag = true;
    }
  }

  resetPage(){
    this.editShowType = null;
    this.item = {org_type: this.item.org_type};
  }

}
