<!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>
<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; z-index: 999;}
    .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-nav" data-permission="purchase.add_purchase_order">
                <div class="layui-nav-item">
                  <a href="javascript:;" style="color:#fff;">添加</a>
                  <dl class="layui-nav-child"> <!-- 二级菜单 -->
                    <dd><a href="#" id="btn_add_ma">原料</a></dd>
                    <dd><a href="#" id="btn_add_con">耗材</a></dd>
                  </dl>
                </div>
              </button>
                <button class="layui-nav" data-permission="purchase.export_purchase_order">
                <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_entry" data-permission="purchase.add_material_godown_entry|purchase.add_consumable_godown_entry"><i class="layui-icon layui-icon-form"></i>到货</button>
              <button class="layui-btn layui-btn-sm" id="btn_print" data-permission="purchase.print_purchase_order"><i class="layui-icon layui-icon-form"></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="purchase.view_purchase_order">查看</a>
                  {{#  if(d.status ==0){ }}
                  <a class="layui-btn layui-btn-xs" lay-event="edit" data-permission="purchase.edit_purchase_order">修改</a>
                  <a class="layui-btn layui-btn-xs" lay-event="senior" data-permission="purchase.senior_purchase_order">高级修改</a>
                  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" data-permission="purchase.delete_purchase_order">删除</a>
                  <a class="layui-btn layui-btn-xs" lay-event="check_pass" data-permission="purchase.check_purchase_order">审核</a>
                  {{#  } }}
                  {{#  if(d.status ==1){ }}
                  <a class="layui-btn layui-btn-xs" lay-event="check_revoke" data-permission="purchase.check_purchase_order">撤销审核</a>
                    {{#  } }}
                </div>
            </script>

        </div>
      </div>
    </div>
  </div>
</div>

  <div id="dlg_query" style="display: none">
      <form class="layui-form" lay-filter="query-form-element">
    <div class="layui-card-body" pad15>
        <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-inline">
                 <input type="text" class="layui-input" 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="arrval">
                        <option value="" selected></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">
                    <select id="id_check_status" name="status">
                        <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="supplier_name" 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" 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="notes" 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>
    </div>
      </form>
   </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', 'element', 'utils'], function(){
    var $ = layui.$;
    var table = layui.table
        ,laydate = layui.laydate
        ,form = layui.form
        ,utils = layui.utils
        ,admin = layui.admin;

    table.render({
      elem: '#datagrid'
      ,url: '/purchase/purchase_order/data/'
      ,where: _params
      ,cols: [[
          {type:'checkbox',width:50, fixed: 'left'}
        ,{field:'order_no', title:'单号', width:150, fixed: 'left'}
        ,{field:'no', title:'合同号', width:150, fixed: 'left'}
        ,{field:'supplier_name', title:'供应商', width:120, fixed: 'left'}
        ,{field:'products', title:'产品', width:120, fixed: 'left'}
        ,{field:'payment_type', title:'付款方式', width:120}
        ,{field:'count', align:'right', title:'合计数量', width:100}
        ,{field:'amount', align:'right', title:'合计金额', width:100}
        ,{field:'apply_amount', align:'right', title:'已申请金额', width:100}
        ,{field:'status_text', title:'状态', width:100}
        ,{field:'arrval_text', title:'到货状态', width:100}
        ,{field:'entries', title:'批次', width:140}
        ,{field:'create_time', title:'创建时间', width:150}
        ,{field:'create_user_text', title:'创建人', width:90}
        ,{field:'check_time', title:'审核时间', width:150}
        ,{field:'check_user_text', title:'审核人', width:90}
        ,{field:'notes', title:'备注', minWidth:200}
        ,{field:'deliver_time', title:'交货时间', minWidth:200}
        ,{width:250, 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 == 'count') {
                cols[i].totalRowText = res.more.sum_count;
            }

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

        }
        return {
          "code": res.code, //解析接口状态
          "count": res.count, //解析数据长度
          "data": res.data //解析数据列表
        };
      }
       ,done: function (res, curr, count) {
          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%', '90%'],
          content: 'purchase_order_detail.html?id='+data.id
        });
      } else if(obj.event === 'del'){
          if (data.status != 0){
            layer.msg('该合同已审核,不允许删除');
            return;
          }
        layer.confirm('确定要删除吗？', function(index){
          layer.close(index);
          layui.admin.req({
            url: '/purchase/purchase_order/delete/?id='+data.id
            ,done: function(res){
              table.reload('datagrid',{});
            }
          });
          layer.close(index);
        });
      }else if(obj.event === 'edit'){
          if (data.status != 0){
            layer.msg('该订单已审核,不允许修改');
            return;
        }
        table.editdata = data;
        layer.open({
          type: 2,
          title: '修改',
          area: ['90%', '70%'],
          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_order_edit.html?id='+data.id+ '&type=' + data.supplier_type
        });
      } else if(obj.event === 'senior'){
          if (data.status != 0){
            layer.msg('该订单已审核,不允许修改');
            return;
        }
        table.editdata = data;
        layer.open({
          type: 2,
          title: '高级修改',
          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: 'purchase_order_senior.html?id='+data.id+ '&type=' + data.supplier_type
        });
      } else if(obj.event === 'check_pass'){
          if (data.status !== 0){
            layer.msg('该合同已审核, 禁止重复操作');
            return;
          }
          if (!data.no){
            layer.msg('该合同尚未填写合同号, 不允许审核');
            return;
          }
          layer.confirm('确定要通过审核吗？', function(index){
              layer.close(index);
              layui.admin.req({
                url: '/purchase/purchase_order/check/?id='+data.id + '&status=1'
                ,done: function(res){
                  table.reload('datagrid',{});
                }
              });
              layer.close(index);
        });
      }else if(obj.event === 'check_revoke'){
          if (data.status == 0){
            layer.msg('该合同尚未审核');
            return;
          }
          if (data.arrval != 0){
            layer.msg('该合同已到货, 不允许撤销审核');
            return;
          }
          if (data.entries){
            layer.msg('该合同已到货, 不允许撤销审核');
            return;
          }
          if (data.is_pay == 1){
            layer.msg('该合同已存在付款单, 不允许撤销审核');
            return;
          }
        layer.confirm('确定要撤销审核吗？', function(index){
              layer.close(index);
              layui.admin.req({
                url: '/purchase/purchase_order/check/?id='+data.id + '&status=0'
                ,done: function(res){
                  table.reload('datagrid',{});
                }
              });
              layer.close(index);
        });
      }
    });

    $('#btn_add_ma').on('click', function(){
        layer.open({
          type: 2,
          title: '添加采购合同',
          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: 'purchase_order_senior.html?type=0'
        });
    });
    $('#btn_add_con').on('click', function(){
        layer.open({
          type: 2,
          title: '添加采购合同',
          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: 'purchase_order_senior.html?type=1'
        });
    });
    $('#btn_download').on('click', function(){
        layui.admin.req({
            url: '/purchase/purchase_order/export/',
            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: '/purchase/purchase_order/export_detail/?id=' + data.id,
            done: function (res) {
                  layui.view.download(res.data.filename);
            }
        });
    });

    $('#btn_entry').on('click', function(){
         var data = utils.getSelectedRow('datagrid');
         if(data == false){
             return;
         }
         if (data.status == 0){
            layer.msg('该合同尚未审核');
            return;
         }
         if (data.arrval == 2){
            layer.msg('该合同已全部到货');
            return;
         }
         table.editdata = data;

         var permissions = layui.data(layui.setter.tableName)['permissions'];
         var edit_html = '';
         if (data.supplier_type == 0){
             edit_html = 'material_godownentry_edit.html?order_id=';
             if(permissions.indexOf('purchase.add_material_godown_entry') == -1){
                 layer.msg('您没有添加原料入库的权限，请联系管理员');
                return;
             }
         }else if(data.supplier_type == 1) {
             edit_html = 'consumable_godownentry_edit.html?order_id=';
             if(permissions.indexOf('purchase.add_consumable_godown_entry') == -1){
                 layer.msg('您没有添加耗材入库的权限，请联系管理员');
                return;
             }
         }

         layer.open({
          type: 2,
          title: '到货',
          shadeClose: false,
          area: ['80%', '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: edit_html + data.id
        });
    });

  $('#btn_print').on('click', function(){
         var data = utils.getSelectedRow('datagrid');
         if(data == false){
             return;
         }
         if (data.status == 0){
            layer.msg('该合同尚未审核');
            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: 'purchase_order_print.html?id='+data.id
        });
    });

   $('#btn_query').on('click', function(){
        layer.open({
          type: 1,
          shadeClose: true,
          area: ['500px', '450px'],
          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>

