<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
    <title>能辉</title>
    <meta name="decorator" content="base" />
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
    <style>
        .layui-card{box-shadow: none;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form layui-card">
        <div class="layui-tab  layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="CKGL_DBDGL_DBDGL_T" html='<li class="layui-this sf-tab"><a href="${ctx}/allocation/allocationPage">调拨单管理</a></li>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset"  onclick="reset()" >
                    <i class="layui-icon fs-12">&#xe9aa;</i>
                    重置
                </button>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " lay-submit lay-filter="LAY-user-back-search" data-type="reload">
                    <i class="layui-icon fs-12">&#xe615;</i>
                    查询
                </button>
            </ul>
        </div>
        <div class="layui-card-body" id="searchForm">
            <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"  autocomplete="off" 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>
                        </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="remarks"  autocomplete="off" 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="outName"  autocomplete="off" 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="inName"  autocomplete="off" 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="warehouseId" lay-search >
                            <option value="">请选择</option>
                            <c:forEach items="${warehouses}" 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">
                        <select name="inWarehouseId" lay-search >
                            <option value="">请选择</option>
                            <c:forEach items="${warehouses}" var="warehouse">
                                <option value="${warehouse.columns.id}">${warehouse.columns.name}</option>
                            </c:forEach>
                        </select>
                    </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="createTimeSta" id="createTimeSta" class="layui-input"  placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeEnd" id="createTimeEnd" class="layui-input"  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="outTimeSta" id="confirmTimeSta" class="layui-input"  placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="outTimeEnd" id="confirmTimeEnd" class="layui-input"  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="inTimeSta" id="examineTimeSta" class="layui-input"  placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="inTimeEnd" id="examineTimeEnd" class="layui-input"  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="auditTimeSta" id="finishTimeSta" class="layui-input"  placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="auditTimeEnd" id="finishTimeEnd" class="layui-input"  placeholder="结束日期">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class='layui-card'>
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test-table-reload" lay-filter="table-toolbar"></table>
                <script type="text/html" id="toolbarDemo">
                    <c:if test="${userType eq 1}">
                        <sfTags:flowableAddPermission formId="${formId}" html='<button class="layui-btn layui-btn-sm" id="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
                           <%-- <sfTags:pagePermission authFlag="CKGL_DBDGL_SC_T" html='<button class="layui-btn layui-btn-sm" id="delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>'/>--%>

                    </c:if>
                          <sfTags:pagePermission authFlag="CKGL_DBDGL_CK_T" html='<button class="layui-btn layui-btn-sm" id="export" lay-event="export"><i class="layui-icon layui-icon-export"></i>导出</button>'/>
                </script>


        </div>
    </div>

</div>


<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/html" id="table-toolbar">

        {{# if(d.order_status == '4'){ }}
        <sfTags:flowableAddPermission formId="${formId}" html='<a class="layui-table-opt-link" lay-event="edit">编辑</a>'/>
        {{# } }}


    {{# if(d.order_status == '3' && d.audit == '1'){ }}
        <a class="layui-table-opt-link"lay-event="examine">审批</a>
    {{# }else if(d.order_status == '1' && d.out_ascription_id == '${ascription.ascriptionId}'){ }}
    <sfTags:pagePermission authFlag="CKGL_DBDGL_CK_T" html='<a class="layui-table-opt-link" lay-event="outWarehouse">出库</a>'/>
    {{# }else if(d.order_status == '2' && d.in_ascription_id == '${ascription.ascriptionId}'){ }}
    <sfTags:pagePermission authFlag="CKGL_DBDGL_RK_T" html='<a class="layui-table-opt-link" lay-event="inWarehouse">入库</a>'/>
    {{# } }}

</script>

<script type="text/javascript">
    var table;
    var $;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laydate','form'], function(){
        var tableExt = layui.tableExt;
        var layer = layui.layer;
        $ = layui.jquery;
        var laydate = layui.laydate;
        var  form = layui.form;

        table = tableExt.getLayTable();

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

        var insEnd = laydate.render({
            elem: '#createTimeEnd'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        //确认时间
        var confirmStart = laydate.render({
            elem: '#confirmTimeSta'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                confirmEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                confirmEnd.config.elem[0].focus();
            }
        });

        var confirmEnd = laydate.render({
            elem: '#confirmTimeEnd'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                confirmStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //开始检验时间
        var examineStart = laydate.render({
            elem: '#examineTimeSta'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                examineEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                examineEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var examineEnd = laydate.render({
            elem: '#examineTimeEnd'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                examineStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        //完成时间
        var finishStart = laydate.render({
            elem: '#finishTimeSta'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                finishEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                finishEnd.config.elem[0].focus();
            }
        });

        var finishEnd = laydate.render({
            elem: '#finishTimeEnd'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                finishStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        tableExt.render({
            elem: '#test-table-reload'
            ,toolbar : '#toolbarDemo'
            , height: 'full-218'
            ,defaultToolbar : false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            ,url: '${ctx}/allocation/allocationOrderList'
            ,cols: [[
                {type: 'numbers', title: '序号' ,fixed: 'left'}
                ,{field : 'id', checkbox : true,fixed: 'left' }
                ,{field:'id', title: '操作',toolbar: '#table-toolbar',align: 'center',width: 90,fixed: 'left' }
                ,{field:'code', title: '调拨单号',align: 'center',width: 180,fixed: 'left'
                    ,templet:function(d){
                        return '<a href="#" class="layui-table-link" onclick="orderDetail(\''+d.id+'\')">'+d.code+'</a>'
                    }
                }
                ,{field:'status', title: '调拨状态',align: 'center' ,width: 150
                    ,templet:function(d){
                        if(d.order_status=='1'){
                            return "待调出方出库";
                        }else if(d.order_status=='2'){
                            return "待调入方入库";
                        }else if(d.order_status=='3'){
                            return "审批中";
                        }else if(d.order_status=='4'){
                            return "审核不通过";
                        }else if(d.order_status=='5'){
                            return "已完成";
                        }else{
                            return "";
                        }
                    }
                }

                ,{field:'out_name', title: '调出方',align: 'center',width: 200}
                ,{field:'warehouse_name', title: '调出仓库',align: 'center',width: 180}
                ,{field:'in_name', title: '调入方',align: 'center',width: 200}
                ,{field:'in_warehouse_name', title: '调入仓库',align: 'center',width: 180}
                ,{field:'address', title: '物料清单',align: 'center',width: 90
                    ,templet:function(d){
                        return '<a href="#" class="layui-table-link" onclick="materialDetail(\''+d.id+'\')">查看</a>';
                    }
                }
                ,{field:'remarks', title: '调拨备注',align: 'center',width:200}
                ,{field:'create_time', title: '调拨时间',align: 'center',width: 180}
                ,{field:'out_time', title: '出库时间',align: 'center',width: 180}
                ,{field:'in_time', title: '入库时间',align: 'center',width: 180}
                ,{field:'audit_time', title: '审核时间',align: 'center',width: 180}
            ]]
            ,done: function(res, curr, count){

            },exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '员工列表',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '员工导出',
                where: function (){                 //当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        table.on('toolbar(table-toolbar)',function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            switch (obj.event){
                case 'delete':
                    if (data.length < 1) {
                        layer.msg("请至少选择一条数据",{icon:5,anim:6});
                        return;
                    }
                    var orderId="";
                    for(var i=0;i<data.length;i++){
                        if(data[i].status != 4){
                            layer.msg("您选择的采购单中有不可操作删除的订单，请重新选择！",{icon:5,anim:6});
                            return;
                        }
                        orderId+=data[i].id+",";
                    }
                    var indexla = layer.confirm('您确定要删除调拨单吗？', {
                        icon: 3,
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            type: "POST",
                            url: "${ctx}/allocation/deleteAllocationOrder",
                            data: {
                                orderIds: orderId
                            },
                            success: function (result) {
                                if (result.code == "200") {
                                    layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                                        reload();
                                    });
                                } else if (result.code == "501") {
                                    layer.msg(result.errMsg , {icon: 2})
                                }else{
                                    layer.msg('操作失败！', {icon: 2})
                                }
                            }
                        });

                    }, function () {
                        layer.close(indexla)
                    });
                    break;
                case 'deleteSys':
                    if (data.length != 1) {
                        layer.msg("请至少选择一条数据",{icon:5,anim:6});
                        return;
                    }
                    var orderId="";

                        orderId+=data[0].id;

                    var indexla = layer.confirm('您确定要删除调拨单吗？', {
                        icon: 3,
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            type: "POST",
                            url: "${ctx}/allocation/deleteAllocationOrder",
                            data: {
                                orderIds: orderId,
                                type:'system'
                            },
                            success: function (result) {
                                if (result.code == "200") {
                                    layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                                        reload();
                                    });
                                } else if (result.code == "501") {
                                    layer.msg(result.errMsg , {icon: 2})
                                }else{
                                    layer.msg('操作失败！', {icon: 2})
                                }
                            }
                        });

                    }, function () {
                        layer.close(indexla)
                    });
                    break;
                case 'export':
                    location.href='${ctx}/allocation/exportAllocationOrder?name=调拨单&' + $('#searchForm').serialize();
                    break;
            }
        })

        //监听行工具事件
        table.on('tool(table-toolbar)', function(obj){
            var data = obj.data;
            var id = data.id;
            if(obj.event === 'inWarehouse'){
                layer.open({
                    type: 2,
                    title: '入库',
                    content: '${ctx}/allocation/inWarehousePage?id=' + id,
                    area: ['80%','80%']
                });
            }else if(obj.event === 'outWarehouse'){
                layer.open({
                    type: 2,
                    title: '发货',
                    content: '${ctx}/allocation/outWarehousePage?id=' + id,
                    area: ['80%','80%']
                });
            }else if(obj.event === 'examine'){
                layer.open({
                    title:'审批',
                    type: 2,
                    area: ['80%', '75%'],
                    fixed: false,
                    content: '${ctx}/allocation/examineAllocationOrderPage?model=audit&id='+id,
                    success: function (layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                })
            }else if(obj.event === 'edit'){
                layer.open({
                    title:'编辑',
                    type: 2,
                    area: ['80%', '80%'],
                    fixed: false,
                    content: '${ctx}/allocation/addAllocationOrderPage?model=edit&id='+id,
                    success: function (layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                })
            }
        });

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

        $("#add").on('click', function(){
            layer.open({
                title:'新增',
                type: 2,
                area: ['80%', '80%'],
                fixed: false,
                content: '${ctx}/allocation/addAllocationOrderPage?model=add',
                success: function (layero, index) {
                    childWin = window[layero.find('iframe')[0]['name']];
                }
            })
        });

    });

    window.importMaterial = function (materials) {
        childWin.importMaterial(materials);
    };

    function orderDetail(orderId){
        layer.open({
            type: 2,
            title: '详情',
            content: '${ctx}/allocation/allcoationOrderDetail?id=' + orderId,
            area: ['80%','80%']
        });
    }

    function materialDetail(orderId){
        layer.open({
            type: 2,
            title: '查看',
            content: '${ctx}/allocation/allocationMaterialList?id=' + orderId,
            area: ['80%','80%']
        });
    }

    function reload(){
        table.reload('test-table-reload', {
            page:{
                curr:1
            }
        });
    }

    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
    }

</script>
</body>
</html>

