/**
 * 预约到货法定节假日配置表
 * view/delivery/appointment_dayoff/index.ftl
 */

let el = {
    tb: '#tb',
    year: "#year",
    yearMonth: "#yearMonth",
    day: "#day",
    searchForm: '#searchForm',
    bodyHeight: document.documentElement.clientHeight,
    status: "#status"
};


$(function () {
    laydate.render({
        elem: el.year
        , type: 'year',
    });

    laydate.render({
        elem: el.yearMonth
        , type: 'month'
    });

    laydate.render({
        elem: el.day
    });

    loadGrid();
});

function loadGrid() {
    let searchHeight = $(el.searchForm).outerHeight();
    let pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        height: el.bodyHeight - searchHeight,
        width: gridwidth,
        url: g.ctx+'pc/order/appointmentDayOff/rest/list',
        singleSelect: true,
        pagination: true,
        idField: '',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        striped: true,
        page: 1,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        queryParams: {},
        columns: [[
            {
                field: 'ck',
                checkbox: 'true'
            },
            {
                field: 'day',
                title: '日期',
                width: '100',
                align: 'left',
            },
            {
                field: 'type',
                title: '类型',
                width: '100',
                align: 'left',
                formatter: function (value) {
                    if (value === 1) {
                        return '法定节假日'
                    }
                    return '周末';
                }
            },

            {
                field: 'createTime',
                title: '创建时间',
                width: '160',
                align: 'left',
            },
            {
                field: 'updateTime',
                title: '更新时间',
                width: '160',
                align: 'left',
            },

        ]],
        toolbar: [
            {
                text: '新增',
                id:'add',
                iconCls: 'icon-add',
                handler: function () {
                    addOrEdit(1);
                }
            },
            "-", {
                text: '编辑',
                id:'edit',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(2);
                }
            },
            "-", {
                text: '删除',
                id:'delete',
                iconCls: 'icon-delete',
                handler: function () {
                    delData();
                }
            },
            // "-", {
            //     text: '启用|禁用',
            //     iconCls: 'icon-import',
            //     handler: function () {
            //         updateStatus();
            //     }
            // },
        ],
        onLoadSuccess: function () {
            displayButtonByUserRole("appointmentDayOff");
            $(el.tb).datagrid('clearSelections');
        }
    });
}

/**
 * 更新数据状态 0 否 1 是
 */
function updateStatus() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg('请选择查看的数据!');
        return;
    }

    $.post(g.ctx+'pc/order/appointmentSupplier/rest/updateStatus', {id: row.id}, function () {
        $(el.tb).datagrid('load');
        parent.layer.msg('保存成功');
    }).error(function (data) {
        let msg = JSON.parse(data['responseText'])['message'];
        layer.msg('保存失败！ ' + msg);
    }).complete(function () {
        parent.layer.closeAll('loading');
    })
}

/**
 * 新增、编辑、查看
 * @param type 1.新增 2.编辑 3.查看
 */
function addOrEdit(type) {
    let title, url, btn, btn1Fc, row;
    switch (type) {
        case 1:
            title = "新增";
            url = g.ctx+'pc/order/appointmentDayOff/detail?type=' + type;
            btn = ['保存', '关闭'];
            btn1Fc = function (index, layero) {
                let iframe = parent.window[layero.find('iframe')[0]['name']];
                iframe.submitForm(function () {
                    parent.layer.close(index);
                    $(el.tb).datagrid('load');
                });
            };
            break;
        case 2:
            title = '编辑';
            row = $(el.tb).datagrid('getSelected');
            if (!row) {
                layer.msg('请选择编辑的数据!');
                return;
            }
            url = g.ctx+'pc/order/appointmentDayOff/detail?id=' + row['id'] + '&type=' + type;
            btn = ['保存', '关闭'];
            btn1Fc = function (index, layero) {
                let iframe = parent.window[layero.find('iframe')[0]['name']];
                iframe.submitForm(function () {
                    parent.layer.close(index);
                    $(el.tb).datagrid('load');
                });
            };
            break;
        case 3:
            title = '查看';
            row = $(el.tb).datagrid('getSelected');
            if (!row) {
                layer.msg('请选择查看的数据!');
                return;
            }
            url = g.ctx+'pc/order/appointmentDayOff/detail?id=' + row['id'];
            btn = ['关闭'];
            btn1Fc = function (index) {
                parent.layer.close(index)
            };
            break;
    }

    parent.layer.open({
        title: title,
        skin: 'layui-layer-molv',
        type: 2,
        area: ['50%', '80%'],
        content: url,
        btn: btn,
        yes: btn1Fc
    })
}

/**
 * 导入主数据
 */
function importData() {
    var url = '/appointmentDayoff/data/imp';
    el.dataImportDialog = openDialog('dataImp', url, {
        title: '数据导入',
        width: 500,
        height: 600,
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.dataImportDialog.dialog('destroy');
        },
        buttons: [{
            text: handlei18n.dr,
            handler: function () {
                topWin.frames['dataImp'].submitExcel(function (data) {
                    data = JSON.parse(data);
                    if (data.code  === 200 ) {
                        topWin.layer.msg('导入成功', {icon: 1});
                        $(el.tb).datagrid('load');
                        el.dataImportDialog.dialog('close');
                    } else {
                        topWin.layer.alert(data.message, {icon: 2});
                    }
                });
            }
        }, {
            text: itablei8n.close,
            handler: function () {
                el.dataImportDialog.dialog('close');
            }
        }]
    });
}


/**
 * 删除
 */
function delData() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg('请选择编辑的数据!');
        return;
    }

    parent.layer.load(1);
    $.post(g.ctx+'pc/order/appointmentDayOff/rest/del', {id: row.id}, function () {
        parent.layer.msg('操作成功');
        $(el.tb).datagrid('load');
    }).error(function (data) {
        let msg = JSON.parse(data['responseText'])['message'];
        layer.msg('保存失败！ ' + msg);
    }).complete(function () {
        parent.layer.closeAll('loading');
    })

}

