<%@ 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="BOMGL_BOMGL_BOMGL_T"
                                       html='<li class="layui-this"><a href="${ctx}/bom/toBomPage">一级BOM管理</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">BOM代码：</label>
                        <div class="layui-input-block">
                            <input type="text" name="number" class="layui-input" id="number" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">BOM描述：</label>
                        <div class="layui-input-block">
                            <input type="text" name="remarks" class="layui-input" id="remarks" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">BOM类型：</label>
                        <div class="layui-input-block">
                            <select name="materielTypeId">
                                <option value="">请选择</option>
                                <c:forEach var="type" items="${materielTypeList}">
                                    <option value="${type.columns.id}">${type.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">
                            <select name="materielGroupId" lay-search>
                                <option value="">请选择</option>
                                <c:forEach var="group" items="${materielGroupList}">
                                    <option value="${group.columns.id}">${group.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">
                            <select name="status">
                                <option value="">请选择</option>
                                <option value="4">待启用</option>
                                <option value="1">已启用</option>
                                <option value="3">已禁用</option>
                            </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="submitTimeMin" class="layui-input" id="submitTimeMin"
                                       placeholder="开始日期" value="">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="submitTimeMax" class="layui-input" id="submitTimeMax"
                                       placeholder="结束日期" value="">
                            </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="BOMGL_BOMGL_BOMGL_ADD_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="BOMGL_BOMGL_BOMGL_VALID_B"
                           html='<button class="layui-btn layui-btn-sm" id="validBtn" lay-event="valid"><i class="layui-icon layui-icon-auz"></i>启用</button>'/>
</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.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;
        table = layui.table;
        laydate = layui.laydate;
        var excel = layui.excel;
        var detail = '${fns:checkPagePermission(request, "BOMGL_BOMGL_BOMGL_VIEW_B")}';
        var disableBtn = '${fns:checkPagePermission(request, "BOMGL_BOMGL_BOMGL_DISABLE_B")}';
        var enableBtn = '${fns:checkPagePermission(request, "BOMGL_BOMGL_BOMGL_ENABLE_B")}';
        var editBtn = '${fns:checkPagePermission(request, "BOMGL_BOMGL_BOMGL_EDIT_B")}';

        //方案信息列表
        tableExt.render({
            elem: '#test'
            , height: 'full-170'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/bom/ajaxForBomData'
            , 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',
                    fixed: 'left'
                }
                , {field: 'id', minWidth: '4%', checkbox: true, align: 'center', rowspan: '2', fixed: 'left'}

                , {field: 'id', title: '操作', minWidth: 150, align: 'center',fixed: 'left'
                    , templet: function (d) {
                    var html = "";
                        if (d.status === '1') { // 已启用
                            if (disableBtn == '0') {
                                html += '<a href="#" class="layui-table-link" onclick="updateStatus(\''+d.id+'\', \'3\')">禁用</a>&nbsp;';
                            }
                        } else if (d.status === '3') {// 已禁用
                            if (enableBtn == '0') {
                                html += '<a href="#" class="layui-table-link" onclick="updateStatus(\''+d.id+'\', \'1\')">解禁</a>&nbsp;';
                            }
                        } else if (d.status === '4') {// 待启用
                            if (disableBtn = '0') {
                                html += '<a href="#" class="layui-table-link" onclick="updateStatus(\''+d.id+'\', \'3\')">禁用</a>&nbsp;';
                            }
                            if (editBtn == '0') {
                                html += '<a href="#" class="layui-table-link" onclick="editBom(\''+d.id+'\')">编辑</a>&nbsp;';
                            }
                        }
                        return html;
                    }
                }
                , {
                    field: 'status', title: '状态', minWidth: 150, align: 'center', exportable: true,fixed: 'left'
                    , templet: function (d) {
                        if (d.status === '1') {
                            return '已启用';
                        } else if (d.status === '3') {
                            return '已禁用';
                        } else if (d.status === '4') {
                            return '待启用';
                        }
                    }
                    , expFormat: function (d) {
                        if (d.status === '1') {
                            return '已启用';
                        } else if (d.status === '3') {
                            return '已禁用';
                        } else if (d.status === '4') {
                            return '待启用';
                        }
                    }
                }
                , {field: 'number', title: 'BOM代码', minWidth: 150, align: 'center', exportable: true}
                , {field: 'materiel_group', title: '物料组', minWidth: 150, align: 'center', exportable: true}
                , {field: 'materiel_type', title: 'BOM类型', minWidth: 150, align: 'center', exportable: true}
                , {field: 'remarks', title: 'BOM描述', minWidth: 200, align: 'center', exportable: true, rowspan: '2'}
                , {
                    field: 'id', minWidth: 150, title: '物料清单', align: 'center', exportable: true, rowspan: '2',
                    templet: function (d) {
                        if (detail == '0') {
                            return "<a href=\"#\" class=\"layui-table-link\" onclick=\"siteDetail('" + d.id + "')\">查看</a>";
                        }
                        return "";
                    }
                }
                , {
                    field: 'submit_time',
                    minWidth: 150,
                    title: '最近编辑时间',
                    align: 'center',
                    exportable: true,
                    rowspan: '2'
                }
            ],
            ]
            , exportOpt: {                           //导出选项配置
                ele: '#exportBtn',                   //导出的Dom元素
                headerTitle: '一级BOM',             //excel表格的标题
                pageData: false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '一级BOM',
                where: function () {
                    console.log(_serializeJson($("#searchForm")));//当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        //开始日期
        var submitTimeMin = laydate.render({
            elem: '#submitTimeMin'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                submitTimeMax.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                submitTimeMax.config.elem[0].focus();
            }
        });
        //结束日期
        var submitTimeMax = laydate.render({
            elem: '#submitTimeMax'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                submitTimeMin.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}/bom/toAddBomForm',
                    area: ['80%', '80%'],
                    success: function (layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                });
            } else if (obj.event === 'valid') {
                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('只有待启用状态才可操作启用');
                        return;
                    }
                    ids.push(checkStatus.data[i].id);
                }
                layer.confirm('确认启用选中数据？', {icon: 3}, function () {
                    $.post('${ctx}/bom/updateStatus', {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);
                        }
                    });
                });

            }
        });

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

        });

        function isBlank(val) {
            if (val == null || val == '' || val == undefined) {
                return true;
            }
            return false;
        }


        //监听搜索
        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.importMaterial = function (materials) {
        childWin.importMaterial(materials);
    };

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

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

    /*编辑bom*/
    function editBom(id) {
        layer.open({
            type: 2,
            title: '编辑',
            content: '${ctx}/bom/toBomDetail?mode=edit&id=' + id,
            area: ['80%', '80%']
        });
    }

    /*更改状态*/
    function updateStatus(id, status) {
        var content = '';
        if (status === '1') {
            content = '启用';
        } else if (status === '3') {
            content = '禁用';
        }
        layer.confirm('确定' + content + '该数据？', {icon: 3}, function () {
            $.post('${ctx}/bom/updateStatus', {ids: id, status: status}, function (result) {
                if (result.code === '200') {
                    layer.msg('操作成功', {time: 1000}, function () {
                        $(".layui-laypage-btn").click();
                    });
                } else {
                    layer.msg('操作失败！' + result.errMsg);
                }
            });
        });
    }


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