/**
 * 对账模块-调整对账模块相关表格列的顺序或显示隐藏JS
 *
 * Created by li.kui on 2020/9/10.
 */
'use strict';
var el = {
    tbTableColumn: '#tbTableColumn',
    tbCurrentLayout: '#tbCurrentLayout',
    userId: '#userId',
    pageTable: '#pageTable',
    dataDiv: '#dataDiv',
    columnLayout: '#columnLayout',
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    var pageTable = $(el.pageTable).val();

    initColumnLayout(pageTable);

    initTableColumnDataGird(pageTable);

    initSettingDataGird(pageTable);
});


/**
 * 初始化列表布局下拉框
 */
function initColumnLayout(pageTable) {
    $(el.columnLayout).combobox({
        url: g.ctx + 'fin/table/column/rest/all/setting?pageTable=' + pageTable,
        panelHeight: 300,
        valueField: 'userSettingDesc',
        textField: 'userSettingDesc',
        prompt: '--- 请选择 ---',
        onSelect: function (data) {
            var queryParams = $(el.tbCurrentLayout).datagrid('options').queryParams;
            queryParams.userSettingDesc = data.userSettingDesc;
            queryParams.pageTable = pageTable;
            $(el.tbCurrentLayout).datagrid('load');
        }
    });
}


/**
 * 初始化表格所有列
 */
function initTableColumnDataGird(pageTable) {
    var dataDivHeight = $(el.dataDiv).outerHeight() + 20;
    $(el.tbTableColumn).datagrid({
        loadMsg: '数据加载中......',
        title: '表格所有列',
        url: g.ctx + 'fin/table/column/rest/list',
        height: el.bodyHeight - dataDivHeight,
        singleSelect: false,
        rownumbers: true,
        pagination: false,
        queryParams: {pageTable: pageTable},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'columnField',
                    title: '表格列字段值',
                    width: 160
                },
                {
                    field: 'columnTitle',
                    title: '表格列名',
                    width: 200
                }
            ]
        ],
        toolbar: [
            {
                text: '添加至布局',
                iconCls: 'icon-add',
                handler: function () {
                    addToCurrentLayout();
                }
            }
        ]
    });
}


/**
 * 初始化配置表格
 */
function initSettingDataGird(pageTable) {
    var dataDivHeight = $(el.dataDiv).outerHeight() + 20;
    $(el.tbCurrentLayout).datagrid({
        loadMsg: '数据加载中......',
        title: '布局设置',
        url: g.ctx + 'fin/table/column/rest/user/list',
        height: el.bodyHeight - dataDivHeight,
        singleSelect: false,
        rownumbers: true,
        pagination: false,
        queryParams: {
            pageTable: pageTable,
            userSettingDesc: ''
        },
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'columnField',
                    title: '表格列字段值',
                    width: 160
                },
                {
                    field: 'columnTitle',
                    title: '表格列名',
                    width: 200
                }
            ]
        ],
        toolbar: [
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    removeColumn();
                }
            },
            '-',
            {
                text: '置顶',
                iconCls: 'icon-move-top',
                handler: function () {
                    topColumn();
                }
            },
            '-',
            {
                text: '向上',
                iconCls: 'icon-move-up',
                handler: function () {
                    moveUp();
                }
            },
            '-',
            {
                text: '向下',
                iconCls: 'icon-move-down',
                handler: function () {
                    moveDown();
                }
            },
            '-',
            {
                text: '置底',
                iconCls: 'icon-move-bottom',
                handler: function () {
                    bottomColumn();
                }
            }
        ],
        onLoadSuccess: function (data) {
            var rows = data.rows;

            $(el.tbTableColumn).datagrid('clearSelections');
            var tcRows = $(el.tbTableColumn).datagrid('getRows');

            for(var j = 0; j < rows.length; j++){
                for(var i = 0; i < tcRows.length; i++){
                    var row = tcRows[i];
                    if(rows[j].columnField === row.columnField){
                        var rowIndex = $(el.tbTableColumn).datagrid('getRowIndex', row);
                        $(el.tbTableColumn).datagrid('checkRow', rowIndex);
                        break;
                    }
                }
            }
        }
    });
}


/**
 * 添加列至布局
 */
function addToCurrentLayout() {
    var rows = $(el.tbTableColumn).datagrid('getSelections'), clRows = $(el.tbCurrentLayout).datagrid('getRows');
    if(rows.length === 0){
        topWin.layer.msg('请选择需要添加至布局的列');
        return false;
    }

    for(var i = 0; i < rows.length; i++){
        var row = rows[i], rowIndex = $(el.tbTableColumn).datagrid('getRowIndex', row);
        var existsFlag = false;
        for(var j = 0; j < clRows.length; j++){
            if(clRows[j].columnField === row.columnField){
                existsFlag = true;
            }
        }

        if(!existsFlag){
            $(el.tbTableColumn).datagrid('checkRow', rowIndex);
            var item = {};
            item.id = 0;
            item.columnField = row.columnField;
            item.columnTitle = row.columnTitle;
            item.pageTable = row.pageTable;
            $(el.tbCurrentLayout).datagrid('appendRow', item);
        }
    }
}


/**
 * 删除布局中列
 */
