/**
 * 工厂码头配置页面
 * view/delivery/appointment_dayoff/index.ftl
 */

let el = {
    tb: '#tb',
    factory: "#factory",
    docker: "#docker",
    searchForm: '#searchForm',
    bodyHeight: document.documentElement.clientHeight,
};


$(function () {
    $(el.docker).combogrid({
        panelWidth: 450,
        idField: 'dockerCode',
        textField: 'dockerName',
        prompt: '-- 请选择 --',
        editable: false,
        url: g.ctx+'pc/order/appointmentDocker/rest/listAll',
        columns: [[
            {field: 'dockerCode', title: '卸货点编码', width: 60},
            {field: 'dockerName', title: '卸货点名称', width: 100},
            {field: 'factoryCode', title: '工厂编码', width: 100},
            {field: 'factoryName', title: '工厂名称', width: 200}
        ]],
        onSelect: function (value) {
            var g = $(el.docker).combogrid('grid');
            var rows = g.datagrid('getRows');
            var r = rows[value];
            $(el.factory).val(r.factory);
        }
    });

    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/appointmentDocker/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: 'factoryCode',
                title: '工厂编码',
                width: '100',
                align: 'left',
            },
            {
                field: 'factoryName',
                title: '工厂名称',
                width: '100',
                align: 'left',
            },
            {
                field: 'dockerCode',
                title: '卸货点编码',
                width: '100',
                align: 'left',
            },
            {
                field: 'dockerName',
                title: '卸货点名称',
                width: '100',
                align: 'left',
            },
            {
                field: 'urgentNum',
                title: '绿色放行值',
                width: '100',
                align: 'left',
            }
        ]],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    addOrEdit(1);
                }
            },
            "-", {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(2);
                }
            },
            "-", {
                text: '删除',
                iconCls: 'icon-delete',
                handler: function () {
                    delData();
                }
            },
        ],
        onLoadSuccess: function () {
            $(el.tb).datagrid('clearSelections');
        }
    });
}

/**
 * 新增、编辑、查看
 * @param type 1.新增 2.编辑 3.查看
 */
function addOrEdit(type) {
    let title, url, btn, btn1Fc, row;
    switch (type) {
        case 1:
            title = "新增";
            url = g.ctx+'pc/order/appointmentDocker/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/appointmentDocker/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/appointmentDocker/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 delData() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg('请选择编辑的数据!');
        return;
    }

    parent.layer.load(1);
    $.post(g.ctx+'pc/order/appointmentDocker/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');
    })

}

