/**
 * 查找定义，网格多选返回
 * @param {*} lookDataset 查找数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */

import axios from "@birt/funclib/AxiosUtil";
import { StringUtil } from "@birt/funclib/StringUtil.js";


export function newInstance(lookDataset, conf) {
    const dlgId = StringUtil.randomString(6);
    const tableID = "HcTable_" + dlgId;
    const selectedTableId = "HcTable_2_" + dlgId;
    // 克隆选中数据集
    let selectDataset = JSON.parse(JSON.stringify(lookDataset));
    selectDataset.controlId += "_2_" + dlgId;
    selectDataset._infcPush.receiveLookupInput = "";
    // 设置数据集id
    lookDataset.controlId += "_" + dlgId;
    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, conf.hcQueryPanel || {});
    }

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

    // 网格已选记录集
    var hcSelectedTable = {
        controlName: "HcTable",
        controlId: selectedTableId,
        dataset: selectDataset.controlId,
        highlightCurrentRow: true,
        height: function (parentHeight) {
            return parentHeight - 125;
        },
        children: [],
        events: {
            rowDblclick: function (row, column, event) {
                let rowKeyVal = this.store.getRowKeyValue(row);
                this.store.data.forEach((r, i) => {
                    let rKeyVal = this.store.getRowKeyValue(r);
                    if (rowKeyVal == rKeyVal) {
                        this.store.data.splice(i, 1);
                        return true;
                    }
                })
            }
        }
    }
    // 应用网格个性化配置
    Object.assign(hcSelectedTable, 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",
        axios: axios,
        height: function (parentHeight) {
            return parentHeight;
        },
        confirm: function () {
            let dsLk = this.dataset[selectDataset.controlId];
            return dsLk.data;
        },
        events: {
            afterLoad: function (param) {
                
                let dsSelect = this.dataset[selectDataset.controlId];
                dsSelect.setData([]);
            }
        },
        // mounted: function () {
        //     
        //     this.on(
        //         "afterLoad",
        //         function () {
        //             alert(1);
        //         }
        //     );
        // }
    }, conf.birtWorkBook || {})
    // 添加布局结构
    birtWorkBook.children = [
        {
            controlName: "BirtSheet",
            controlId: "BirtSheet_" + dlgId,
            name: "sheet_" + dlgId,
            pageIndex: 0,
            dataSets: [lookDataset, selectDataset],
            height: function (parentHeight) {
                return parentHeight;
            },
            children: [
                {
                    controlName: "BirtFormSheet",
                    controlId: "BirtFormSheet_" + dlgId,
                    children: []
                        .concat(hcQueryPanel ? hcQueryPanel : [])
                        .concat({
                            controlName: "HcRow",
                            children: [
                                {
                                    controlName: "HcCol",
                                    span: 16,
                                    children: [hcTable]
                                },
                                {
                                    controlName: "HcCol",
                                    span: 8,
                                    children: [hcSelectedTable]
                                }
                            ]
                        })
                        .concat(hcPagination)
                }
            ],
        }
    ]
    return birtWorkBook
}