/**
 * 查询方案、查询面板、工具条、网格，标准查询页面
 * @param {*} queryDataset 保存数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { StringUtil } from "@birt/funclib/StringUtil.js";
import { $CONST } from "@birt/funclib/ConstUtil";
export function newInstance(queryDataset, conf) {
    
    const dlgId = StringUtil.randomString(6);
    const hcTabsId = "HcTabs_" + dlgId;
    const hcTabDetailId = "detail_" + dlgId;
    const hcToolBarId = "HcFormToolbar_" + dlgId
    const hcTableId = "HcTable_" + dlgId
    // 设置数据集id，分页
    queryDataset.controlId += "_" + dlgId;
    queryDataset._infcPagging = {
        _isPagging: true,
        pageSize: 1000
    }
    // 查询面板
    let hcQueryPanel = false;
    if (conf.hcQueryPanel) {
        hcQueryPanel = {
            controlName: "HcQueryPanel",
            controlId: "HcQueryPanel_" + dlgId,
            dataset: queryDataset.controlId,
            children: []
        }
        // 应用查询面板个性化配置
        Object.assign(hcQueryPanel, conf.hcQueryPanel || {});
    }
    
    // 操作工具条
    let hcFormToolbar = false;
    if (conf.hcFormToolbar) {
        hcFormToolbar = {
            controlName: "HcFormToolbar",
            controlId: hcToolBarId,
            dataset: queryDataset.controlId,
            children: []
        }
        // 应用表单个性化配置
        Object.assign(hcFormToolbar, conf.hcFormToolbar || {});
    }
    // 网格明细
    var hcTable = {
        controlName: "HcTable",
        controlId: hcTableId,
        dataset: queryDataset.controlId,
        border: true,
        showAddBtn: false,
        // showSummary:true,
        height: function (parentHeight) {
            console.log(parentHeight);
            return parentHeight - 250;
        },
        events: {
            // 双击跳转至详情页
            rowDblclick: function (row, column, event) {
                
                let curtTabs = this.getRefCompt(hcTabsId);
                this.getRefCompt(hcToolBarId).$el.style.display = 'none'
                let pushParam = Object.assign({ url: "/data/querys?pn = " + pn }, row);
                curtTabs.setActiveTabName(hcTabDetailId, pushParam); //
            },
        },
        children: [],
    };
    // 应用网格个性化配置
    Object.assign(hcTable, conf.hcTable || {});
     // 操作列 
    let operateColumn = {
        controlName: "HcTableColumnScope",
        controlId: "HcTableColumn_operate",
        label: "操作",
        width: 100,
        headerAlign: "center",
        fixed: "right",
        slot: "scope",
        children: [{
            controlName: "HcButton",
            controlId: "HcButton_add",
            type: "primary",
            circle: true,
            size: 'mini',
            icon: "el-icon-plus",
            disabled: function () {
            return this.store && this.store.allowAdd && !this.store.allowAdd();
            },
            events: {
            click() {
                
                var hcTable = this.scope.row
                let newRecd = this.store.newRecord();
                newRecd.forgguid = null
                newRecd.forgpguid = hcTable.forgguid
                this.store.add(newRecd);
            },
            },
        },
        {
            controlName: "HcButton",
            controlId: "HcButton_del",
            type: "danger",
            size: 'mini',
            circle: true,
            icon: "el-icon-delete",
            disabled: function () {
            return this.store && this.store.allowRemove && !this.store.allowRemove();
            },
            events: {
            click() {
                this.store.remove(this.scope.row);
            },
            },
        },
        ],
    }
    if(!hcTable.children.operateColumn){
        hcTable.children.push(operateColumn);
    }
    // 详情表单 
    var hcDetailForm = false;
    if (conf.hcDetailForm) {
        hcDetailForm = Object.assign(
            conf.hcDetailForm, {
            height: function (parentHeight) {
                return parentHeight - 10;
            },
        })
    }

    // 分页工具条
    var hcPagination = {
        controlName: "HcPagination",
        controlId: "HcPagination_" + dlgId,
        pageSizes: [10, 20, 50, 100],
        layout: "total, prev, pager, next, sizes, jumper",
        dataset: queryDataset.controlId,
    };

    var hcTabs = {
        controlName: "HcTabs",
        controlId: hcTabsId,
        tabPosition: "top",
        activeTabName: "list_" + dlgId,
        // type: "border-card",
        events:{
            click: function(){
              var tabs = this.getRefCompt(hcTabsId).activeTabName
              if (tabs.indexOf('detail') > -1) {
                this.getRefCompt(hcToolBarId).$el.style.display = 'none'
              } else {
                this.getRefCompt(hcToolBarId).$el.style.display = 'block'
              }
            }
          },
        children: [
            {
                controlName: "HcTabPane",
                controlId: "HcTabPane_list_" + dlgId,
                label: "列表",
                name: "list_" + dlgId,
                children: []
                    // .concat(hcFormToolbar ? hcFormToolbar : [])
                    .concat(hcTable)
                    .concat(hcPagination)
            },
            {
                controlName: "HcTabPane",
                controlId: "HcTabPane_detail_" + dlgId,
                label: "详情",
                name: hcTabDetailId,
                children: [].concat(hcDetailForm ? hcDetailForm : [])
            }
        ],
        slot: [].concat(hcFormToolbar ? hcFormToolbar : [])
    }

    // 应用报表模型
    var birtWorkBook = Object.assign({
        controlName: "BirtWorkBook",
        controlId: "BirtWorkBook_" + dlgId,
        showToolBar: false,
        totalPage: 1,
        renderType: "pages",
        height: function (parentHeight) {
            return parentHeight - 30;
        },
    }, conf.birtWorkBook || {})
    // 添加布局结构
    birtWorkBook.children = [
        {
            controlName: "BirtSheet",
            controlId: "BirtSheet_" + dlgId,
            name: "sheet_" + dlgId,
            pageIndex: 0,
            dataSets: [queryDataset],
            children: [
                {
                    controlName: "BirtFormSheet",
                    controlId: "BirtFormSheet_" + dlgId,
                    children: []
                        .concat(hcQueryPanel ? hcQueryPanel : [])
                        .concat(hcTabs)
                }
            ]
        }
    ]
    return birtWorkBook
}