<!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">
        .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="product.export_consumable_stock_ledger"><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>
                </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="date" id="id_date" 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">
                    <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" style="width: 100px;">去除全部为0：</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="exclude_zero" lay-skin="switch" lay-text="是|否">
                </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"></script>
<script>
    var _data_list = [];
    var _params = '';
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'table', 'laydate'], function(){
        var $ = layui.$
        ,admin = layui.admin
        ,form = layui.form
        ,laydate = layui.laydate
        ,table = layui.table;

        admin.req({
          url: '/warehouse/all_select/?type=1',
          done: function(res){
            var data = res.data;
            var warehouse_node = $('#id_warehouse');
            for (var i in data) {
                var pid = data[i].id;
                var value = data[i].name;
                warehouse_node.append("<option value='"+pid+"'>"+value+"</option>");
            }
            form.render();
         }
        });

        var title1 = [
            {title: '序号', type: 'numbers', rowspan: 2},
            {field: 'product_name', title: '产品名称', width: 150, rowspan: 2},
            {field: 'product_model', title: '产品代码', width: 150, rowspan: 2},
            {field: 'unit', title: '单位', width: 80, rowspan: 2},
            {align: 'center', title: '期初结存', width: 100, colspan: 3},
            {align: 'center', title: '本期收入', width: 100, colspan: 9},
            {align: 'center', title: '本期发出', width: 100, colspan: 9},
            {align: 'center', title: '期末结存', width: 100, colspan: 9}
        ];

        var title2 = [
            {field: 'prev_count', align:'right', title: '数量', width: 100},
            {field: 'prev_avg', align:'right', title: '平均单价', width: 100},
            {field: 'prev_amount', align:'right', title: '金额', width: 130},
            {field: 'in_count', align:'right', title: '总数量', width: 100},
            {field: 'in_avg', align:'right', title: '平均单价', width: 100},
            {field: 'in_amount', align:'right', title: '总金额', width: 130},
            {field: 'rk_count', align:'right', title: '入库数量', width: 100},
            {field: 'rk_amount', align:'right', title: '入库金额', width: 130},
            {field: 'py_count', align:'right', title: '盘盈数量', width: 100},
            {field: 'py_amount', align:'right', title: '盘盈金额', width: 130},
            {field: 'th_count', align:'right', title: '退货数量', width: 100},
            {field: 'th_amount', align:'right', title: '退货金额', width: 130},
            {field: 'out_count', align:'right', title: '总数量', width: 100},
            {field: 'out_avg', align:'right', title: '平均单价', width: 100},
            {field: 'out_amount', align:'right', title: '总金额', width: 130},
            {field: 'ck_count', align:'right', title: '出库数量', width: 100},
            {field: 'ck_amount', align:'right', title: '出库金额', width: 130},
            {field: 'pk_count', align:'right', title: '盘亏数量', width: 100},
            {field: 'pk_amount', align:'right', title: '盘亏金额', width: 130},
            {field: 'tl_count', align:'right', title: '退料数量', width: 100},
            {field: 'tl_amount', align:'right', title: '退料金额', width: 130},
            {field: 'remain_count', align:'right', title: '数量', width: 100},
            {field: 'remain_avg', align:'right', title: '平均单价', width: 100},
            {field: 'remain_amount', align:'right', title: '金额', width: 130}
        ];
        _data_list=[title1,title2];

        table.render({
          elem: '#datagrid'
          ,url: '/warehouse/stock/ledger_data/?product_type=1'
          ,cols: _data_list
          ,page: true
          ,height: 'full-104'
            ,totalRow: true
          ,parseData:function(res) {
            var cols = this.cols[1];
            for (var i in cols) {
                if (cols[i].field == 'prev_count') {
                    cols[i].totalRowText = res.more.prev_count;
                } else if (cols[i].field == 'prev_amount') {
                    cols[i].totalRowText = res.more.prev_amount;
                } else if (cols[i].field == 'remain_count') {
                    cols[i].totalRowText = res.more.remain_count;
                }else if (cols[i].field == 'remain_amount') {
                    cols[i].totalRowText = res.more.remain_amount;
                } else if (cols[i].field == 'rk_count') {
                    cols[i].totalRowText = res.more.rk_count;
                } else if (cols[i].field == 'rk_amount') {
                    cols[i].totalRowText = res.more.rk_amount;
                } else if (cols[i].field == 'tl_count') {
                    cols[i].totalRowText = res.more.tl_count;
                } else if (cols[i].field == 'tl_amount') {
                    cols[i].totalRowText = res.more.tl_amount;
                } else if (cols[i].field == 'py_count') {
                    cols[i].totalRowText = res.more.py_count;
                } else if (cols[i].field == 'py_amount') {
                    cols[i].totalRowText = res.more.py_amount;
                }else if (cols[i].field == 'ck_count') {
                    cols[i].totalRowText = res.more.ck_count;
                }else if (cols[i].field == 'ck_amount') {
                    cols[i].totalRowText = res.more.ck_amount;
                }else if (cols[i].field == 'th_count') {
                    cols[i].totalRowText = res.more.th_count;
                }else if (cols[i].field == 'th_amount') {
                    cols[i].totalRowText = res.more.th_amount;
                }else if (cols[i].field == 'pk_count') {
                    cols[i].totalRowText = res.more.pk_count;
                }else if (cols[i].field == 'pk_amount') {
                    cols[i].totalRowText = res.more.pk_amount;
                }else if (cols[i].field == 'in_count') {
                    cols[i].totalRowText = res.more.in_count;
                }else if (cols[i].field == 'in_amount') {
                    cols[i].totalRowText = res.more.in_amount;
                }else if (cols[i].field == 'out_count') {
                    cols[i].totalRowText = res.more.out_count;
                }else if (cols[i].field == 'out_amount') {
                    cols[i].totalRowText = res.more.out_amount;
                }
            }
            return {
              "code": res.code, //解析接口状态
              "count": res.count, //解析数据长度
              "data": res.data //解析数据列表
            };
          }
          ,done: function () {
            layui.index.removeNoPermButtons();
          }
        });

        $('#btn_export').on('click', function(){
            layui.admin.req({
                url: '/warehouse/stock/ledger_export/?product_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', '400px'],
              title: '查询',
              btn: ['查询'],
              yes: function(index, dom){
                 $('#query_search').click();
              },
              content: $('#dlg_query')
            });
        });
        form.on('submit(query-form-element)', function(data){
          if (!data.field.exclude_zero) {
              data.field.exclude_zero = '';
          }
          _params = data.field;
          table.reload('datagrid', {
              where: data.field
              ,page:{curr:1}
          });
          layer.closeAll();
          return false
        });

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