<%@ 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" 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="FAGL_DQCLGL_DQCLGL_T" html='<li class=""><a href="${ctx}/planManage/electricMaterialSet">物料管理</a></li>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset" id="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" 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-lg2 layui-col-sm3">
                        <label class="layui-form-label">物料类型：</label>
                        <div class="layui-input-block">
                            <select name="type" id="type" lay-search lay-verify="required">
                                <option value="">请选择</option>
                                <c:forEach items="${types}" var="ty">
                                    <option value="${ty.columns.id}" data-value="${ty.columns.attribute}">${ty.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="model" class="layui-input" id="model" >
                        </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-xs3">
                                <input type="text" name="createTimeSta" class="layui-input" id="createTimeSta" placeholder="开始日期">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs3">
                                <input type="text" name="createTimeEnd" class="layui-input" id="createTimeEnd" placeholder="结束日期">
                            </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">
 <%--   <sfTags:pagePermission authFlag="FAGL_DQCLGL_TJ_B" 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="FAGL_DQCLGL_XG_B" html='<button class="layui-btn layui-btn-sm" id="editBtn" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑 </button>'/>--%>
    <sfTags:pagePermission authFlag="FAGL_DQCLGL_SC_B" html='<button class="layui-btn layui-btn-sm" id="deleteBtn" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除 </button>'/>
    <%--<sfTags:pagePermission authFlag="FAGL_DQCLGL_PLDR_B" html='<button class="layui-btn layui-btn-sm" id="import" lay-event="import">批量导入</button>'/>--%>
</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 saveMark = false;
    //初始数据
    var $form;
    var form;
    var upload;
    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
            ,tableExt = layui.tableExt;
        laydate = layui.laydate;
        table = layui.table;
        var excel = layui.excel;

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

        var detail = '${fns:checkPagePermission(request, "FAGL_FAGL_FAGL_DETAIL_B")}';
        var download = '${fns:checkPagePermission(request, "FAGL_FAGL_FAGL_DOWNLOAD_B")}';
        //方案信息列表
        tableExt.render({
            elem: '#test'
            , height: 'full-130'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/planManage/electricMaterialSetList'
            , 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',rowspan:'2'}
                ,{field : 'id', minWidth : '4%',checkbox : true,align : 'center',rowspan:'2'}
                , {field: 'type', title: '物料类型', minWidth: 180, align: 'center',exportable:true ,rowspan:'2'
                   /* ,templet:function(d){
                    if(d.type=='1'){
                        return "逆变器";
                    }else if(d.type=='2'){
                        return "并网箱";
                    }else if(d.type=='4'){
                        return "支架";
                    }else{
                        return "组件";
                    }
                    }*/
                }
                , {field: 'model', title: '物料规格', minWidth: 200, align: 'center',exportable:true ,rowspan:'2'
                    ,templet:function(d){
                        if(d.type=='3'){
                            return d.model+"W";
                        }else{
                            return d.model;
                        }
                    }
                }
                , {field: 'material_model', minWidth: 180, title: '物料型号', align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'create_time', minWidth: 180, title: '创建时间', align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'remarks', minWidth: 300, title: '物料描述', align: 'center',exportable:true ,rowspan:'2'}
            ],
            ]
            ,exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '物料管理',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当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({
                    type: 2,
                    title: '新增',
                    content: '${ctx}/planManage/addElectricMaterialSetPage',
                    area: ['40%','40%'],
                    success: function(layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                });
            } else if (obj.event === 'edit') {
                if(checkStatus.data.length < 1){
                    layer.msg("请先选择数据",{icon:5,anim:6});
                    return;
                }else if(checkStatus.data.length > 1){
                    layer.msg("只能选择一条数据",{icon:5,anim:6});
                    return;
                }
                layer.open({
                    type: 2,
                    title: '编辑',
                    content: '${ctx}/planManage/addElectricMaterialSetPage?id=' + checkStatus.data[0].id,
                    area: ['40%','40%'],
                    success: function(layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                });
            } 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+",";
                }
                layer.confirm('您确定要删除电气材料吗？',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                deleteElectric(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 () {
                        //确认执行的代码
                        deleteElectric(ids);
                    },
                    no: function () {

                    }
                });
            }else if(obj.event ==='import'){
                layer.open({
                    title: '导入电气材料',
                    type: 2,
                    area: ['550px', '300px'],
                    content: '${ctx}/planManage/toImportElectricMaterialSet'
                });

            }
        });

        $("#reset").on('click', function () {
            $("#searchForm").find("select").val("");
            $("#searchForm").find("input").val("");
        })

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

        });

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

        function isBlank(val) {
            if (val == null || val == '' || val == undefined) {
                return true;
            }
            return false;
        }
        $('#searchBtn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        var active = {
            reload: function(){
                reload();
            }
        };
    });

    function reload(){
        table.reload('test', {
            where: {
                type:$("#type").val().trim(),
                model:$("#model").val().trim(),
                createTimeSta:$("#createTimeSta").val().trim(),
                createTimeEnd:$("#createTimeEnd").val().trim(),
            }
        });
    }

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


    function deleteElectric(ids){
        $.ajax({
            type: "POST",
            url: "${ctx}/planManage/deleteElectricMaterialSet",
            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>



