<%@ 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;}
        .layui-btn + .layui-btn {
            margin-left: 35px;
        }
    </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">
            <c:choose>
                <c:when test="${userType eq '1' || userType eq '4'}">
                    <sfTags:pagePermission authFlag="XTSZ_CKSZ_CKSZ_T" html='<li class="layui-this sf-tab"><a href="${ctx}/warehouseSetting/warehouseSettingPage">仓库设置</a></li>'/>
                </c:when>
                <c:otherwise>
                    <sfTags:pagePermission authFlag="XTSZ_CKSZ_CKSZ_TT" html='<li class="layui-this sf-tab"><a href="${ctx}/warehouseSetting/warehouseSettingPage">仓库设置</a></li>'/>
                </c:otherwise>
            </c:choose>

                <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="name" id="name" 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="personInCharge" 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="mobile" autocomplete="off" 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="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>
        </div>
    </div>

    <div class='layui-card'>
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <c:choose>
                    <c:when test="${userType eq '1' || userType eq '4'}">
                        <sfTags:pagePermission authFlag="XTSZ_CKSZ_XZ_T" html='<button class="layui-btn layui-btn-sm" id="add" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
                    </c:when>
                    <c:otherwise>
                        <sfTags:pagePermission authFlag="XTSZ_CKSZ_XZ_TT" html='<button class="layui-btn layui-btn-sm" id="traderAdd" lay-event="traderAdd"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
                    </c:otherwise>
                </c:choose>
             </script>
        </div>
    </div>

</div>


<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/html" id="table-toolbar">
    <c:choose>
        <c:when test="${userType eq '1' || userType eq '4'}">
            {{# if(d.type == '1'){ }}
                {{# if(d.status == '0'){ }}
                <sfTags:pagePermission authFlag="XTSZ_CKSZ_JY_T" html='<a class="layui-table-opt-link" lay-event="disabled">禁用</a>'/>
                {{# }else{ }}
                <sfTags:pagePermission authFlag="XTSZ_CKSZ_QY_T" html='<a class="layui-table-opt-link" lay-event="canUse">启用</a>'/>
                {{# } }}
                <sfTags:pagePermission authFlag="XTSZ_CKSZ_BJ_T" html='<a class="layui-table-opt-link ml-10" lay-event="edit">编辑</a>'/>
            {{# } }}
        </c:when>
        <c:when test="${userType eq '7' || userType eq '6'}">
            {{# if(d.status == '0'){ }}
            <sfTags:pagePermission authFlag="XTSZ_CKSZ_JY_TT" html='<a class="layui-table-opt-link" lay-event="disabled">禁用</a>'/>
            {{# }else{ }}
            <sfTags:pagePermission authFlag="XTSZ_CKSZ_QY_TT" html='<a class="layui-table-opt-link" lay-event="canUse">启用</a>'/>
            {{# } }}
            <sfTags:pagePermission authFlag="XTSZ_CKSZ_BJ_TT" html='<a class="layui-table-opt-link ml-10" lay-event="traderEdit">编辑</a>'/>

        </c:when>
        <c:otherwise>

        </c:otherwise>
    </c:choose>

</script>

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

        table.render({
            elem: '#test'
            ,toolbar : '#toolbarDemo'
            , height: 'full-180'
            ,defaultToolbar : false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            ,url: '${ctx}/warehouseSetting/warehouseSettingList'
            ,cols: [[
                {type: 'numbers',title:'序号', fixed: 'left'}
                ,{field:'id', title: '操作',toolbar: '#table-toolbar',align: 'center',minWidth: 150, fixed: 'left'}
                <c:if test="${userType eq '1' || userType eq '4'}">
                ,{field:'name', title: '仓库名称',align: 'center'}
                </c:if>
                <c:if test="${userType eq '7' || userType eq '6'}">
                ,{field:'name', title: '仓库名称',align: 'center',fixed:'left',width: 180,exportable:true
                    ,templet: function (d) {
                        return "<a href=\"#\" class=\"layui-table-link \" style='font-size:14px;' onclick=\"lookDetail('" + d.id + "')\">" + d.name + "</a>";
                    }
                }
                </c:if>
                ,{field:'status', title: '仓库状态',align: 'center'
                  ,templet:function(d){
                     if(d.status=='0'){
                         return '启用';
                     }else{
                         return '禁用';
                     }
                    }
                }
                ,{field:'person_in_charge', title: '接货人',align: 'center'}
                ,{field:'mobile', title: '联系方式',align: 'center'}
                ,{field:'qq_code', title: '外部编码',align: 'center'}
                ,{field:'address', title: '仓库地址',align: 'center'}
                ,{field:'create_time', title: '创建时间',align: 'center'}
            ]]
            ,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(test)', function (obj) {
            switch (obj.event){
                case 'add':
                    layer.open({
                        title:'新增',
                        type: 2,
                        area: ["800px", "600px"],
                        content: '${ctx}/warehouseSetting/addWarehouseSettingPage'
                    })
                    break;
                case 'traderAdd':
                    layer.open({
                        title:'申请新增',
                        type: 2,
                        area: ["70%", "80%"],
                        content: '${ctx}/apply/warehouseApply/addWarehouseApply'
                    })
                    break;

            }
        })

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var id = data.id;
            if(obj.event === 'disabled'){
                layer.confirm('禁用后，该仓库将不可再进行入库使用，确认禁用？',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                disabledWarehouse(id);
                                //关闭弹窗
                                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 () {
                        //确认执行的代码
                        disabledWarehouse(id);
                    },
                    no: function () {

                    }
                });
            } else if(obj.event === 'canUse'){
                layer.confirm('确认启用该仓库？',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                canUseWarehouse(id);
                                //关闭弹窗
                                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 () {
                        //确认执行的代码
                        canUseWarehouse(id);
                    },
                    no: function () {

                    }
                });
            } else if(obj.event === 'edit'){
                var id = data.id;
                layer.open({
                    title:'编辑',
                    type: 2,
                    area: ["800px", "600px"],
                    fixed: false,
                    content: '${ctx}/warehouseSetting/addWarehouseSettingPage?id='+id
                })
            }else if ( obj.event ==='traderEdit'){
                var id = data.id;
                layer.open({
                    title:'编辑',
                    type: 2,
                    area: ["70%", "80%"],
                    fixed: false,
                    content: '${ctx}/apply/warehouseApply/addWarehouseApply?id='+id
                })
            }
        });

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



    });


    function disabledWarehouse(id){
        $.post('${ctx}/warehouseSetting/disabledWarehouse', {id: id}, function(result){
            if("200" == result.code){
                layer.msg('操作成功', {time: 2000}, function () {
                    reload();
                });
            }else if("501"==result.code){
                layer.msg(result.errMsg, {icon: 2});
            }else{
                layer.msg("禁用失败");
            }
        });
    }

    function canUseWarehouse(id){
        $.post('${ctx}/warehouseSetting/canUsedWarehouse', {id: id}, function(result){
            if("200" == result.code){
                layer.msg('操作成功', {time: 2000}, function () {
                    reload();
                });
            }else if("501"==result.code){
                layer.msg(result.errMsg, {icon: 2});
            }else{
                layer.msg("启用失败");
            }
        });
    }

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

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

    function lookDetail(id){
        layer.open({
            title:'查看',
            type: 2,
            area: ["70%", "80%"],
            content: '${ctx}/apply/warehouseApply/warehouseApplyDetail?id='+id
        })
    }

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