<!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;}
    .layui-form-select dl{max-height: 150px;}
  </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-btn layui-btn-sm" id="btn_export" data-permission="warehouse.export_consumable_stock"><i class="layui-icon layui-icon-download-circle"></i>导出</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" data-permission="warehouse.view_consumable_stock_log">查看动态</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="last_deliver_time" id="id_last_deliver_time" placeholder=" - ">
                </div>
            </div>
            <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="last_entry_time" id="id_last_entry_time" placeholder=" - ">
                </div>
            </div>

            <div class="layui-col-xs12 layui-col-sm12">
                <label class="layui-form-label">仓别：</label>
                <div class="layui-input-block">
                    <select id="id_warehouse" name="warehouse">
                      <option value="">选择仓别</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-sm12">
                <label class="layui-form-label">在用：</label>
                <div class="layui-input-block">
                    <select name="product_enabled">
                        <option value="" selected></option>
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-sm12">
                <label class="layui-form-label">库存状态：</label>
                <div class="layui-input-block">
                    <select name="empty">
                        <option value="" ></option>
                        <option value="1" selected>有库存</option>
                        <option value="0">零库存</option>
                        <option value='2'>超储</option>
                        <option value='3'>短缺</option>
                        <option value='4'>负库存</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_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="warehouse_place" 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" class="layui-input" autocomplete="off" name="stock_time" id="id_stock_time" placeholder=" - ">
                </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',
    utils:'utils'
  }).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 warehosue_option = '';
    admin.req({
        url: '/warehouse/all_select/?type=1',
        done: function(res){
            var warehouses = res.data;
            for (var i in warehouses) {
                 warehosue_option += "<option value="+ warehouses[i].id+">"+ warehouses[i].name+"</option>";
            }
            $("#id_warehouse").append(warehosue_option);
            form.render();
        }
    });
    var is_has = utils.isHasPermissions('material.view_consumable_cost');
    var cols = [];
    cols.push({field:'product_name', title:'名称', width:100});
    cols.push({field:'product_model', title:'代码', width:100});
    cols.push({field:'option_type_text', title:'类别', width:100});
    cols.push({field:'product_unit', title:'单位', width:100});
    cols.push({field:'product_standard', title:'规格', width:100});
    cols.push({field:'stock', title:'库存',align:'right', width:100});
    cols.push({field:'warehouse_text', title:'仓别', width:100});
    cols.push({field:'warehouse_place', title:'存放库位', width: 120});
    if (is_has){
        cols.push({field:'avg_cost_price', title:'成本价', align:'right', width:120});
        cols.push({field:'warehouse_amount', title:'库存金额', align:'right', width:100});
    }

    cols.push({field:'product_code', title:'助记码', width:100});
    cols.push({field:'last_entry_time', title:'最后入库时间', width:150});
    cols.push({field:'last_deliverd_time', title:'最后出库时间', width:150});
    cols.push({field:'days', title:'未出库天数', align:'right', width:100});
    cols.push({width:100, align:'left', fixed: 'right', toolbar: '#datagrid-operate-bar'});

    table.render({
      elem: '#datagrid'
      ,url: '/warehouse/stock/data/?type=1&empty=1'
      ,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 == 'stock') {
                    cols[i].totalRowText = res.more.stock_count;
                }

                if (cols[i].field == 'warehouse_amount') {
                    cols[i].totalRowText = res.more.warehouse_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: '[' + data.product_name + '-' + data.product_model + ']库存动态',
          shadeClose: true,
          area: ['90%', '60%'],
          content: 'stock_log.html'
        });
      }
    });
    $('#btn_export').on('click', function(){
        layui.admin.req({
            url: '/warehouse/stock/export/?type=1',
            data:_params,
            done: function (res) {
                  layui.view.download(res.data.filename);
            }
        });
    });

    $('#btn_query').on('click', function(){
        layer.open({
          type: 1,
          shadeClose: true,
          area: ['600px', '500px'],
          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: '#id_stock_time'
        ,type: 'datetime'
    });
    laydate.render({
        elem: '#id_last_deliver_time'
        ,range: true
    });
    laydate.render({
        elem: '#id_last_entry_time'
        ,range: true
    });
  });
  </script>
</body>
</html>

