/**
 * <p>
 *  edit.js
 * </p>
 *
 * @author Administrator
 * @since 2021/5/24
 */
let editPage;
let layer;
layui.use(['form', 'layer', 'editPage', 'laytpl', 'laydate', 'element', 'table', 'dict', 'datalabel'], function () {
    let form = layui.form,
        laytpl = layui.laytpl,
        laydate = layui.laydate,
        table = layui.table;
    editPage = layui.editPage;
    layer = layui.layer;
    // 获取请求参数
    const requestParam = layui.url().search;

    // 数据资源表map，由子页面回传
    let metadataTableMap = new Map();

    // 初始化编辑表单
    editPage.init({
        formId: 'metadataForm',
        formInitUrl: requestParam.id ? `${ctx}/business/metadata/get/${requestParam.id}` : '',
        formSubmitInvoke: async function () { // 自定义表单提交时的方法
            let metadataBasic = form.val('metadataForm');
            console.info(user);
            if(metadataBasic.secretLevel > user.secretLevel){
                layer.alert('禁止上传超过自身涉密等级的数据，您的涉密等级为【'+ DICT.getText('secret_level', user.secretLevel) +'】', {icon:2})
                return;
            }
            metadataBasic.metadataDetailsList = [];
            for (let metadata of metadataTableMap.values()) {
                metadataBasic.metadataDetailsList.push(metadata);
            }
            // 置标标签数据
            metadataBasic.labels = layui.datalabel.getData(metadataBasic.id, 'metadata');
            let response;
            await $.ajax({
                url: `${ctx}/business/metadata/save/full`,
                type: 'post',
                data: metadataBasic,
                async: false,
                success: res => response = res,
                error: () => response = {flag: false, msg: '保存失败'}
            })

            return response
        },
        formInitDone: function (data) {
            initDictData(data);
            // 表单初始化完成后，查询数据源列表，并渲染选择数据源下拉框
            $.post(`${ctx}/business/datasource/list`, {}, function (res) {
                $('#dataSourcesSelect').html(`<option value="" selected>请选择数据源</option>`)
                if (res.flag) {
                    for (let source of res.data) {
                        const selected = data && data.dataSourceId == source.id ? 'selected' : '';
                        $('#dataSourcesSelect').append(`<option value="${source.id}" ${selected}>${source.name}</option>`)
                    }
                } else {
                    layer.msg('数据源查询失败');
                }
                form.render('select');
            })

            // 根据数据资源详情表渲染数据资源标签组件
            if (data && data.metadataDetailsList && data.metadataDetailsList.length > 0) {
                data.metadataDetailsList.forEach(meta => {
                    if (meta.columnList) { // 需要选中的列
                        meta.columnList.forEach(col => col.selected = meta.foreignColumn == col.columnName ? 'selected' : '')
                    }
                    // 将数据添加到数据资源map中去，用来渲染组件
                    metadataTableMap.set(meta.tableName, meta)
                });
                renderMetadataTablesTab(data);
            }
            // 渲染置标标签数据
            if(data && data.labels){
                layui.datalabel.setData(data.labels);
            }
        }
    });

    /**
     * 渲染数据资源表标签组件
     */
    const renderMetadataTablesTab = function (initData) {
        //if (metadataTableMap.size == 0) return;
        laytpl($("#checkedTableTemplate").html()).render(metadataTableMap, function (html) {
            $("#metadataTablesTab").html(html);
        })
        form.render();

        // 删除数据资源表标签，只能再渲染完成后再绑定事件监听，否则无效
        $('#metadataTablesTab .table-tab i').click(function (elem) {
            const tableName = $(this).data('id');
            metadataTableMap.delete(tableName);
            renderMetadataTablesTab();
        });

        // 添加数据资源表的下拉框的事件监听
        metadataSelectorEventListener();
        // 设置主表单选按钮的监听
        renderMasterTableRadio();
        // 监听字段配置按钮
        filedConfigBtnListener();
    }


    /**
     * 打开选择数据表的弹窗
     */
    $("#btn_selectTable").click(function () {
        const datasourceId = $('#dataSourcesSelect').val();
        let layerWin;
        if (datasourceId) {
            parent.layer.open({
                id: 'selectTable',
                type: 2,
                area: ['550px', '550px'],
                content: `${ctx}/business/datasource/selectTableLayer?datasourceId=${datasourceId}`,
                btn: ['确定', '取消'],
                success: function (layerObj) {
                    layerWin = parent.window[layerObj.find('iframe')[0]['name']]
                },
                yes: function (index) {
                    layerWin.ok().then((data) => {
                        metadataTableMap = data;
                        /*metadataTableMap.clear()
                        data.forEach((value, key)=>metadataTableMap.set(key, value))*/
                        renderMetadataTablesTab();
                    })
                    parent.layer.close(index);
                }
            });
        } else {
            layer.msg('请先选择数据源');
        }
    });

    /**
     * 数据资源下拉框事件监听
     */
    function metadataSelectorEventListener() {
        for (let [index, item] of metadataTableMap) {
            form.on(`select(selectForeignColumn_${index})`, function (data) {
                let metadata = metadataTableMap.get(index);
                if (metadata) {
                    metadata.foreignColumn = data.value;
                }
            });
            form.on(`select(selectForeignTable_${index})`, function (data) {
                let metadata = metadataTableMap.get(index);
                if (metadata) {
                    metadata.foreignTable = data.value;
                }
            });
        }
    }

    /**
     * 初始化主表单选按钮
     * @param initData
     */
    function renderMasterTableRadio(initData) {
        form.on('radio(master-radio)', obj => metadataTableMap.forEach((value, key) => {
            value.master = key == obj.value;
            form.val('metadataForm', {metadataName: obj.value})
        }));
        // 如果有初始化数据，则需要设置回显单选按钮的状态
        if (initData && initData.metadataDetailsList && initData.metadataDetailsList.length > 0) {
            initData.metadataDetailsList.forEach(meta => {
                if (meta.master) {
                    form.val('metadataForm', {master: meta.tableName})
                }
            })
        } else {
            const firstValue = metadataTableMap.values().next().value;
            let metadataName = '', metadataComment = '';
            if (firstValue) {
                firstValue.master = true;
                metadataName = firstValue.tableName;
                metadataComment = firstValue.tableComment;
            }
            form.val('metadataForm', {master: metadataName, metadataName, metadataComment})
        }
    }

    /**
     * 监听字段配置按钮
     */
    function filedConfigBtnListener() {
        // 设置字段默认全部显示
        metadataTableMap.forEach((tableInfo, key) => {
            if (!tableInfo.tableColumns) {
                const cols = tableInfo.columnList.map(({columnName, comment, dataType}) => {
                    return {columnName, comment, dataType}
                });
                tableInfo.tableColumns = JSON.stringify(cols);
                metadataTableMap.set(key, tableInfo);
            }
        })


        // 绑定按钮事件
        $('a.btn-field-config').on('click', function ({target}) {
            const tableName = $(target).data('table');
            const tableInfo = metadataTableMap.get(tableName);
            openFieldConfigLayer(tableInfo);
        })
    }

    /**
     * 打开字段配置窗口
     * @param tableInfo
     */
    function openFieldConfigLayer(tableInfo) {
        // 设置选中数据
        if (tableInfo.tableColumns) {
            const checkedCols = JSON.parse(tableInfo.tableColumns);
            if (Array.isArray(checkedCols)) {
                for (let item of checkedCols) {
                    for (let col of tableInfo.columnList) {
                        if (item.columnName === col.columnName) {
                            col.LAY_CHECKED = true;
                        }
                    }
                }
            }
        }

        const data = $.extend([], tableInfo.columnList);
        layer.open({
            title: '配置字段',
            type: 1,
            area: ['800px', '480px'],
            btn: ['确定', '取消'],
            content: `<table id="field-config-table" lay-filter="field-config-table"></table>`,
            success: function () {
                table.render({
                    elem: '#field-config-table',
                    data: data,
                    page: false,
                    limit: 999,
                    cols: [[
                        {type: 'checkbox', width: 50},
                        {field: 'columnName', title: '字段名称', width: 300},
                        {field: 'dataType', title: '数据类型', width: 120},
                        {field: 'comment', title: '注释', width: 300},
                    ]]
                });
            },
            yes: function (index) {
                const {data} = table.checkStatus('field-config-table');
                let tableColumns = [];
                for (let {columnName, comment} of data) {
                    tableColumns.push({columnName, comment})
                }

                tableInfo.tableColumns = JSON.stringify(tableColumns);
                metadataTableMap.set(tableInfo.tableName, tableInfo);
                layer.close(index);
            }
        });
    }

    /**
     * 初始化字典项数据
     */
    function initDictData(data) {
        layui.dict.setData('dict-data-type', data);
        layui.dict.setData('dict-data-form', data);
        layui.dict.setData('dict-secret-level', data);
    }

    console.info(new Date().getTime())
    //日期选择器
    laydate.render({
        elem: 'input[name="collectionTime"]',
        type: 'datetime',
        value: new Date(),
        format: "yyyy-MM-dd HH:mm:ss"
    });
});

/**
 * 对父页面暴露save方法
 * @returns {*}
 */
function save() {
    return editPage.submit();
}
