/**
 * 树节点查找返回，可多选，单选
 * @param {*} lookDataset 查找数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { inputFilterSelect } from "../compose/input-filter-select"
import { StringUtil } from "@birt/funclib/StringUtil.js";


export function newInstance(lookDataset, conf) {
    const dlgId = StringUtil.randomString(6);
    // 设置数据集id
    lookDataset.controlId += "_" + dlgId;
    // 关键字过滤框，带已未选方案
    let inputFilter = inputFilterSelect(dlgId, lookDataset.controlId, conf.hcTree.labelField);
    // 树节点选择器
    // 只返回父节点
    let checkStrictly = true;
    if (conf.hcTree.checkStrictly !== undefined) {
        checkStrictly = conf.hcTree.checkStrictly;
    }
    let hcTree = {
        controlName: "HcTree",
        controlId: "HcTree_" + dlgId,
        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,
        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 || []);
                }
            }
        }
    }
    // 返回布局结构
    return {
        controlName: "BirtWorkBook",
        controlId: "BirtWorkBook_" + dlgId,
        showToolBar: false,
        totalPage: 1,
        renderType: "pages",
        height: function (parentHeight) {
            return parentHeight;
        },
        confirm: function (callFun) {
            let dsLk = this.dataset[lookDataset.controlId];
            let retuData = dsLk.getCheckedRecds(!checkStrictly, false);
            return retuData;
        },
        children: [
            {
                controlName: "BirtSheet",
                controlId: "BirtSheet_" + dlgId,
                name: "sheet_" + dlgId,
                pageIndex: 0,
                dataSets: [lookDataset],
                children: [
                    {
                        controlName: "BirtFormSheet",
                        controlId: "BirtFormSheet_" + dlgId,
                        children: [
                            inputFilter,
                            hcTree
                        ]
                    }
                ]
            }
        ]
    }
}