/**
 * 查询方案、查询面板、工具条、网格，标准查询页面
 * @param {*} queryDataset 保存数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { StringUtil } from "@birt/funclib/StringUtil.js";
import { $CONST } from "@birt/funclib/ConstUtil";
import { QueryStyleFactory } from "../query-panel/QueryStyleFactory";

export function TabTableDetail(conf) {
    const dlgId = StringUtil.randomString(6);
    const hcTabsId = "HcTabs_" + dlgId;
    const hcTabListId = "list_" + dlgId;
    const hcTabDetailId = "detail_" + dlgId;
    const hcQueryPanelId = "HcQueryPanel_" + dlgId;
    // 设置数据集id，分页
    var queryDataset = conf.mainDataset;
    queryDataset.controlId += "_" + dlgId;
    queryDataset._infcPagging = {
        _isPagging: true,
        returnCount: true
    }

    // 查询面板
    let hcQueryPanel = false;
    if (conf.hcQueryPanel) {
        hcQueryPanel = {
            controlName: "HcQueryPanel",
            controlId: hcQueryPanelId,
            dataset: queryDataset.controlId,
            children: []
        }
        // 应用查询面板个性化配置
        Object.assign(hcQueryPanel, QueryStyleFactory.buildForm(conf.hcQueryPanel) || {});
    }

    // 操作工具条
    let hcFormToolbar = false;
    if (conf.hcFormToolbar) {
        hcFormToolbar = {
            controlName: "HcFormToolbar",
            controlId: "HcFormToolbar_" + dlgId,
            dataset: queryDataset.controlId,
            children: []
        }
        // 应用表单个性化配置
        Object.assign(hcFormToolbar, conf.hcFormToolbar || {});
    }
    // 网格明细
    var hcTable = {
        controlName: "HcTable",
        controlId: "HcTable_" + dlgId,
        dataset: queryDataset.controlId,
        border: true,
        showAddBtn: false,
        // showSummary: true,
        height: function (parentHeight) {
            return parentHeight - 120;
        },
        events: {
            // 双击跳转至详情页
            rowDblclick: function (row, column, event) {
                debugger
                let curtTabs = this.getRefCompt(hcTabsId);
                let pushParam = Object.assign({}, row);
                let eleNumber = this.store && this.store._getEleNumber();
                if (eleNumber) {
                    pushParam[$CONST.FLOWNUMBER] = row[eleNumber.field];
                }
                // 下推功能路径
                // pushParam[$CONST.FUNCPATH] = this.getWorkBook().getModelPath();
                curtTabs.setActiveTabName(hcTabDetailId, pushParam);
            },
        },
        children: [],
    };
    // 应用网格个性化配置
    Object.assign(hcTable, conf.hcTable || {});

    // 详情表单 
    var hcDetailForm = false;
    if (conf.hcDetailForm) {
        hcDetailForm = Object.assign(
            conf.hcDetailForm,
            {
                height: function (parentHeight) {
                    debugger
                    return parentHeight - 170;
                },
            })
    }

    // 分页工具条
    var hcPagination = {
        controlName: "HcPagination",
        controlId: "HcPagination_" + dlgId,
        pageSizes: [10, 20, 50, 100],
        layout: "total, prev, pager, next, sizes, jumper",
        dataset: queryDataset.controlId,
    };
    // 返回按钮
    var returnBtn = {
        controlName: 'HcButton',
        controlId: 'HcButton_' + dlgId,
        title: '返回',
        size: 'mini',
        style: 'position: absolute;top:35px;left:35px;z-index:2001',
        events:{
            click:function(){
                debugger
                let curtTabs = this.getRefCompt(hcTabsId);
                let qPanel = this.getRefCompt(hcQueryPanelId);
                curtTabs.setActiveTabName(hcTabListId);
                if(qPanel){
                    qPanel.$refs[hcQueryPanelId].doQuery()
                }
            }
        }
    }
    var hcTabs = {
        controlName: "HcTabs",
        controlId: hcTabsId,
        tabPosition: "top",
        activeTabName: hcTabListId,
        tabPosition: 'right',
        // type: "border-card",
        children: [
            {
                controlName: "HcTabPane",
                controlId: "HcTabPane_list_" + dlgId,
                label: "列表",
                name: hcTabListId,
                children: []
                    .concat(hcQueryPanel ? hcQueryPanel : [])
                    .concat(hcFormToolbar ? hcFormToolbar : [])
                    .concat(hcTable)
                    .concat(hcPagination)
            },
            {
                controlName: "HcTabPane",
                controlId: "HcTabPane_detail_" + dlgId,
                label: "详情",
                name: hcTabDetailId,
                children: []
                    .concat(returnBtn)
                    .concat(hcDetailForm ? hcDetailForm : [])
            }
        ]
    }
    // 应用报表模型
    var birtWorkBook = Object.assign({
        controlName: "BirtWorkBook",
        controlId: "BirtWorkBook_" + dlgId,
        showToolBar: false,
        totalPage: 1,
        renderType: "pages",
        height: function (parentHeight) {

            return parentHeight - 1;
        },
    }, 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
}