function removeColumn() {
    var rows = $(el.tbCurrentLayout).datagrid('getSelections'), tcRows = $(el.tbTableColumn).datagrid('getRows');
    if(rows.length === 0){
        topWin.layer.msg('请选择需要删除的列');
        return false;
    }

    for(var i = 0; i < rows.length; i++){
        var row = rows[i], rowIndex = $(el.tbCurrentLayout).datagrid('getRowIndex', row);

        for(var j = 0; j < tcRows.length; j++){
            var tcRowIndex = $(el.tbTableColumn).datagrid('getRowIndex', tcRows[j]);
            if(tcRows[j].columnField === row.columnField){
                $(el.tbTableColumn).datagrid('uncheckRow', tcRowIndex);
                break;
            }
        }

        $(el.tbCurrentLayout).datagrid('deleteRow', rowIndex);
    }
}


/**
 * 置顶列
 */
function topColumn() {
    var rows = $(el.tbCurrentLayout).datagrid('getSelections');
    if(rows.length === 0){
        topWin.layer.msg('请选择需要置顶的列');
        return false;
    }

    for(var i = rows.length - 1; i >= 0; i--){
        var row = rows[i], rowIndex = $(el.tbCurrentLayout).datagrid('getRowIndex', row);
        $(el.tbCurrentLayout).datagrid('deleteRow', rowIndex);

        $(el.tbCurrentLayout).datagrid('insertRow', {
            index: 0,
            row: row
        });
    }
}


/**
 * 向上移动
 */
function moveUp() {
    var rows = $(el.tbCurrentLayout).datagrid('getSelections');
    if(rows.length === 0){
        topWin.layer.msg('请选择需要向上移动的列');
        return false;
    }

    var firstRowIndex = $(el.tbCurrentLayout).datagrid('getRowIndex', rows[0]);
    if(firstRowIndex === 0){
        return false;
    }

    for(var i = rows.length - 1; i >= 0; i--){
        var row = rows[i], rowIndex = $(el.tbCurrentLayout).datagrid('getRowIndex', row);
        $(el.tbCurrentLayout).datagrid('deleteRow', rowIndex);

        $(el.tbCurrentLayout).datagrid('insertRow', {
            index: firstRowIndex - 1,
            row: row
        });
    }
}


/**
 * 向下移动
 */
function moveDown() {
    var rows = $(el.tbCurrentLayout).datagrid('getSelections'), all = $(el.tbCurrentLayout).datagrid('getRows');
    if(rows.length === 0){
        topWin.layer.msg('请选择需要向下移动的列');
        return false;
    }

    var lastRowIndex = $(el.tbCurrentLayout).datagrid('getRowIndex', rows[rows.length - 1]);
    if(lastRowIndex === all.length - 1){
        return false;
    }

    for(var i = rows.length - 1; i >= 0; i--){
        var row = rows[i], rowIndex = $(el.tbCurrentLayout).datagrid('getRowIndex', row);
        $(el.tbCurrentLayout).datagrid('deleteRow', rowIndex);

        $(el.tbCurrentLayout).datagrid('insertRow', {
            index: lastRowIndex + 1,
            row: row
        });
    }
}


/**
 * 置底部
 */
function bottomColumn() {
    var rows = $(el.tbCurrentLayout).datagrid('getSelections'), all = $(el.tbCurrentLayout).datagrid('getRows');
    if(rows.length === 0){
        topWin.layer.msg('请选择需要置底的列');
        return false;
    }

    for(var i = 0; i < rows.length; i++){
        var row = rows[i], rowIndex = $(el.tbCurrentLayout).datagrid('getRowIndex', row);
        $(el.tbCurrentLayout).datagrid('deleteRow', rowIndex);

        $(el.tbCurrentLayout).datagrid('insertRow', {
            index: all.length,
            row: row
        });
    }
}


/**
 * 确定布局
 *
 * @param callback 回调函数
 */
function confirmSetUp(callback) {
    var rows = $(el.tbCurrentLayout).datagrid('getRows');
    if(rows.length === 0){
        topWin.layer.msg('请添加需要显示的列');
        return false;
    }

    callback(rows);
}


/**
 * 确定并保存设置的布局
 *
 * @param callback 回调函数
 */
function confirmAndSaveSetUp(callback) {
    var rows = $(el.tbCurrentLayout).datagrid('getRows');
    if(rows.length === 0){
        topWin.layer.msg('请添加需要显示的列');
        return false;
    }

    var data = [], pageTable = $(el.pageTable).val(), userId = $(el.userId).val();
    for(var i = 0; i < rows.length; i++){
        var row = rows[i], item = {};
        item.id = 0;
        item.pageTable = pageTable;
        item.columnField = row.columnField;
        item.columnTitle = row.columnTitle;
        item.columnSort = Number(i + 1);
        item.userId = userId;
        item.userCode = '';
        item.userSettingDesc = '';

        data.push(item);
    }

    topWin.layer.prompt({title: '请填写布局描述', formType: 0, maxlength: 255}, function (text, index) {
        if (!text) {
            topWin.layer.msg('请填写布局描述', {icon: 0});
            return false;
        }

        topWin.layer.close(index);

        var loading = topWin.layer.msg('数据保存中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            type: 'POST',
            url: g.ctx + 'fin/table/column/rest/save/setting',
            data: {
                userData: JSON.stringify(data),
                userSettingDesc: text
            },
            success: function () {
                topWin.layer.close(loading);
                callback(rows);
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}


/**
 * 数据保存
 *
 * @param userSettingDesc 用户配置描述
 * @param optType 操作类型 1:新增 2:编辑
 */
function dataSave(userSettingDesc, optType) {

}