<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>待匹配订单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base"/>
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css"/>
    <style>
        .layui-table-tool-temp {
            padding-right: 0
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <form class="layui-form layui-card" id="searchForm">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <a href="${ctx}/material/order/waitMatchOrder">
                    <li class="layui-this">待匹配订单</li>
                </a>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset">
                    <i class="layui-icon fs-12">&#xe9aa;</i>
                    重置
                </button>
                <button type="button" class="layui-btn f-r mt-10 mr-15 pl-10 pr-10" id="searchBtn" lay-submit
                        lay-filter="LAY-user-back-search">
                    <i class="layui-icon fs-12">&#xe615;</i>
                    查询
                </button>
            </ul>
        </div>
        <div class="layui-card-body layuiadmin-card-header-auto">
            <input type="hidden" name="status" value="5"/>
            <div class="layui-form-item">
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">订单编号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="number" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">产品编号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="projectCode" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">下单人：</label>
                    <div class="layui-input-block">
                        <input type="text" name="traderName" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">订单备注：</label>
                    <div class="layui-input-block">
                        <input type="text" name="remark" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">下单时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeMin" class="layui-input" id="repairTime"
                                   placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeMax" class="layui-input" id="endTime"
                                   placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-col-lg4 layui-col-sm6">
                    <label class="layui-form-label">审批时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="auditTimeMin" class="layui-input" id="orderTimeMin"
                                   placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="auditTimeMax" class="layui-input" id="orderTimeMax"
                                   placeholder="结束日期">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
        </div>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <sfTags:pagePermission authFlag='JXSDDGL_DPPDD_DPPDD_HB_B' html='<button class="layui-btn layui-btn-sm" lay-event="merge"><i
            class="layui-icon layui-icon-add-1"></i>合并订单
    </button>'/>
</script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/xm-select.js"></script>
<script>
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer', 'laypage', 'laydate', 'form'], function () {
        $ = layui.$;
        tableExt = layui.tableExt;
        layer = layui.layer;
        laypage = layui.laypage;
        laydate = layui.laydate;
        form = layui.form;
        table = tableExt.getLayTable();
        //匹配
        var ppBtn = ${fns:checkPagePermission(request, 'JXSDDGL_DPPDD_DPPDD_PP_B') eq '0'};

        //开始日期
        var insStart = laydate.render({
            elem: '#repairTime'
            , type: 'datetime'
            , done: function (value, date) {
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var insEnd = laydate.render({
            elem: '#endTime'
            , type: 'datetime'
            , done: function (value, date) {
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        var orderTimeMin = laydate.render({
            elem: '#orderTimeMin'
            , type: 'datetime'
            , done: function (value, date) {
                //更新结束日期的最小日期
                orderTimeMax.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                orderTimeMax.config.elem[0].focus();
            }
        });
        //结束日期
        var orderTimeMax = laydate.render({
            elem: '#orderTimeMax'
            , type: 'datetime'
            , done: function (value, date) {
                //更新开始日期的最大日期
                orderTimeMin.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        window.searchOrder = function () {
            render();
        };

        render();

        function render() {
            tableExt.render({
                elem: '#test'
                , height: 'full-180'
                , toolbar: '#toolbarDemo'
                , defaultToolbar: false
                , limits: [20, 50, 100]
                , limit: 20
                , page: true //开启分页
                , url: '${ctx}/material/apply/ajaxForApplyOrderList'
                , where: _serializeJson($("#searchForm"))
                , cols: [[
                    {type: 'numbers', fixed: 'left', title: '序号'},
                    {type: 'checkbox', fixed: 'left', title: '选择'}
                    , {
                        field: 'id', title: '操作', minWidth: 100, fixed: 'left', sort: false, align: 'center', templet: function (d) {
                            if (ppBtn && d.status === '5') {
                                return "<a href=\"#\" class=\"layui-table-link layui-icon   \" style='font-size:14px;' onclick=\"match('" + d.id + "')\">匹配</a>";
                            }
                            return '- -';
                        }
                    }
                    , {
                        field: 'number',
                        title: '订单编号',
                        minWidth: 200,
                        sort: false,
                        align: 'center',
                        fixed: 'left',
                        exportable: true,
                        templet: function (d) {
                            return "<a href=\"#\" class=\"layui-table-link \" style='font-size:14px;' onclick=\"orderDetail('" + d.id + "')\">" + d.number + "</a>";
                        }
                    }
                    , {
                        field: 'project_code',
                        title: '产品编号',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true
                    }
                    , {
                        field: 'traderName',
                        title: '下单人',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true
                    }
                    , {
                        field: 'traderName',
                        title: '物料清单',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        templet: function (d) {
                            return "<a href=\"#\" class=\"layui-table-link layui-icon   \" style='font-size:14px;' onclick=\"materialDetail('" + d.id + "')\">查看</a>&nbsp;&nbsp;";
                        }
                    }
                    , {
                        field: 'inWarehouseName',
                        title: '入库仓库',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 150
                    }
                    , {
                        field: 'relate_no',
                        title: '拆单/并单号',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true
                    }
                    , {
                        field: 'remark',
                        title: '订单备注',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 180
                    }
                    , {
                        field: 'create_time',
                        title: '下单时间',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 200
                    }
                    , {
                        field: 'audit_time',
                        title: '审批时间',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 200
                    }
                ]]
                , exportOpt: {                           //导出选项配置
                    ele: '#exportBtn',                   //导出的Dom元素
                    headerTitle: '待匹配订单',             //excel表格的标题
                    pageData: false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                    maxAmount: 5000,                    //当pageData为false生效
                    fileName: '待匹配订单',
                    where: function () {                 //当pageData为false时生效，过滤的条件
                        return _serializeJson($("#searchForm"));
                    }
                }
            });
        }


        function transStatus(status) {
            switch (status) {
                case '1':
                    return '待业务员审批';
                case '2':
                    return '待工程部审批';
                case '3':
                    return '待运管部审批';
                case '4':
                    return '审批驳回';
                case '5':
                    return '待匹配';
                case '7':
                    return '已拆单';
                case '8':
                    return '已并单';
                case '9':
                    return '已下单';
                default:
                    return '';
            }
        }


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'merge': {
                    if (checkStatus.data.length < 1) {
                        layer.msg("请先选择数据", {icon: 5, anim: 6});
                        return;
                    }
                    var ids = [];
                    for (var i = 0; i < checkStatus.data.length; i++) {
                        if (checkStatus.data[i].status !== '5') {
                            layer.msg('仅待匹配的订单可以合并');
                            return;
                        }
                        ids.push(checkStatus.data[i].id);
                    }
                    layer.confirm('确认要合并吗？', {
                        icon: 3,
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            type: 'post',
                            url: '${ctx}/material/order/mergeOrder',
                            data: {
                                orderIds: ids.join()
                            },
                            success: function (data) {
                                if (data.code === '200') {
                                    layer.msg('操作成功', {time: 1000});
                                    render();
                                } else if (data.code === '501') {
                                    layer.msg(data.errMsg, {time: 1000});
                                } else {
                                    layer.msg('操作失败', {time: 1000});
                                }
                            }
                        })
                    });
                }
            }
        });

        //监听行单击事件
        table.on('row(test)', function (obj) {
            var data = obj.data;
            var oldData = table.cache["test"];
            var checkCell = obj.tr.find(".layui-form-checkbox");
            var CHECKED;

        });
        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('test', {
                where: field,
                page: {
                    curr: 1
                }
            });
        });

    });

    /**
     * 审批
     */
    function audit(id) {
        $.ajax({
            type: 'post',
            url: '${ctx}/material/order/canOperate',
            data: {
                orderId: id
            },
            dataType: 'json',
            success: function (data) {
                if (data.code === '200') {
                    if (data.data) {
                        layer.open({
                            type: 2,
                            title: "审批",
                            resize: false,
                            area: ['80%', '70%'],
                            content: '${ctx}/material/order/toAudit?applyId=' + id
                        });
                    } else {
                        layer.msg('当前角色无操作权限');
                    }
                } else {
                    layer.msg('操作失败，请联系管理员');
                }
            }
        });
    }

    /**
     * 匹配
     */
    function match(id) {
        layer.open({
            type: 2,
            title: "匹配",
            resize: false,
            area: ['80%', '70%'],
            content: '${ctx}/material/order/toMatch?applyId=' + id
        });
    }

    function orderDetail(id) {
        layer.open({
            type: 2,
            title: "查看",
            resize: false,
            area: ['80%', '70%'],
            content: '${ctx}/material/apply/materialDetail?applyId=' + id
        });
    }

    function materialDetail(id) {
        layer.open({
            type: 2,
            title: "查看",
            resize: false,
            area: ['80%', '70%'],
            content: '${ctx}/material/apply/materialList?id=' + id
        });
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>

