/**
 * 预约记录列表
 * view/delivery/appointment_record/index.ftl
 */

let el = {
    tb: '#tb',
    searchForm: '#searchForm',
    appointmentCode: '#appointmentCode',
    appointmentId: '#appointmentId',
    urgencyFlag: '#urgencyFlag',
    status: '#status',
    bodyHeight: document.documentElement.clientHeight,
};
$(function () {
    initCombobox();
    loadGrid();
});


function initCombobox() {
    $(el.status).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '请选择', value: ''},
            {label: '确认中', value: '0'},
            {label: '已预约', value: '1'}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto'
    });

    //时间范围
    // $(el.appointmentId).combobox({
    //     valueField: 'id',
    //     textField: 'rangeTime',
    //     url: "/rest/appointmentTime/selector",
    //     editable: false,
    //     prompt: '--- 请选择 ---',
    //     panelHeight: 'auto'
    // });

    //预约类型
    $(el.urgencyFlag).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '请选择', value: ''},
            {label: '是', value: '1'},
            {label: '否', value: '0'},
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto'
    });
}


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/appointmentRecord/rest/list',
        singleSelect: false,
        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: 'urgencyFlag',
                title: '绿色放行',
                width: '65',
                align: 'left',
                formatter: function (data) {
                    switch (Number(data)) {
                        case 0:
                            return "<span>否</span>";
                        case 1:
                            return "<span style='color: darkgreen'>是</span>";
                        default:
                            return "";
                    }
                }
            },
            {
                field: 'appointmentCode',
                title: '预约编码',
                width: '120',
                align: 'left',
            },
            {
                field: 'factoryName',
                title: '工厂名称',
                width: '160',
                align: 'left',
            },
            {
                field: 'dockerName',
                title: '卸货点名称',
                width: '120',
                align: 'left',
            },

            {
                field: 'status',
                title: '预约状态',
                width: '70',
                align: 'left',
                formatter: function (data) {
                    switch (data) {
                        case 0:
                            return "<span style='color: red'>确认中</span>";
                        case 1:
                            return "<span  style='color: darkgreen'>已预约</span>";
                        default:
                            return ""
                    }
                }
            },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: '90',
                align: 'left',
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: '160',
                align: 'left',
            },
            {
                field: 'appointmentDate',
                title: '当前预约时间',
                width: '160',
                align: 'left',
                formatter: function (value, row, index) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD') + " " + row.appointmentStartTime + "-" + row.appointmentEndTime;
                    } else {
                        return value;
                    }
                }
            },
            {
                field: 'lastDate',
                title: '上次预约时间',
                width: '160',
                align: 'left',
                formatter: function (value, row, index) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD') + " " + row.lastStartTime + "-" + row.lastEndTime;
                    } else {
                        return value;
                    }
                }
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: '140',
                align: 'left',
            },
            {
                field: 'updateTime',
                title: '变更操作时间',
                width: '140',
                align: 'left',
            },

        ]],
        toolbar: [{
            text: '变更',
            id: 'btn_change',
            iconCls: 'icon-edit',
            handler: function () {
                changeDate();
            }
        },
            "-", {
                text: '取消',
                id: 'btn_canle',
                iconCls: 'icon-delete',
                handler: function () {
                    delData();
                }
            }, "-", {
                text: '确认放行',
                id: 'btn_confirm',
                iconCls: 'icon-ok',
                handler: function () {
                    var rows = $(el.tb).datagrid('getSelections');
                    if (rows.length !== 1) {
                        layer.msg('请选择一条数据查看!', {icon: 2});
                        return;
                    }
                    topWin.layer.confirm('是否确认绿色放行?', {icon: 3, title: '提示'}, function (iidx) {
                        topWin.layer.load(1);
                        $.post(g.ctx + 'pc/order/appointmentRecord/rest/pass', {id: rows[0].id}, function () {
                            topWin.layer.msg('操作成功');
                            $(el.tb).datagrid('load');
                            topWin.layer.close(iidx);
                        }).error(function (data) {
                            topWin.layer.alert(data.responseJSON.message, {icon: 2});
                            topWin.layer.close(iidx);
                        }).complete(function () {
                            topWin.layer.closeAll('loading');
                        })
                    })
                }
            },
            "-", {
                text: '查看',
                id: 'btn_lookup',
                iconCls: 'icon-import',
                handler: function () {
                    lookUp();
                }
            },
        ],
        onBeforeLoad: function () {
            displayButtonByUserRole("appointmentRecord");
        },
        onLoadSuccess: function () {
            $(el.tb).datagrid('clearSelections');
        }
    });
}


/**
 * 导出
 */
function downLoad() {
    var formData = $(el.searchForm).serializeJson();
    var paramStr = '';
    for (let key in formData) {
        paramStr += "&" + key + "=" + formData[key];
    }
    var url = '/rest/appointmentRecord/exportExcel?' + paramStr;
    window.location.href = url;
}

/**
 * 查看
 */
function lookUp() {
    var rows = $(el.tb).datagrid('getSelections');
    if (rows.length !== 1) {
        layer.msg('请选择一条数据查看!', {icon: 2});
        return;
    }
    var url = g.ctx + "pc/order/appointmentPurchase/lookUp?id=" + rows[0].id;
    var appointmentLookupDialog = openDialog("appointmentLookupDialog", url, {
        title: "查看",
        width: '90%',
        height: '600',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            appointmentLookupDialog.dialog('destroy');
        },
        buttons: [{
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                appointmentLookupDialog.dialog('close');
            }
        }]
    });
}


/**
 * 变更
 */
function changeDate() {
    var rows = $(el.tb).datagrid('getSelections');
    if (rows.length !== 1) {
        topWin.layer.msg('只能选一条数据变更!', {icon: 2});
        return;
    }

    let url = g.ctx + 'pc/order/appointmentRecord/appointment?id=' + rows[0].id
    var appointmentDialog = openDialog("appointmentDialog", url, {
        title: "变更",
        width: '776',
        height: '96%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            appointmentDialog.dialog('destroy');
        },
        buttons: [{
            text: '插队',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['appointmentDialog'].submit(1, function (data) {
                    appointmentDialog.dialog('close');
                    $(el.tb).datagrid('load');
                });
            }
        }, {
            text: '预约',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['appointmentDialog'].submit(0, function (data) {
                    appointmentDialog.dialog('close');
                    $(el.tb).datagrid('load');
                });
            }
        }, {
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                appointmentDialog.dialog('close');
            }
        }]
    });
}


/**
 * 取消
 */
function delData() {
    var rows = $(el.tb).datagrid('getSelections');
    if (rows.length === 0) {
        topWin.layer.msg('请选择数据!');
        return;
    }
    var states = [], ids = [];
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if (row.status !== 1) {
            states.push((i + 1));
        }
        ids.push(row.id);
    }

    if (states.length > 0) {
        topWin.layer.msg('仅已预约的数据可以取消', {icon: 2});
        return;
    }
    topWin.layer.confirm('确定要取消吗?', {icon: 3, title: '提示'}, function (iidx) {
        topWin.layer.load(1);
        $.post(g.ctx + 'pc/order/appointmentRecord/rest/cancle', {ids: ids.join(",")}, function () {
            topWin.layer.msg('操作成功');
            $(el.tb).datagrid('load');
            topWin.layer.close(iidx);
        }).error(function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
            topWin.layer.close(iidx);
        }).complete(function () {
            topWin.layer.closeAll('loading');
        })
    });


}



