<!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;}
  </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_add" data-permission="supplier.add_supplier"><i class="layui-icon layui-icon-add-circle"></i>添加</button>

              <button class="layui-nav" data-permission="supplier.import_supplier">
                <div class="layui-nav-item">
                  <a href="javascript:;" style="color:#fff;">导入</a>
                  <dl class="layui-nav-child"> <!-- 二级菜单 -->
                    <dd><a href="#" id="btn_import">执行导入</a></dd>
                    <dd><a href="#" id="btn_download">下载模板</a></dd>
                  </dl>
                </div>
              </button>

              <button class="layui-btn layui-btn-sm" id="btn_export" data-permission="supplier.export_supplier"><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="edit" data-permission="supplier.add_supplier">修改</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" data-permission="supplier.delete_supplier">删除</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" name="name" class="layui-input" autocomplete="off">
            </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="contacts" class="layui-input" autocomplete="off">
            </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="phone_number" class="layui-input" autocomplete="off">
            </div>
          </div>
        </div>
        <div style="display: none">
          <button id="query-submit" lay-submit lay-filter="query-form-element">查询</button>
        </div>
      </form>
    </div>
  </div>

  <script src="../../layuiadmin/layui/layui.js?t=1"></script>
  <script>
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','element', 'table', 'form', 'upload'], function(){
    var $ = layui.$;
    var table = layui.table
    ,form = layui.form
    ,upload = layui.upload;

    table.render({
      elem: '#datagrid'
      ,url: '/supplier/supplier/data/'
      ,cols: [[
        {field:'name', title:'名称', width:150}
        ,{field:'type_text', title:'类别', width:80}
        ,{field:'contacts', title:'联系人', width:80}
        ,{field:'phone_number', title:'电话', width:120}
        ,{field:'address', title:'地址', width:200}
        ,{field:'opening_bank', title:'开户行', width:100}
        ,{field:'account', title:'账号', width:140}
        ,{field:'tax_number', title:'税号', width:140}
        ,{field:'credit_code', title:'信用代码', width:100}
        ,{field:'enabled_text', title:'可用', width:80}
        ,{field:'notes', title:'备注', minWidth: 150}
        ,{width:100, align:'left', fixed: 'right', toolbar: '#datagrid-operate-bar'}
      ]]
      ,page: true
      ,height: 'full-104'
      ,done: function () {
        layui.index.removeNoPermButtons();
      }
    });

    //监听工具条
    table.on('tool(datagrid-operate)', function(obj){
      var data = obj.data;
      if(obj.event === 'detail'){
        layer.msg('ID：'+ data.id + ' 的查看操作');
      } else if(obj.event === 'del'){
        layer.confirm('确定要删除吗？', function(index){
          layer.close(index);
          layui.admin.req({
            url: '/supplier/supplier/delete/?id='+data.id
            ,done: function(res){
              table.reload('datagrid',{});
              //obj.del();
            }
          });
        });
      } else if(obj.event === 'edit'){
        table.editdata = data;
        //layer.alert('编辑行：<br>'+ JSON.stringify(data))
        layer.open({
          type: 2,
          title: '修改',
          shadeClose: false,
          area: ['700px', '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: 'supplier_edit.html?id=' + data.id
        });
      }
    });

    $('#btn_add').on('click', function(){
        layer.open({
          type: 2,
          title: '添加供应商',
          shadeClose: false,
          area: ['700px', '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: 'supplier_edit.html'
        });
    });

    $('#btn_query').on('click', function () {
      layer.open({
        type: 1,
        shadeClose: true,
        area: ['520px', '290px'],
        title: '查询',
        btn: ['查询'],
        yes: function (index, dom) {
          $("#query-submit").click();
        },
        content: $('#dlg_query')
      });
    });

    var _params;
    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
    });

    $('#btn_export').on('click', function(){
        layui.admin.req({
            url: '/supplier/supplier/export/'
            ,data: _params
            ,type: 'get'
            ,done: function(res){
               layui.view.download(res.data.filename);
            }
        });
    });

    $('#btn_download').on('click', function () {
      layui.view.download("/static/xls/供应商导入模板.xlsx");
    });


    upload.render({
      elem: '#btn_import'
      , url: '/supplier/supplier/import/'
      , accept: 'file'
      , acceptMime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      , exts: 'xlsx'
      , field: 'excel_file'
      , multiple: false
      , done: function (res) {
        if (res.code == 0) {
          table.reload('datagrid', {});
        } else {
          layer.msg(res.msg);
        }
      }
      , error: function () {
        layer.msg('导入失败');
      }
    });
  });
  </script>
</body>
</html>

