<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <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" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card layui-form">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="CKGL_PDDGL_PDDGL_T"
                                       html='<li class="layui-this"><a href="${ctx}/inventory/toInventoryPage">盘点单管理</a></li>'/>
                <button type="reset" class="layui-btn f-r mt-10 mr-15 pl-10 pr-10" type="reset" onclick="reset()">
                    <i class="layui-icon">&#xe666;</i>重置
                </button>
                <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">
            <form class="layui-form layui-card" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">盘点单号：</label>
                        <div class="layui-input-block">
                            <input type="text" name="code" class="layui-input" id="code" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">盘点状态：</label>
                        <div class="layui-input-block">
                            <select name="status">
                                <option value="">请选择</option>
                                <option value="4">审批驳回</option>
                                <option value="3">已完成</option>
                            </select>
                        </div>
                    </div>
                    <c:if test="${userType eq '1' || userType eq '4'}">
                        <div class="layui-col-md2 layui-col-sm3">
                            <label class="layui-form-label">盘点对象：</label>
                            <div class="layui-input-block">
                                <select name="inventoryType">
                                    <option value="">请选择</option>
                                    <option value="1">供应链</option>
                                    <option value="3">供应商</option>
                                    <option value="2">代理商</option>
                                </select>
                            </div>
                        </div>
                    </c:if>

                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">盘点仓库：</label>
                        <div class="layui-input-block">
                            <select name="warehouseId" lay-search>
                                <option value="">请选择</option>
                                <c:forEach var="warehouse" items="${warehouseList}">
                                    <option value="${warehouse.columns.id}">${warehouse.columns.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">盘点人：</label>
                        <div class="layui-input-block">
                            <input type="text" name="inventoryName" class="layui-input" id="inventoryName"
                                   autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">盘点备注：</label>
                        <div class="layui-input-block">
                            <input type="text" name="remarks" class="layui-input" id="remarks" autocomplete="off">
                        </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="createTimeMin"
                                       placeholder="开始日期" value="" autocomplete="off">
                            </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="createTimeMax"
                                       placeholder="结束日期" value="" autocomplete="off">
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <div class="layui-card-body">
            <table class="layui-hide " id="test" lay-filter="test"></table>
        </div>

    </div>
</div>
<div id="toolbarDemo" class="layui-form-item layui-hide">
    <c:if test="${userType eq '1' || userType eq '4'}">
        <sfTags:flowableAddPermission formId="${formId}"
                               html='<button class="layui-btn layui-btn-sm" id="addBtn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
        <sfTags:pagePermission authFlag="CKGL_PDDGL_PDDGL_DELETE_B"
                               html='<button class="layui-btn layui-btn-sm" id="deleteBtn" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>'/>
    </c:if>
    <sfTags:pagePermission authFlag="CKGL_PDDGL_PDDGL_DAOCHU_B" html='<button class="layui-btn layui-btn-sm" lay-event="export" id="exportBtn"><i class="layui-icon layui-icon-export"></i>导出列表</button>'/>


</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/lib/formSelects-v4.js"></script>
<script type="text/javascript">
   var entrycount=0;
    //初始数据
    var form;
    var $;
    var table;
    var tableExt;
    var laydate;
    var detail = '${fns:checkPagePermission(request, "CKGL_PDDGL_PDDGL_VIEW_B")}';
    var editBtn = '${fns:checkPagePermission(request, "CKGL_PDDGL_PDDGL_EDIT_B")}';
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer', 'laypage', 'laydate', 'form'], function () {
        $ = layui.$;
        form = layui.form;
        tableExt = layui.tableExt;
        table = layui.table;
        laydate = layui.laydate;
        table = tableExt.getLayTable();


        //方案信息列表
        tableExt.render({
            elem: '#test'
            , height: 'full-170'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/inventory/ajaxForInventoryOrderData'
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , page: true
            , toolbar: "#toolbarDemo"
            , method: "post"
            , limits: [20, 50, 100]
            , limit: 20
            , cols: [[
                {field: 'number', minWidth: 50, title: '序号', align: 'center', type: 'numbers', fixed: 'left'}
                , {field: 'id', minWidth: '4%', checkbox: true, align: 'center', fixed: 'left',}
                <c:if test="${userType eq '1' || userType eq '4'}">
                , {
                    field: 'id', title: '操作', minWidth: 150, align: 'center', fixed: 'left'
                    , templet: function (d) {
                        var html = "";
                        if (d.status === '1' || d.status == '2') { // 待审批
                            var fg=d.audit;
                            if("1"==fg){
                                html+= '<a class="layui-table-opt-link" onclick="toAudit(\'' + d.id + '\')">审批</a>';
                            }
                        }else if (d.status === '0' && d.inventory_id == '${userId}') {// 审批驳回
                            html += '<a href="#" class="layui-table-link" onclick="editOrder(\'' + d.id + '\')">编辑</a>&nbsp;';
                        }
                        return html;
                    }
                }
                </c:if>
                , {field: 'code', title: '盘点单号', minWidth: 180, align: 'center',fixed: 'left',exportable: true
                    , templet: function (d) {
                        return '<a href="#" class="layui-table-link" onclick="siteDetail(\''+d.id+'\')">'+d.code+'</a>'
                    }
                }
                , {
                    field: 'status', title: '盘点状态', minWidth: 150, align: 'center', exportable: true
                    , templet: function (d) {
                        if (d.status === '1') {
                            return d.taskName;
                        } else if (d.status === '2') {
                            return '驳回';
                        } else if (d.status === '3') {
                            return '已完成';
                        } else if (d.status === '4') {
                            return '取消';
                        }else if (d.status === '0') {
                            return "审批驳回";
                        }else{
                            return "审批中";
                        }
                    },expFormat: function(d){
                        if (d.status === '1') {
                            return d.taskName;
                        } else if (d.status === '2') {
                            return '驳回';
                        } else if (d.status === '3') {
                            return '已完成';
                        } else if (d.status === '4') {
                            return '取消';
                        }else if (d.status === '0') {
                            return "审批驳回";
                        }else{
                            return "审批中";
                        }
                    }
                }
                , {field: 'inventory_type', title: '盘点对象', minWidth: 150, align: 'center',exportable: true
                    , templet: function (d) {
                        if (d.inventory_type === '1') {
                            return '供应链';
                        } else if (d.inventory_type === '3') {
                            return '供应商';
                        } else if (d.inventory_type === '2') {
                            return '代理商';
                        }
                    },expFormat: function (d) {
                        if (d.inventory_type === '1') {
                            return '供应链';
                        } else if (d.inventory_type === '3') {
                            return '供应商';
                        } else if (d.inventory_type === '2') {
                            return '代理商';
                        }
                    }
                    }
                , {field: 'inventory_object_name', title: '对象名称', minWidth: 260, align: 'center',exportable: true}
                , {field: 'warehouse_name', title: '盘点仓库', minWidth: 260, align: 'center',exportable: true}


                , {field: 'inventory_name', title: '盘点人', minWidth: 120, align: 'center',exportable: true}
                , {field: 'difference_amount', title: '盘盈/盘亏', minWidth: 120, align: 'center',exportable: true
                    ,templet: function (d) {
                        if (d.difference_amount > 0) {
                            return '+' + d.difference_amount;
                        } else {
                            return d.difference_amount;
                        }
                    },
                    expFormat: function (d) {
                        if (d.difference_amount > 0) {
                            return '+' + d.difference_amount;
                        } else {
                            return d.difference_amount;
                        }
                    }
                }
                , {field: 'remarks', title: '盘点备注', minWidth: 260, align: 'center',exportable: true}
                , {field: 'create_time', minWidth: 180, title: '盘点时间', align: 'center',exportable: true}
            ],
            ]
            ,exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '盘点单管理',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 5000,                    //当pageData为false生效
                fileName: '盘点单管理',
                where: function (){                 //当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            },done: function(res, curr, count){
                //得到数据总量
                entrycount = count;

            }
        });

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

        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    title: '新增',
                    content: '${ctx}/inventory/toAddInventoryOrderForm',
                    area: ['80%', '80%'],
                    success: function (layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                });
            }else if(obj.event === 'audit'){

                layer.open({
                    title:'审批',
                    type: 2,
                    area: ["60%", "70%"],
                    content: '${ctx}/inventory/toInventoryOrderDetailForm?mode=audit&id='+id+'&from=audit'
                })
            }else if (obj.event === 'delete') {
                if (checkStatus.data.length < 1) {
                    layer.msg("请先选择数据", {icon: 5, anim: 6});
                    return;
                }
                var ids = [];
                for (var i = 0; i < checkStatus.data.length; i++) {
                    var status = checkStatus.data[i].status;
                    if (status != '4') {
                        layer.msg('只有审核驳回状态才可操作删除', {icon: 5, anim: 6});
                        return;
                    }
                    ids.push(checkStatus.data[i].id);
                }
                layer.confirm('确认删除选中数据',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                deleteInventory(ids);
                                //关闭弹窗
                                layer.close(index);
                                console.log("回车确认时事件")
                                //取消连续回车键，导致不断弹出询问框
                                $(':focus').blur();
                            }
                        };
                        //监听键盘事件
                        $(document).on('keydown', this.enterEsc);
                        //esc键触发取消
                        this.escQuit = (ev) => {
                            let code_val = ev.which;
                            if(code_val == 0x1B){
                                $('.layui-layer-btn1').click();
                                return false;
                            }
                        };
                        $(document).on('keydown', this.escQuit);
                    },
                    end: function () {
                        //取消键盘监听事件
                        $(document).off('keydown', this.enterEsc);
                        $(document).off('keydown', this.escQuit);
                    },
                    yes: function () {
                        //确认执行的代码
                        deleteInventory(ids);
                    },
                    no: function () {

                    }
                });

            }
        });

        //监听事件
        table.on('tool(test)', function (obj) {

        });




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

        //重置
        form.on('submit(resetButton)', function (data) {
            $("#searchForm")[0].reset();
            layui.form.render();

        });

        window.showImg = function (src){
            layer.photos({ photos: {"data": [{"src": src}]} });
        }
    });


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

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

    /* 审批 */
    function toAudit(id) {
        layer.open({
            type: 2,
            title: '审批',
            content: '${ctx}/inventory/toInventoryOrderDetailForm?mode=audit&id=' + id+'&from=audit',
            area: ['80%', '80%']
        });
    }

    /*bom详情*/
    function siteDetail(id) {
        layer.open({
            type: 2,
            title: '查看',
            content: '${ctx}/inventory/toInventoryOrderDetailForm?mode=view&id=' + id,
            area: ['80%', '80%']
        });
    }

    /*编辑bom*/
    function editOrder(id) {
        layer.open({
            type: 2,
            title: '编辑',
            content: '${ctx}/inventory/toInventoryOrderDetailForm?mode=edit&id=' + id,
            area: ['80%', '80%'],
            success: function (layero, index) {
                childWin = window[layero.find('iframe')[0]['name']];
            }
        });
    }

    /* 查看物料清单 */
    function materialDetail(id) {
        layer.open({
            type: 2,
            title: '查看',
            content: '${ctx}/inventory/toMaterialListForm?id='+id,
            area: ['80%', '80%']
        });
    }

    function deleteInventory(ids){
        $.post('${ctx}/inventory/deleteOrder', {ids: ids.join(","), status: '1'}, function (result) {
            if (result.code === '200') {
                layer.msg('操作成功', {time: 1000}, function () {
                    $(".layui-laypage-btn").click();
                });
            } else {
                layer.msg('操作失败！' + result.errMsg);
            }
        });
    }



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