/**
 * 树型组件编辑：新增、修改、删除，根据名称过滤节点操作
 * @param {*} dlgId 对话框随机id,防止id冲突与数据集id统一命名引用
 * @param {*} dsId 绑定过滤数据集ID
 * @param {*} option 扩展选项
 * @returns 
 */
 import pinyin from "pinyin";
 import { JsonUtil } from "@birt/funclib/JsonUtil"


 export function treeEditFilter(dlgId, dsId, option) {
    debugger
    let filterField = option.hcTree.labelField;
    let lazy = option.hcTree.lazy;
    let fireAction = lazy ? "" : "keyup";
    let treeId = "HcTree_org";
    // 过滤编辑器
    let inputFilter = {
        controlName: "HcInputFilter",
        controlId: "HcInputFilter_" + dlgId,
        size: "small",
        fireAction: fireAction,
        events: {
            filterChange: function (filterKey) {
                this.getRefCompt(treeId).filter(
                    filterKey
                );
            },
        }
    };
     // 删除
     let btnDeleteId = "HcLookup_delete_" + dlgId;
     let buttonDelete = {
         controlName: "HcLookup",
         controlId: btnDeleteId,
         dataset: dsId,
         lookDialog: {
             footer: ["cancel", "delete"],
            //  events: {
            //      deleted:function() {
            //          var _this = this.getWorkBook().dataset[dsId]
            //          var vm = this
            //          setDatas(_this, vm, 'delete')
            //      },
            //    },
         },
         children: [
             {
                 controlName: "HcButton",
                 title: "删除",
                 icon: "el-icon-delete",
                 size: "mini",
                 style: "float:left;margin-left:5px;width:80px;",
                 vshow: false,
                 events: {
                     click: function () {
                         debugger
                         let treeMenu = this.getRefCompt(treeId);
                         let currentRow = treeMenu.elTree().getCurrentNode();
                         if (!currentRow || currentRow._isVirtualRoot) {
                             this.$message({
                                 message: '请选择要删除的节点',
                                 type: 'warning'
                             });
                             return;
                         }
                         let btnDelete = this.getRefCompt(btnDeleteId);
                         let pushRecd = btnDelete.getPushAddMapRecds(currentRow);
                         let pushParam = {
                             pushType: "delete",
                             dataset: btnDelete.lookDataset.dataset,
                             // data: [pushRecd]
                         };
                         Object.assign(pushParam,pushRecd);
                         btnDelete.openLookDialog({ __push: pushParam })
                     },
                 },
             }
         ]
     }
     function remove(node,data,store,_this){
         debugger
         let wb = _this.getWorkBook();
         wb.param.node = node
         wb.param.treeData = data
         let btnDelete = _this.getRefCompt(btnDeleteId);
         var pushRecd = data;
         var pushParam = {
             pushType: "delete",
             dataset: btnDelete.lookDataset.dataset,
             data: [pushRecd]
         };
         Object.assign(pushParam,pushRecd);
         btnDelete.openLookDialog({ __push: pushParam })
     }
     JsonUtil.deepMerge(buttonDelete, option.hcLookupDelete);
     JsonUtil.deepMerge(buttonDelete, option.hcLookup);
     // 编辑树型按钮工具条
     let treeEditToolbar = {
        controlName: "HcRow",
        style: "margin-top:5px;",
        children: [
            buttonDelete,
        ]
    }
     // 编辑树组件
     let hcTree = {
         controlName: "HcTree",
         controlId: treeId,
         dataset: dsId,
         labelField: filterField,
         // defaultExpandAll: true,
         // accordion: true,
         highlightCurrent: true,
         expandOnClickNode: false,
         lazy: false,
         mounted: function () {
            debugger
            this.store.tree = this
            this.store.on("afterLoadData", afterLoadData);
            function afterLoadData(data){
                debugger
                let tree = this.tree
                let key = this._infcTree.idField
                let pKey = this._infcTree.parentIdField
                if(tree && key && pKey){
                    if(data && data[0][pKey] == -1){
                        if(tree.defaultExpandedKeys){
                            tree.defaultExpandedKeys.push(data[0][key])
                        }else{
                            tree.defaultExpandedKeys = [data[0][key]]
                        }
                    }
                }
            }
        },
         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 - 80;
         },
         style: "overflow: auto;border:1px solid rgb(235, 238, 245);margin-top: 5px;",
         renderContent(h, { node, data, store}) {
             var _this = this
             var iconClass = ''
             switch (data.FORGKIND) {
                 case '.ROOT':
                     iconClass = 'iconfont icon-root'
                     break;
                 case '.ORG':
                     iconClass = 'iconfont icon-org'
                     break;
                 case '.DPT':
                     iconClass = 'iconfont icon-dpt'
                     break;
                 case '.PTM':
                     iconClass = 'iconfont icon-ptm'
                     break;
                 case '.PSM':
                     iconClass = 'el-icon-user'
                     break;
             
                 default:
                     iconClass = ''
                     break;
             }
             return (
               <span class="custom-tree-node">
                 <span title={data.FORGKINDNAME}>
                    <i class={iconClass}></i>
                    {node.label}
                 </span>
                 {/* <span class="btn">
                   <el-button size="large" title="删除" type="text" icon="el-icon-delete" on-click={ () => remove(node,data,store,_this) }></el-button>
                 </span> */}
               </span>);
           }
     };
     // 应用树个性化配置
     Object.assign(hcTree, option.hcTree);
 
    // 返回布局结构
    return [
        inputFilter,
        treeEditToolbar,
        hcTree
    ]

}















































































