<%@ 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_CGDGL_CGDGL_TMGL_T" html='<li class="layui-this"><a href="${ctx}/barcodeManage/barcodeList">条码管理</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 "  id="searchBtn"  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 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="materialCode" class="layui-input">
                        </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="qq_code" class="layui-input">
                        </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="materialName" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">物料类型：</label>
                        <div class="layui-input-block">
                            <select name="materialType">
                                <option value="">请选择</option>
                                <c:forEach var="materialType" items="${materialTypeList}">
                                    <option value="${materialType.columns.id}">${materialType.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="materialSpecification" class="layui-input" >
                        </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="materialRemark" class="layui-input" >
                        </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="materialBarcode" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">状态：</label>
                        <div class="layui-input-block">
                           <select name="barcodeStatus">
                               <option value="">请选择</option>
                               <option value="1">已使用</option>
                               <option value="2">未使用</option>
                           </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="batch_Code" 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="create_time" class="layui-input" id="create_time" value="${minTime}"
                                       placeholder="开始时间">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="create_time_end" class="layui-input" id="create_time_end" value="${maxTime}"
                                       placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </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">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" id="addBtn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>批量导入 </button>
                    <button class="layui-btn layui-btn-sm" id="exportBtn" lay-event="export"><i class="layui-icon layui-icon-export"></i>导出</button>
                    <sfTags:pagePermission authFlag="CKGL_CGDGL_CGDGL_TMGL_SC_B" html='<button class="layui-btn layui-btn-sm" id="deleteBtn" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除 </button>'/>
                </div>
            </script>
        </div>

    </div>

</div>

</div>

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

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

        //方案信息列表
        tableExt.render({
            elem: '#test'
            , height: 'full-190'
            , defaultToolbar: false
            , url: '${ctx}/barcodeManage/barcodeDataList'
            , page: true
            , toolbar:"#toolbarDemo"
            , method:"post"
            , limits: [20, 50, 100]
            , limit: 20
            , where:_serializeJson($("#searchForm"))
            , cols: [[
                {title: '序号', type: 'numbers', fixed: 'left'}
                ,{field : 'id',checkbox : true, fixed: 'left'}
                , {field: 'material_barcode', minWidth: 150, title: '物料条码', align: 'center',exportable:true , fixed: 'left'}
                , {field: 'code', title: '物料代码', minWidth: 150, align: 'center',exportable:true , fixed: 'left'}
                , {field: 'qq_code', title: '外部编码', minWidth: 150, align: 'center',exportable:true , fixed: 'left'}
                , {field: 'material_name', title: '物料名称', minWidth: 200, align: 'center',exportable:true }
                , {field: 'material_type', title: '物料类型', width: 120, align: 'center',exportable:true }
                , {field: 'material_specs', minWidth: 200, title: '物料规格', align: 'center',exportable:true }
                , {field: 'material_model', minWidth: 200, title: '物料型号', align: 'center',exportable:true }
                , {field: 'remarks', minWidth: 150, title: '物料描述', align: 'center',exportable:true}
                , {field: 'material_brand', width: 150, title: '物料品牌', align: 'center',exportable:true }
                , {field: 'create_time', width: 150, title: '导入时间', align: 'center',exportable:true }
                , {field: 'unit', width: 100, title: '单位', align: 'center',exportable:true }
                , {field: 'batch_Code', width: 100, title: '批次号', align: 'center',exportable:true }
                , {field: 'batch_Code_note', width: 100, title: '批次号备注', align: 'center',exportable:true }
                , {field: 'power_station_code', title: '电站编码', minWidth: 150, align: 'center',exportable:true }
                , {field: 'partner_name', title: '电站资方', minWidth: 150, align: 'center',exportable:true }
                , {field: 'barcode_status', width: 120, title: '条码状态', align: 'center',exportable:true ,
                    templet: function (d) {
                        if (d.barcode_status == '1') {
                            return "已使用";
                        }
                        return "未使用";
                    },
                    expFormat: function (d) {
                        if (d.barcode_status == '1') {
                            return "已使用";
                        }
                        return "未使用";
                    }
            }],
            ]
            ,exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '条码管理',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 10000,                    //当pageData为false生效
                fileName: '条码管理',
                where: function (){
                    console.log(_serializeJson($("#searchForm")));//当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        table.on('toolbar(test)',function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'add') {
                layer.open({
                    title: '批量导入',
                    type: 2,
                    area: ['550px', '300px'],
                    content: '${ctx}/barcodeManage/toImportBarcode'
                });
            }else  if (obj.event === 'delete') {
                var data = checkStatus.data;
                if (data.length < 1) {
                    layer.msg("请先选择数据",{icon:5,anim:6});
                    return;
                }
                var ids="";
                for(var i=0;i<data.length;i++){
                    ids+=data[i].id+",";
                }
                var barcodeStatus = "";
                for(var i=0;i<data.length;i++){
                    if(data[i].barcode_status === '1'){
                        barcodeStatus +=data[i].material_barcode+",";
                    }
                }
                if(barcodeStatus!=""){
                    let s = barcodeStatus.substring(0,barcodeStatus.length-1);
                    layer.msg(s+"已使用不能删除！",{icon:5,anim:6});
                    return;
                }
                layer.confirm('您确定要删除条码吗？',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                deletePlan(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 () {
                        //确认执行的代码
                        deletePlan(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
                }
            });
        });

        $('input').on('keydown', function(event){
            if(event.keyCode == 13){
                //执行相关操作
                $("#searchBtn").click();
            }
        });

    });

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

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

    function deletePlan(ids){

        $.ajax({
            type: "POST",
            url: '${ctx}/barcodeManage/delete',
            data: {
                ids: ids
            },
            success: function (result) {
                if(result.code=='200'){
                    layer.msg('删除成功！', {icon: 1, time: 1000}, function () {
                        window.location.reload();
                    });
                }else {
                    layer.msg('删除失败!', {icon: 2});
                }
            },
            error:{
            }
        })
    }
</script>
</body>
</html>
