<!DOCTYPE html>
<html lang="en">
<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>
<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>
<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" data-permission="purchase.add_purchase_invoice" id="btn_add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
                        <button class="layui-btn layui-btn-sm" data-permission="purchase.export_purchase_invoice" id="btn_export"><i class="layui-icon layui-icon-download-circle"></i>导出</button>
                        <button class="layui-btn layui-btn-sm" id="id_upload" data-permission="purchase.add_purchase_invoice"><i class="layui-icon layui-icon-upload-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">
                        {{#  if(d.images){ }}
                      <a class="layui-btn layui-btn-xs" id="images" lay-event="images">查看图片</a>
                      {{#  } }}
                      {{#  if(d.check_status ==0){ }}
                      <a class="layui-btn layui-btn-xs" data-permission="purchase.check_purchase_invoice" id="check" lay-event="check">审核</a>
                      {{#  } }}
                      {{#  if(d.check_status ==1){ }}
                      <a class="layui-btn layui-btn-xs" data-permission="purchase.check_purchase_invoice" id="checkout" lay-event="checkout">撤销审核</a>
                      {{#  } }}
                    </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">
                        <select id="id_invoice" name="invoice">
                            <option value="">全部</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">
                        <select id="id_status" name="check_status">
                            <option value="">全部</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="order_no" 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="supplier_name" class="layui-input">
                    </div>
                </div>
               </div>
              <div class="layui-form-item" style="display: none">
                    <button class="layui-btn" lay-submit lay-filter="query-form-element" id="query_btn">查询</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', 'upload', 'utils'], function () {
            var $ = layui.$;
            var table = layui.table
                ,laydate = layui.laydate
                ,upload = layui.upload
                ,utils = layui.utils
                ,form = layui.form;

            table.render({
                elem: '#datagrid'
                ,url: '/purchase/purchase_invoice/data/'
              ,cols: [[
                {type:'checkbox',width:50}
                ,{field:'order_no', title:'合同号', width:120}
                ,{field:'name', title:'产品名称', width:120}
                ,{field:'model', title:'产品代码', width:120}
                ,{field:'supplier_name', title:'供应商', width:100}
                ,{field:'amount', align:'right', title:'金额', width:130}
                ,{field:'invoice_no', title:'发票号', width:80}
                ,{field:'invoice_amount', align:'right', title:'发票金额', width:130}
                ,{field:'invoice_date', title:'开票日期', width:150}
                ,{field:'create_time', title:'创建时间', width:150}
                ,{field:'create_user_text', title:'创建人', width:100}
                ,{field:'check_status_text', title:'审核状态', width: 100}
                ,{field:'check_time', title:'审核时间', width:150}
                ,{field:'check_user_text', title:'审核人', width: 100}
                ,{width:170, align:'left', fixed: 'right', toolbar: '#datagrid-operate-bar'}
              ]]
              ,page: true
              ,totalRow: true
              ,height: 'full-104'
              ,parseData:function(res) {
                var cols = this.cols[0];
                for (var i in cols) {
                    if (cols[i].field == 'amount') {
                        cols[i].totalRowText = res.more.sum_amount;
                    }
                    if (cols[i].field == 'invoice_amount') {
                        cols[i].totalRowText = res.more.sum_invoice_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;
              if(obj.event === 'check'){
                   if (data.check_status == 1){
                        layer.msg('发票已审核,禁止重复操作');
                        return false;
                    }
                    if (!data.invoice_no){
                        layer.msg('尚未录入发票');
                        return false;
                    }
                layer.confirm('确定要审核吗？', function(index){
                    layer.close(index);
                    layui.admin.req({
                        url: '/purchase/purchase_invoice/check/?status=1&id='+data.id
                        ,done: function(res){
                            table.reload('datagrid',{});
                        }
                    });
                });
              }else if(obj.event === 'checkout'){
                  if (data.check_status == 0){
                      layer.msg('该单据尚未审核');
                      return false;
                  }
                  layer.confirm('确定要撤销审核吗？', function(index){
                      layer.close(index);
                      layui.admin.req({
                         url: '/purchase/purchase_invoice/check/?status=0&id='+data.id
                         ,done: function(res){
                             table.reload('datagrid',{});
                         }
                      });
                  });
              }else if(obj.event === 'images'){
                  layer.open({
                  type: 2,
                  title: '查看图片',
                  area: ['90%', '90%'],
                  content: 'purchase_invoice_images.html?id=' + data.id
                });
              }
            });

            $('#id_upload').on('click', function(){
                var checkStatus = table.checkStatus('datagrid')
                 ,data = checkStatus.data;
                if (data.length == 0) {
                     layer.msg('请至少选择一条记录');
                     return false;
                 }
                var ids = [];
                 for (var i in data) {
                     if (data[i].check_status == 1){
                        layer.msg('合同'+data[i].order_no+'已生效, 不可执行此操作');
                        return false;
                     }
                     ids.push(data[i].id)
                 }
                 layer.open({
                  type: 2,
                  title: '上传',
                  area: ['600px', '450px'],
                  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: 'purchase_invoice_upload.html?ids=' + ids
                });
            });

            $('#btn_export').on('click', function(){
                layui.admin.req({
                    url: '/purchase/purchase_invoice/export/',
                    data:_params,
                    done: function (res) {
                          layui.view.download(res.data.filename);
                    }
                });
            });
            $('#btn_add').on('click', function(){
                var checkStatus = table.checkStatus('datagrid')
                 ,data = checkStatus.data;
                if (data.length == 0) {
                     layer.msg('请至少选择一条记录');
                     return false;
                 }
                 table.editdata = data;
                layer.open({
                  type: 2,
                  title: '添加',
                  area: ['600px', '450px'],
                  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: 'purchase_invoice_edit.html'
                });
            });
            $('#btn_query').on('click', function(){
                layer.open({
                  type: 1,
                  shadeClose: true,
                  area: ['500px', '400px'],
                  title: '查询',
                  btn: ['查询'],
                  yes: function(index, dom){
                     $('#query_btn').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>
