<%@ 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" type="text/css" href="${ctxPlugin}/static/css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css"/>
    <style>
        .layui-table-tool-temp {
            padding-right: 0
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <form class="layui-form layui-card" id="searchForm">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="KCGL_DLSKC_T" html='<<a href="${ctx}/material/stockList2"><li class="layui-this">库存管理</li></a>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="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 type="button"
                        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">
            <input type="hidden" name="showZeroStock"/>
            <div class="layui-form-item">
<%--                <c:if test="${userType eq '1' || userType eq '4'}">--%>
<%--                    <div class="layui-col-lg2 layui-col-sm3">--%>
<%--                        <label class="layui-form-label">物料归属：</label>--%>
<%--                        <div class="layui-input-block">--%>
<%--                            <div id="belong"></div>--%>
<%--                        </div>--%>
<%--                    </div>--%>
<%--                </c:if>--%>
                <c:if test="${userType ne '1' && userType ne '4'}">
                    <input type="hidden" name="ascriptionId" value="${ascriptionId}"/>
                </c:if>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">物料代码：</label>
                    <div class="layui-input-block">
                        <input type="text" name="code" 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="qq_code" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">物料类型：</label>
                    <div class="layui-input-block">
                        <select name="materialTypeId" id="materialTypeId">
                            <option value="">请选择</option>
                            <c:forEach items="${types}" var="ty">
                                <option value="${ty.columns.id}">${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="remarks" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">仓库：</label>
                    <div class="layui-input-block">
                        <select name="warehouseIds" id="warehouseId" lay-search>
                            <option value="">请选择</option>
                            <c:forEach items="${wares}" var="warehouse">
                                <option value="${warehouse.columns.id}">${warehouse.columns.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

            </div>
        </div>
    </form>
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
        </div>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <input type="checkbox" name="zero" lay-filter="zero" title="显示库存为零的物料" lay-skin="primary">
    <sfTags:pagePermission authFlag="KCGL_KCGL_DC_B"
                           html='<button class="layui-btn layui-btn-sm f-r" id="exportBtn" lay-event="exports"><i class="layui-icon layui-icon-export"></i>导出</button>'/>
</script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/xm-select.js"></script>
<script>
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer', 'laypage', 'laydate', 'form'], function () {
        $ = layui.$;
        tableExt = layui.tableExt;
        layer = layui.layer;
        laypage = layui.laypage;
        laydate = layui.laydate;
        form = layui.form;
        table = tableExt.getLayTable();

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

        <c:if test="${userType eq '1' || userType eq '4'}">
        xmSelect.render({
            el: '#belong',
            name: 'ascriptionId',
            size: 'mini',
            filterable: true,
            model: {
                label: {
                    type: 'block',
                    block: {
                        //最大显示数量, 0:不限制
                        showCount: 1,
                        //是否显示删除图标
                        showIcon: true,
                    }
                }
            },
            toolbar: {
                show: true
            },
            height: '300px',
            data: [
                {name: '供应链', value: 'a'},
<%--                <c:forEach items="${traderList}" var="trader">--%>
<%--                {name: '${trader.columns.name}', value: '${trader.columns.id}'},--%>
<%--                </c:forEach>--%>
                <c:forEach items="${supplyList}" var="supply">
                {name: '${supply.columns.name}', value: '${supply.columns.id}'},
                </c:forEach>
            ]
        });
        </c:if>

        form.on('checkbox(zero)', function (d) {
            console.log(d);
            var showZeroStock = d.elem.checked ? "1" : "";
            $('input[name=showZeroStock]').val(showZeroStock);
            renderTable();
            $('input[name="zero"]').prop('checked', d.elem.checked);
            form.render();
        });

        renderTable();

        function renderTable() {
            tableExt.render({
                elem: '#test'
                , height: 'full-180'
                , toolbar: '#toolbarDemo'
                , defaultToolbar: false
                , limits: [20, 50, 100]
                , limit: 20
                , page: true //开启分页
                , url: '${ctx}/material/ajaxForStockList2'
                , where: _serializeJson($("#searchForm"))
                , cols: [[
                    {type: 'numbers', fixed: 'left', title: '序号'}

                    , {
                        field: 'warehouseName',
                        title: '仓库名称',
                        minWidth: 280,
                        sort: false,
                        align: 'center',
                        exportable: true
                    }
                    , {
                        field: 'code',
                        title: '物料代码',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true
                    } ,
                    {
                        field: 'qq_code',
                        title: '外部编码',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 180
                    }
                    , {
                        field: 'materialName',
                        title: '物料名称',
                        minWidth: 150,
                        sort: false,
                        align: 'center',
                        exportable: true
                    }
                    , {
                        field: 'typeName',
                        title: '物料类型',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 100
                    }
                    , {
                        field: 'material_specs',
                        title: '物料规格',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 300,
                        templet: function (d) {
                            if (d.typeName == '组件') {
                                return d.material_specs+"Wp";
                            } else {
                                return d.material_specs;
                            }
                        },expFormat:function(d){
                            if (d.typeName == '组件') {
                                return d.material_specs+"Wp";
                            } else {
                                return d.material_specs;
                            }
                        }
                    }, {
                        field: 'unitName',
                        title: '库存单位',
                        sort: false,
                        align: 'center',
                        minWidth: 100,
                        exportable: true
                    }
                    , {
                        field: 'material_brand',
                        title: '物料品牌',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 100
                    }

                    , {
                        field: 'warning',
                        title: '可用库存',
                        sort: false,
                        align: 'center',
                        minWidth: 100,
                        exportable: true
                    }
                    , {
                        field: 'froze_out_num',
                        title: '冻结库存',
                        sort: false,
                        align: 'center',
                        minWidth: 150,
                        exportable: true,
                        templet: function (d) {
                            return "<a href=\"#\" class=\"layui-table-link layui-icon   \" style='font-size:14px;' onclick=\"frozeOutNum('" + d.id + "')\">" + d.froze_out_num + "</a>&nbsp;&nbsp;";
                        }
                    }

                    , {
                        field: 'bd_sale_price',
                        title: '零售价格',
                        sort: false,
                        align: 'center',
                        minWidth: 100,
                        exportable: true
                    }
                    <c:if test="${userType eq '1' || userType eq '4'}">
                    , {
                        field: 'ascription_name',
                        title: '物料归属',
                        minWidth: 280,
                        sort: false,
                        align: 'center',
                        exportable: true
                    }
                    </c:if>

                    , {
                        field: 'remarks',
                        title: '物料描述',
                        sort: false,
                        align: 'center',
                        exportable: true,
                        minWidth: 280
                    }



                ]]
                , exportOpt: {                           //导出选项配置
                    ele: '#exportBtn',                   //导出的Dom元素
                    headerTitle: '库存管理',             //excel表格的标题
                    pageData: false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                    maxAmount: 6000,                    //当pageData为false生效
                    fileName: '库存管理',
                    where: function () {                 //当pageData为false时生效，过滤的条件
                        return _serializeJson($("#searchForm"));
                    }
                }
            });
        }


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            switch (obj.event) {
            }
        });

        //监听行单击事件
        table.on('row(test)', function (obj) {
            var data = obj.data;
            var oldData = table.cache["test"];
            var checkCell = obj.tr.find(".layui-form-checkbox");
            var CHECKED;

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

    });

    /**
     * 编辑
     */
    function editMaterialStock(id) {
        layer.open({
            type: 2,
            title: "编辑",
            resize: false,
            area: ['50%', '65%'],
            content: '${ctx}/material/editMaterialStock?id=' + id
        });
    }

    /**
     * 冻结库存
     */
    function frozeOutNum(id) {
        layer.open({
            type: 2,
            title: "查看",
            resize: false,
            area: ['80%', '70%'],
            content: '${ctx}/material/frozeStockDetail?id=' + id
        });
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>

