<!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">
</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_department_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="dataTable" lay-filter="dataTable"></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_product_type" name="product_type">
                      <option value="">全部</option>
                      <option value="0">原料</option>
                      <option value="1">耗材</option>
                      <option value="2">成品</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 _departments = [];
    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;

        var title1 = [
            {title: '序号', type: 'numbers', rowspan: 2},
            {field: 'product_name', title: '产品名称', width: 150, rowspan: 2},
            {field: 'product_model', title: '产品代码', width: 150, rowspan: 2},
            {field: 'type_text', title: '类别', width: 80, rowspan: 2}
        ];

        var title2 = [];
        admin.req({
          url: '/account/select_department/',
          done: function(res){
            _departments = res.data;
            var data = res.data;
            for (var i in data) {
                title1.push({align: 'center', title: data[i].name, colspan: 2});
                title2.push({field: data[i].id+'_count', align:'right', title: '数量', width: 80});
                title2.push({field: data[i].id+'_amount', align:'right', title: '金额', width: 130});
            }
            _data_list=[title1,title2];

            rendTable();
         }
        });

        var layTableId = "dataTable";
        var tableIns = null;

        var rendTable =function () {
            tableIns = table.render({
              elem: '#dataTable'
              ,id:layTableId
              ,url: '/statistics/department_ledger/data/'
              ,cols: _data_list
              ,page: true
              ,totalRow: true
              ,parseData:function(res) {
                var cols = this.cols[1];
                for (var i in cols) {
                    for (var k in _departments) {
                        var key_count = _departments[k].id + '_count';
                        var key_amount = _departments[k].id + '_amount';
                         if (cols[i].field == key_count) {
                            cols[i].totalRowText = res.more[key_count];
                         }

                         if (cols[i].field == key_amount) {
                            cols[i].totalRowText = res.more[key_amount];
                         }
                    }
                }
                return {
                  "code": res.code, //解析接口状态
                  "count": res.count, //解析数据长度
                  "data": res.data //解析数据列表
                };
              }
              ,height: 'full-104'
              ,done: function () {
                layui.index.removeNoPermButtons();
              }
            });
        };



        $('#btn_export').on('click', function(){
            layui.admin.req({
                url: '/statistics/department_ledger/export/',
                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('dataTable', {
              where: data.field
              ,page:{curr:1}
          });
          layer.closeAll();
          return false
        });

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