<%@ 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/apply/orderList">
                    <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="traderId" value="${traderId}"/>
            <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">
                        <select name="status">
                            <option value="">请选择</option>
                            <option value="1">待业务员审批</option>
                            <option value="2">待工程部审批</option>
                            <option value="3">待运管部审批</option>
                            <option value="4">审批驳回</option>
                            <option value="5">待匹配</option>
                            <option value="7">已拆单</option>
                            <option value="8">已并单</option>
                            <option value="9">已下单</option>
                        </select>
                    </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="creator" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">入库仓库：</label>
                    <div class="layui-input-block">
                        <select name="inWarehouseId">
                            <option value="">请选择</option>
                            <c:forEach items="${wares}" var="warehouse">
                                <option value="${warehouse.columns.id}">${warehouse.columns.name}</option>
                            </c:forEach>
                        </select>
                    </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>
        </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='DDGL_WLSQDGL_WLSQDGL_XZ_B' html='<button class="layui-btn layui-btn-sm" lay-event="add"><i
            class="layui-icon layui-icon-add-1"></i>新增
    </button>'/>
    <sfTags:pagePermission authFlag='DDGL_WLSQDGL_WLSQDGL_SC_B' html='<button class="layui-btn layui-btn-sm" lay-event="delete"><i
            class="layui-icon layui-icon-delete"></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 bjBtn = ${fns:checkPagePermission(request, 'DDGL_WLSQDGL_WLSQDGL_BJ_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
                });
            }
        });
        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, sort: false, align: 'center', templet: function (d) {
                            if (d.status === '4' && bjBtn) {
                                return "<a href=\"#\" class=\"layui-table-link layui-icon   \" style='font-size:14px;' onclick=\"editMaterialStock('" + d.id + "')\">编辑</a>&nbsp;&nbsp;";
                            }
                            return '- -';
                        }
                    }
                    , {
                        field: 'number',
                        title: '订单编号',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true,
                        templet: function (d) {
                            return "<a href=\"#\" class=\"layui-table-link\" style='font-size:14px;' onclick=\"orderDetail('" + d.id + "')\">" + d.number + "</a>";
                        }
                    }
                    , {
                        field: 'number',
                        title: '订单状态',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true,
                        templet: function (d) {
                            return transStatus(d.status);
                        },
                        expFormat: function (d) {
                            return transStatus(d.status);
                        }
                    }
                    , {
                        field: 'creator',
                        title: '下单人',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true
                    }
                    , {
                        field: 'order_name',
                        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: 'remark',
                        title: '订单备注',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 180
                    }
                    , {
                        field: 'create_time',
                        title: '申请时间',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 150
                    }
                ]]
                , 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 'add':
                    layer.open({
                        type: 2,
                        title: "新增",
                        resize: false,
                        area: ['80%', '70%'],
                        content: '${ctx}/material/apply/addApply'
                    });
                    break;
                case 'delete':
                    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 !== '4') {
                            layer.msg('仅审批驳回的订单可以删除');
                            return;
                        }
                        ids.push(checkStatus.data[i].id);
                    }
                    layer.confirm('确认要删除吗？', {
                        icon: 3,
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            type: 'post',
                            url: '${ctx}/material/apply/deleteApplyOrder',
                            data: {
                                ids: 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 editMaterialStock(id) {
        layer.open({
            type: 2,
            title: "编辑",
            resize: false,
            area: ['80%', '70%'],
            content: '${ctx}/material/apply/addApply?id=' + 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>

