/**
 * 查找定义，网格单选返回
 * @param {*} lookDataset 查找数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { QueryStyleFactory } from "./query-panel/QueryStyleFactory";

export function GridSingleSelect(conf) {
    const dlgId = 'sam';
    const tableID = "HcTable_" + dlgId;
    // 设置数据集id
    let lookDataset = conf.mainDataset;
    lookDataset.controlId += "_" + dlgId;
    if (!lookDataset._infcPagging) {
        lookDataset._infcPagging = {
            _isPagging: true,
            returnCount: true
        }
    }
    // 横向查询面板
    let hcQueryPanel = true;
    if (conf.hcQueryPanel) {
        hcQueryPanel = {
            controlName: "HcQueryPanel",
            controlId: "HcQueryPanel_" + dlgId,
            inline: true,
            dataset: lookDataset.controlId,
            size: "small",
            children: []
        }
        // 应用查询面板个性化配置
        Object.assign(hcQueryPanel, QueryStyleFactory.buildForm(conf.hcQueryPanel));
    }

    // 网格明细
    var hcTable = {
        isEdit: false,
        controlName: "HcTable",
        controlId: tableID,
        dataset: lookDataset.controlId,
        highlightCurrentRow: true,
        height: function (parentHeight) {
            return parentHeight - 125;
        },
        children: [],
        events: {
            rowDblclick: function (row, column, event) {
                let dlg = this.getWorkBook().dialog;
                if (dlg) {
                    dlg._doConfirm(row);
                }
            },
        }
    };
    // 应用网格个性化配置
    Object.assign(hcTable, conf.hcTable || {});

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


    // 应用报表模型
    var birtWorkBook = Object.assign({
        controlName: "BirtWorkBook",
        controlId: "BirtWorkBook_" + dlgId,
        showToolBar: false,
        totalPage: 1,
        renderType: "pages",
        height: function (parentHeight) {
            return parentHeight;
        },
        confirm: function (row) {
            if (row instanceof MouseEvent) {
                let dsLk = this.dataset[lookDataset.controlId];
                let retuData = dsLk.getCurtRecord();
                return [retuData];
                // let tb = this.cmps[tableID];
                // return [tb.curtRow];
            } else {
                return [row];
            }
        },
    }, conf.birtWorkBook || {})
    // 添加布局结构
    birtWorkBook.children = [
        {
            controlName: "BirtSheet",
            controlId: "BirtSheet_" + dlgId,
            name: "sheet_" + dlgId,
            pageIndex: 0,
            dataSets: [lookDataset],
            height: function (parentHeight) {
                return parentHeight;
            },
            children: [
                {
                    controlName: "BirtFormSheet",
                    controlId: "BirtFormSheet_" + dlgId,
                    children: []
                        .concat(hcQueryPanel ? hcQueryPanel : [])
                        .concat(hcTable)
                        .concat(hcPagination)
                }
            ]
        }
    ]
    return birtWorkBook
}