/**
 * 树节点查找返回，可多选，单选
 * @param {*} lookDataset 查找数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { inputFilterSelect } from "../compose/input-filter-select"
import { StringUtil } from "@birt/funclib/StringUtil.js";
import pinyin from "pinyin";
 
 var treeClickCount = 0,
   timer = null
 export function TreeSingleSelect(conf) {
     
     const dlgId = StringUtil.randomString(6);
     const treeId = "HcTree_" + dlgId;
     // 设置数据集id
     let lookDataset = conf.lookDataset;
     lookDataset.controlId += "_" + dlgId;
     // 关键字过滤框，带已未选方案
     let inputFilter = inputFilterSelect(dlgId,treeId, lookDataset.controlId, conf.hcTree.labelField);
     // 树节点选择器
     // 只返回父节点
     let checkStrictly = true;
     if (conf.hcTree.checkStrictly !== undefined) {
         checkStrictly = conf.hcTree.checkStrictly;
     }
     let hcTree = {
         controlName: "HcTree",
         controlId: treeId,
         // lazy: true,
         dataset: lookDataset.controlId,
         isTreeData: false,
         idField: conf.hcTree.idField,
         parentIdField: conf.hcTree.parentIdField,
         labelField: conf.hcTree.labelField,
         // showCheckbox: true,
         defaultExpandAll: true,
         checkStrictly: checkStrictly,
         filterNodeMethod:function(value, data,node) {
          if (!value){
              return true
          }else{
              let key =(node.label +"_" + pinyin.getCamelChars(node.label)).toUpperCase();
              return key.indexOf(value.toUpperCase()) !== -1;
          }
      },
         height: function (parentHeight) {
             return parentHeight - 55;
         },
         style: "overflow: auto;border:1px solid rgb(235, 238, 245);margin-top: 10px;",
         mounted: function () {
             this.getRefCompt("BirtWorkBook_" + dlgId).on("afterLoad", doPageAfterLoad);
             let me = this;
             function doPageAfterLoad(param) {
                 if (param) {
                     me.elTree().setCheckedNodes(param.lastTargetRetuData || []);
                 }
             }
         },
         events:{
          currentChange: function (data, node) {
               
               this.store.setCurtRecord(data);
           },
           nodeClick: function (data) {
             debugger
             treeClickCount++;
             if (treeClickCount >= 2) {
               return;
             }
             //计时器,计算300毫秒为单位,可自行修改
             timer = window.setTimeout(() => {
               if (treeClickCount == 1) {
                 //把次数归零
                 treeClickCount = 0;
                 //单击事件处理
               } else if (treeClickCount > 1) {
                 //把次数归零
                 treeClickCount = 0;
                 //双击事件
                 if(data.fresguid && (data.frestype.indexOf('dir') > -1 || data.frestype == 'pro')){
                   this.$message({
                     type: 'warning',
                     message: '只能选中流程或功能节点'
                   })
                 }else{
                   let dlg = this.getWorkBook().dialog;
                   if (dlg) {
                       dlg._doConfirm(data);
                   }
                 }
               }
             }, 300);
           }
         }
         
     }
     // 返回布局结构
     return {
         controlName: "BirtWorkBook",
         controlId: "BirtWorkBook_" + dlgId,
         showToolBar: false,
         totalPage: 1,
         renderType: "pages",
         height: function (parentHeight) {
             return parentHeight;
         },
         confirm: function (callFun) {
             debugger
             // let dsLk = this.dataset[lookDataset.controlId].getCurtRecord();
             let dsLk = this.dataset[lookDataset.controlId].curtRecord;
             let retuData = [];
             retuData[0] = dsLk
             if(retuData[0].fresguid && (retuData[0].frestype.indexOf('dir') > -1 || retuData[0].frestype == 'pro')){
                 this.$message({
                 type: 'warning',
                 message: '只能选中流程或功能节点'
                 })
             }else{
                 return retuData;
             }
         },
         children: [
             {
                 controlName: "BirtSheet",
                 controlId: "BirtSheet_" + dlgId,
                 name: "sheet_" + dlgId,
                 pageIndex: 0,
                 dataSets: [lookDataset],
                 children: [
                     {
                         controlName: "BirtFormSheet",
                         controlId: "BirtFormSheet_" + dlgId,
                         children: [
                             inputFilter,
                             hcTree
                         ]
                     }
                 ]
             }
         ]
     }
 }