<!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">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <style type="text/css">
        .LAY-btns .layui-nav {padding-left:0;padding-right:10px;top:-4px;margin: 0 10px;border: 0;background-color: #009688;}
        .LAY-btns .layui-nav .layui-nav-item{line-height: 30px;}
        .LAY-btns .layui-nav .layui-nav-child{top:34px;}
        .LAY-btns .layui-nav .layui-nav-bar{display: none;}
        .LAY-btns .layui-nav .layui-nav-child dd.layui-this a{color:#333;background-color:#fff;}
        .LAY-btns .layui-nav .layui-nav-child dd.layui-this a:hover {background-color: #f2f2f2;color: #000;}
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="LAY-btns" style="margin-bottom: 10px;">
                        <button class="layui-nav" data-permission="goods.add_goods_inventory">
                            <div class="layui-nav-item">
                                <a href="javascript:;" style="color:#fff;">添加</a>
                                <dl class="layui-nav-child"> <!-- 二级菜单 -->
                                    <dd><a href="#" id="btn_add_py" >盘盈</a></dd>
                                    <dd><a href="#" id="btn_add_pk">盘亏</a></dd>
                                </dl>
                            </div>
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_print" data-permission="goods.print_goods_inventory"><i class="layui-icon layui-icon-form"></i>打印</button>
                        <button class="layui-nav" data-permission="goods.export_goods_inventory">
                            <div class="layui-nav-item">
                                <a href="javascript:;" style="color:#fff;">导出</a>
                                <dl class="layui-nav-child"> <!-- 二级菜单 -->
                                    <dd><a href="#" id="btn_download" >导出列表</a></dd>
                                    <dd><a href="#" id="btn_download_detail">导出明细</a></dd>
                                </dl>
                            </div>
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_query"><i class="layui-icon layui-icon-search"></i>查询</button>
                    </div>

                    <table class="layui-hide" id="datagrid" lay-filter="datagrid-operate"></table>

                    <script type="text/html" id="datagrid-operate-bar">
                        <div class="layui-btn-group">
                            <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
                            {{#  if(d.check_status ==0){ }}
                            <a class="layui-btn layui-btn-xs" lay-event="edit" data-permission="goods.add_goods_inventory">修改</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" data-permission="goods.delete_goods_inventory">删除</a>
                            <a class="layui-btn layui-btn-xs" lay-event="check" data-permission="goods.check_goods_inventory">审核</a>
                            {{#  } }}
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="dlg_query" style="display: none">
    <div class="layui-card-body" pad15>
        <form class="layui-form" lay-filter="query-form-element">
            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">创建时间：</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" autocomplete="off" name="create_time" id="create_time" placeholder=" - ">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">盘存类别：</label>
                    <div class="layui-input-block">
                        <select name="type">
                            <option value="" selected></option>
                            <option value="0">盘盈</option>
                            <option value="1">盘亏</option>
                        </select>
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">产品名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="product_name" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">产品代码：</label>
                    <div class="layui-input-block">
                        <input type="text" name="product_model" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">盘存单号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="no" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">产品备注：</label>
                    <div class="layui-input-block">
                        <input type="text" name="product_notes" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="display: none">
                <button id="query_search" class="layui-btn" lay-submit lay-filter="query-form-element">查询</button>
            </div>
        </form>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js?t=1"></script>
<script>
    var _params = '';
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'laydate', 'form', 'utils'], function(){
        var $ = layui.$;
        var table = layui.table
            ,laydate = layui.laydate
            ,form = layui.form
            ,utils = layui.utils
            ,admin = layui.admin;
        var is_has = utils.isHasPermissions('goods.view_goods_cost');
        var cols = [];
        cols.push({type:'checkbox',width:50});
        cols.push({field:'no', title:'盘存单号', width:160});
        cols.push({field:'type_text', title:'类别', width:80});
        cols.push({field:'total_count', title:'合计数量', align:'right', width:100});
        if (is_has){
            cols.push({field:'total_amount', title:'合计金额', align:'right', width:130});
        }
        cols.push({field:'warehouse_text', title:'仓别', width:120});
        cols.push({field:'create_time', title:'创建时间', width:150});
        cols.push({field:'create_user_text', title:'创建人', width: 120});
        cols.push({field:'check_status_text', title:'审核状态', width:100});
        cols.push({field:'check_time', title:'审核时间', width:150});
        cols.push({field:'check_user_text', title:'审核人', width:80});
        cols.push({field:'notes', title:'备注', minWidth:200});
        cols.push({width:170, align:'left', fixed: 'right', toolbar: '#datagrid-operate-bar'});
        table.render({
            elem: '#datagrid'
            ,url: '/material/inventory/data/?product_type=2'
            ,cols: [cols]
            ,page: true
            ,totalRow: true
            ,height: 'full-104'
            ,parseData:function(res) {
                var cols = this.cols[0];
                for (var i in cols) {
                    if (cols[i].field == 'total_count') {
                        cols[i].totalRowText = res.more.sum_count;
                    }

                    if (cols[i].field == 'total_amount') {
                        cols[i].totalRowText = res.more.sum_amount;
                    }

                }
                return {
                    "code": res.code, //解析接口状态
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            ,done: function () {
                layui.index.removeNoPermButtons()
            }
        });

        //监听工具条
        table.on('tool(datagrid-operate)', function(obj){
            var data = obj.data;
            table.editdata = data;
            if(obj.event === 'detail'){
                layer.open({
                    type: 2,
                    title: '查看',
                    shadeClose: true,
                    area: ['90%', '60%'],
                    content: '../material/inventory_details.html?id='+data.id
                });
            } else if(obj.event === 'del'){
                if (data.check_status == 1){
                    layer.msg('该盘存单已审核,不允许删除');
                    return
                }
                layer.confirm('确定要删除吗？', function(index){
                    layer.close(index);
                    layui.admin.req({
                        url: '/material/inventory/delete/?id='+data.id
                        ,done: function(res){
                            table.reload('datagrid',{});
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                if (data.check_status == 1){
                    layer.msg('该盘存单已审核, 不允许修改');
                    return
                }
                table.editdata = data;
                if (data.type == 1) {
                    var content = 'goods_loss_inventory_edit.html?id='+data.id + '&type=2'
                }else{
                    content = 'goods_inventory_edit.html?id='+data.id + '&type=2'
                }
                layer.open({
                    type: 2,
                    title: '修改',
                    shadeClose: false,
                    area: ['90%', '90%'],
                    btn: ['保存', '取消'],
                    yes: function(index, dom){
                        layui.onSubmitChild = function (data) {
                            layer.close(index);
                            table.reload('datagrid',{});
                        };
                        layui.submitChild();
                    },
                    btn2: function(index, layero){
                        layer.close(index);//关闭当前按钮
                    },
                    content: content
                });
            }else if(obj.event === 'check'){
                if (data.check_status == 1){
                    layer.msg('该盘存单已审核');
                    return
                }
                layer.confirm('确定要审核吗？', function(index){
                    layer.close(index);
                    layui.admin.req({
                        url: '/material/inventory/check/?id='+data.id
                        ,done: function(res){
                            table.reload('datagrid',{});
                        }
                    });
                });
            }
        });
        $('#btn_download').on('click', function(){
            layui.admin.req({
                url: '/material/inventory/export/?product_type=2',
                data:_params,
                done: function (res) {
                    layui.view.download(res.data.filename);
                }
            });
        });
        $('#btn_download_detail').on('click', function(){
            var data = utils.getSelectedRow('datagrid');
            if(data == false){
                return;
            }


            layui.admin.req({
                url: '/material/inventory/detail_export/?id=' + data.id,
                done: function (res) {
                    layui.view.download(res.data.filename);
                }
            });
        });
        $('#btn_add_py').on('click', function(){
            layer.open({
                type: 2,
                title: '添加盘盈单',
                shadeClose: false,
                area: ['90%', '90%'],
                btn: ['保存', '取消'],
                yes: function(index, dom){
                    layui.onSubmitChild = function (data) {
                        layer.close(index);
                        table.reload('datagrid',{});
                    };
                    layui.submitChild();
                },
                btn2: function(index, layero){
                    layer.close(index);//关闭当前按钮
                },
                content: 'goods_inventory_edit.html?type=2'
            });
        });

        $('#btn_add_pk').on('click', function(){
            layer.open({
                type: 2,
                title: '添加盘亏单',
                shadeClose: false,
                area: ['90%', '90%'],
                btn: ['保存', '取消'],
                yes: function(index, dom){
                    layui.onSubmitChild = function (data) {
                        layer.close(index);
                        table.reload('datagrid',{});
                    };
                    layui.submitChild();
                },
                btn2: function(index, layero){
                    layer.close(index);//关闭当前按钮
                },
                content: 'goods_loss_inventory_edit.html?type=2'
            });
        });

        $('#btn_print').on('click', function(){
            var data = utils.getSelectedRow('datagrid');
            if(data == false){
                return;
            }
            table.editdata = data;

            layer.open({
                type: 2,
                title: '打印',
                shadeClose: true,
                area: ['90%', '90%'],
                btn: ['打印', '取消'],
                yes: function (index, dom) {
                    layui.onSubmitChild = function(data){
                        layer.close(index);
                    };
                    layui.submitChild();
                },
                btn2: function(index, layero){
                    layer.close(index);
                },
                content: '../material/inventory_print.html?id='+data.id
            });
        });

        $('#btn_query').on('click', function(){
            layer.open({
                type: 1,
                shadeClose: true,
                area: ['500px', '400px'],
                title: '查询',
                btn: ['查询'],
                yes: function(index, dom){
                    $('#query_search').click();
                },
                content: $('#dlg_query')
            });
        });
        form.on('submit(query-form-element)', function(data){
            //layer.msg(JSON.stringify(data.field));
            _params = data.field;
            table.reload('datagrid', {
                where: data.field
                ,page:{curr:1}
            });
            layer.closeAll();
            return false
        });

        laydate.render({
            elem: '#create_time'
            ,range: true
        });
    });
</script>
</body>
</html>

