<%@ 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" />
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="WLGL_PCGL_PCGL_T" html='<a href="${ctx}/materialBatch/materialBathList"><li class="layui-this">库存批次</li></a>'/>
                <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"><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" lay-filter="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="materialCode" class="layui-input" value="${materialCode}">
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">物料类型：</label>
                        <div class="layui-input-block">
                            <select name="materialType" >
                                <option value="">请选择</option>
                                <c:forEach items="${types}" var="type">
                                    <option value="${type.columns.name}" >${type.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="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="createTimeSta" class="layui-input" id="endStartTime" placeholder="开始日期">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="createTimeEnd" class="layui-input" id="endEndTime" placeholder="结束日期">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">可用库存：</label>
                        <div class="layui-input-block">
                            <select name="availableStock" lay-search>
                                <option value="">请选择</option>
                                <option value="1">有库存</option>
                                <option value="2">无库存</option>
                            </select>
                        </div>
                    </div>
                    <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">
                                <select name="province" lay-search lay-filter="province"  id="province">
                                    <option value="">请选择</option>
                                    <c:forEach items="${province}" var="trader">
                                        <option value="${trader.columns.ProvinceName}"  title="${trader.columns.ProvinceID}">${trader.columns.ProvinceName}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="layui-col-lg4 layui-col-sm6">
                            <label class="layui-form-label">代理商：</label>
                            <div class="layui-input-block">
                                <select name="traderId" id="traderId" lay-search>
                                    <option value="">请选择</option>
                                    <c:forEach items="${traderList}" var="trader">
                                        <option value="${trader.columns.id}">${trader.columns.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                    </c:if>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">仓库：</label>
                        <div class="layui-input-block">
                            <select name="warehouseId" lay-search>
                                <option value="">请选择</option>
                                <c:forEach items="${wares}" var="ware">
                                    <option value="${ware.columns.id}" <c:if test="${warehouseId eq  ware.columns.id}">selected</c:if>>${ware.columns.name}</option>
                                </c:forEach>
                            </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="newestKeepTimeSta" class="layui-input" id="newestKeepTimeSta" placeholder="开始日期">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="newestKeepTimeEnd" class="layui-input" id="newestKeepTimeEnd" placeholder="结束日期">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-lg6 layui-col-sm6">
                        <label class="layui-form-label">库龄(天)：</label>
                        <div class="layui-input-block">
                            <input type="hidden" id="sapCategoryInput"  name="inStockTime" value="${inStockTime}<c:if test="${not empty inStockTime}">,</c:if>">
                            <input lay-filter="switchSap" type="checkbox" <c:if test="${inStockTime eq '1'}">checked</c:if>  value="1" title="30天以内" lay-skin="primary">
                            <input lay-filter="switchSap" type="checkbox" <c:if test="${inStockTime eq '2'}">checked</c:if>  value="2" title="30(含)天-60天" lay-skin="primary">
                            <input lay-filter="switchSap" type="checkbox" <c:if test="${inStockTime eq '3'}">checked</c:if> value="3" title="60(含)天-90天" lay-skin="primary">
                            <input lay-filter="switchSap" type="checkbox" <c:if test="${inStockTime eq '4'}">checked</c:if>  value="4" title="90天及以上" lay-skin="primary">
                        </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" lay-event="export" id="exportBtn"><i class="layui-icon layui-icon-export"></i>导出</button>
                </div>
            </script>
        </div>
    </div>
</div>

<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 endStartTime = laydate.render({
            elem: '#endStartTime'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新结束日期的最小日期
                endEndTime.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                endEndTime.config.elem[0].focus();
            }
        });
        //结束日期
        var endEndTime = laydate.render({
            elem: '#endEndTime'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新开始日期的最大日期
                endStartTime.config.max = lay.extend({}, date, {
                    day: date.day - 1
                });
            }
        });
        //开始日期
        var newestKeepTimeSta = laydate.render({
            elem: '#newestKeepTimeSta'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新结束日期的最小日期
                newestKeepTimeEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                newestKeepTimeEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var newestKeepTimeEnd = laydate.render({
            elem: '#newestKeepTimeEnd'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新开始日期的最大日期
                newestKeepTimeSta.config.max = lay.extend({}, date, {
                    day: date.day - 1
                });
            }
        });
        tableExt.render({
            elem: '#test'
            , title:'库存批次'
            , height: 'full-220'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/materialBatch/materialBathListData'
            , where: _serializeJson($("#searchForm"))
            , cols: [[
                {type: 'numbers',fixed:'left',title:'序号'}
                , {field: 'material_code', title: '物料代码',minWidth:'160', sort: false, align: 'center',exportable:true}
                , {field: 'qq_code', title: '外部编码',minWidth:'160', sort: false, align: 'center',exportable:true}
                , {field: 'material_name', title: '物料名称',minWidth:'180', sort: false, align: 'center', exportable:true}
                , {field: 'material_type', title: '物料类型',minWidth:'120', sort: false, align: 'center', exportable:true}
                , {field: 'material_specs', title: '物料规格',minWidth:'150', sort: false, align: 'center', exportable:true
                    ,templet:function(d){
                        if (d.material_type == '组件') {
                            return d.material_specs+"Wp";
                        } else {
                            return d.material_specs;
                        }
                    },expFormat:function(d){
                        if (d.material_type == '组件') {
                            return d.material_specs+"Wp";
                        } else {
                            return d.material_specs;
                        }
                    }
                }
                , {field: 'material_unit', title: '库存单位',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'material_brand', title: '供应商名称',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'code', title: '批次编号',minWidth:'170', sort: false, align: 'center',exportable:true}
                , {field: 'amount', title: '入库数量',minWidth:'100', sort: false, align: 'center', exportable:true}
                , {field: 'bd_sale_price', title: '零售单价', minWidth: 100, sort: false, align: 'center', exportable: true}
                , {field: 'total_price', title: '零售总价', minWidth: 100, sort: false, align: 'center', exportable: true,
                    templet:function(d){
                        return d.bd_sale_price * d.amount;
                    },expFormat:function(d){
                        return d.bd_sale_price * d.amount;
                    }
                }
                , {field: 'remaining', title: '可用库存',minWidth:'100', sort: false, align: 'center', exportable:true}
                , {field: 'inStockTime', title: '库龄(天)',minWidth:'130', sort: false, align: 'center', exportable:true,
                    templet:function(d){
                        if (d.remaining > 0 ) {
                            let inputTime = new Date(d.input_time).getTime();
                            let now = new Date().getTime();
                            let diff = (now-inputTime)/(1000 * 60 * 60 * 24);
                            return Math.ceil(diff);
                        } else {
                            // 可用库存为0取最新出库时间-本批次的入库时间来计算
                            let time = new Date(d.newest_keep_time).getTime();
                            let inputTime = new Date(d.input_time).getTime();
                            let diff = (time-inputTime)/(1000 * 60 * 60 * 24);
                            return Math.ceil(diff);
                        }
                        },expFormat:function(d){
                            if (d.remaining > 0 ) {
                                let inputTime = new Date(d.input_time).getTime();
                                let now = new Date().getTime();
                                let diff = (now-inputTime)/(1000 * 60 * 60 * 24);
                                return Math.ceil(diff);
                            } else {
                                // 可用库存为0取最新出库时间-本批次的入库时间来计算
                                let time = new Date(d.newest_keep_time).getTime();
                                let inputTime = new Date(d.input_time).getTime();
                                let diff = (time-inputTime)/(1000 * 60 * 60 * 24);
                                return Math.ceil(diff);
                            }
                        }
                }
                , {field: 'warehouse_name', title: '仓库名称',minWidth:'240', sort: false, align: 'center',exportable:true}
                , {field: 'trader_name', title: '代理商',minWidth:'180', sort: false, align: 'center',exportable:true}
                , {field: 'delivery_method', title: '发货方式',minWidth:'100', sort: false, align: 'center',exportable:true}
                , {field: 'biz_code', title: '关联单号',minWidth:'160', sort: false, align: 'center',exportable:true}
                , {field: 'newest_keep_time', title: '最新出库时间',minWidth:'180', sort: false, align: 'center',exportable:true}
                , {field: 'input_time', title: '入库时间',minWidth:'180', sort: false, align: 'center',exportable:true}

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


        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            //执行重载
            tableExt.reload('test', {
                where: _serializeJson($("#searchForm")),
                page:{
                    curr:1
                }
            });
        });
        form.on('checkbox(switchSap)', function (data) {
            var sapData = document.getElementById("sapCategoryInput").value;
            if (data.elem.checked) {　　//判断当前多选框是选中还是取消选中
                $(this).prop("checked", true);
                form.render('checkbox'); // 重新渲染复选框
                sapData += data.value + ","
                $("#sapCategoryInput").val(sapData)
            } else {
                var rdata = data.value + ",";
                sapData = sapData.replace(rdata, "");
                $(this).prop("checked", false);
                $("#sapCategoryInput").val(sapData)
                form.render('checkbox'); // 重新渲染复选框
            }
        });

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


    function searchOrder() {
        //执行重载
        table.reload('test', {
            where: function (){
                return _serializeJson($("#searchForm"));
            }
        });
    }
    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }

    function reset(){
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input[type='text']").val("");
        $("input[type='checkbox']").prop("checked",false);
        $("#sapCategoryInput").val("");
        layui.form.render();
    }
</script>
</body>
</html>


