<!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">
  <link rel="stylesheet" type="text/css" href="../../layuiadmin/style/formSelects-v4.css"/>
    <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;}
    .seach_items {float:right;margin-left: 10px;}
  </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;">
              <div class="layui-col-xs12">
                  <div style="float: left">
                      <button class="layui-btn" id="btn_download" data-permission="customer.export_customer"><i class="layui-icon layui-icon-download-circle"></i>导出</button>
                  </div>
                    <form class="layui-form" lay-filter="query-form-element">
                        <div class="seach_items">
                            <button class="layui-btn" lay-submit lay-filter="query-form-element"><i class="layui-icon layui-icon-search"></i>查询</button>
                        </div>
                        <div class="seach_items">
                            <input type="text"  name="tel" autocomplete="off" class="layui-input" placeholder="客户电话"/>
                        </div>
                        <div class="seach_items">
                            <input type="text"  name="name" autocomplete="off" class="layui-input" placeholder="客户姓名"/>
                        </div>
                        <div class="seach_items">
                            <div class="layui-inline">
                              <div class="layui-input-inline">
                                <input type="text" name="range_time" class="layui-input" id="range_time" placeholder="注册时间">
                              </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div style="clear: both;"></div>
            </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="customer.edit_customer">修改</a>
                    {{# if(!d.is_stop){ }}
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">停止佣金计提</a>
                    {{# } }}
                    {{# if(d.is_stop){ }}
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="recovery">恢复佣金计提</a>
                    {{# } }}
                </div>
            </script>
          </div>
        </div>
        </div>
    </div>
  </div>

  <script src="../../layuiadmin/layui/layui.js?t=1"></script>
  <script>
  var _params = '';
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
     ,formSelects: 'formSelects-v4'
  }).use(['index', 'table', 'form', 'formSelects', 'upload', 'laydate'], function(){
    var $ = layui.$
            ,table = layui.table
            ,laydate = layui.laydate
            ,form = layui.form;
    table.render({
      elem: '#datagrid'
      ,url: '/tenant/customer/'
      ,title: '客户信息'
      ,id: 'datagrid'
      ,cols: [[
        {field:'name', title:'名称',width: 100}
       ,{field:'tel', title:'电话',width: 150}
       ,{field:'gender_text', title:'性别',width: 80}
       ,{field:'date_join', title:'注册时间',width: 150}
       ,{field:'commission_balance', align: 'right', title:'佣金余额',width: 100}
       ,{field:'points', align:'right', title:'积分数', width:80}
       ,{field:'recommend_count', align: 'right', title:'推荐数',width: 80}
       ,{field:'success_count', align: 'right', title:'成交数',width: 80}
       ,{field:'order_count', align: 'right', title:'订单数',width: 80}
       ,{field:'collection_count', align: 'right', title:'收藏数',width: 80}
        ,{width:150, align:'center', fixed: 'right', toolbar: '#datagrid-operate-bar'}
      ]]
      ,totalRow:true
      ,parseData:function(res) {
        if(res.code === 1){
            return;
        }
        var cols = this.cols[0];
        for (var i in cols) {
            if (cols[i].field === 'commission_balance') {
                cols[i].totalRowText = res.data.footer.total_commission;
            }
            if (cols[i].field === 'recommend_count') {
                cols[i].totalRowText = res.data.footer.total_recommend
            }
            if (cols[i].field === 'success_count') {
                cols[i].totalRowText = res.data.footer.total_success
            }
            if (cols[i].field === 'order_count') {
                cols[i].totalRowText = res.data.footer.total_order
            }
            if (cols[i].field === 'collection_count') {
                cols[i].totalRowText = res.data.footer.total_collection
            }
            if (cols[i].field === 'points') {
                cols[i].totalRowText = res.data.footer.total_points
            }
        }
        return {
          "code": res.code, //解析接口状态
          "count": res.count, //解析数据长度
          "data": res.data.data //解析数据列表
        };
      }
      , done: function () {
        layui.index.removeNoPermButtons()
      }
      ,page: true
      ,height: 'full-108'
    });
    laydate.render({
        elem: '#range_time'
        ,range:true
    });
    form.on('submit(query-form-element)', function(data){
      //layer.msg(JSON.stringify(data.field));
      table.reload('datagrid', {
          where: data.field
          ,page:{curr:1}
      });
      _params = data.field;
      layer.closeAll();
      return false;
    });
    //监听工具条
    table.on('tool(datagrid-operate)', function(obj){
      var data = obj.data;
        if(obj.event === 'edit'){
        table.editdata = data;
        layer.open({
          type: 2,
          title: '修改',
          shadeClose: false,
          area: ['500px', '500px'],
          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?id='+data.id
        });
      }else if(obj.event === 'stop'){
            // 暂停该账号的一些功能
            if (data.is_stop){
                layer.msg('该客户功能已被暂停，禁止重复操作！');
                return;
            }

            layer.confirm('确定要暂停该客户功能吗？', function(index){
                layer.close(index);
                layui.admin.req({
                    notice: true
                    ,url: '/tenant/customer/'+data.id + '/stop/'
                    ,data: {}
                    ,type: 'post'
                    ,done: function(res){
                        table.reload('datagrid',{});
                    }
                });
            });
        }else if(obj.event === 'recovery'){
            // 恢复该客户被暂停的功能
            if (!data.is_stop){
                layer.msg('该客户功能未被暂停，禁止操作！');
                return;
            }

            layer.confirm('确定要恢复该客户功能吗？', function(index){
                layer.close(index);
                layui.admin.req({
                    notice: true
                    ,url: '/tenant/customer/'+data.id + '/recovery/'
                    ,data: {}
                    ,type: 'post'
                    ,done: function(res){
                        table.reload('datagrid',{});
                    }
                });
            });
        }
    });
    // 导出数据
    $('#btn_download').on('click', function(){
        $.get({
            url: '/tenant/customer/export/',
            dataType: 'json',
            data: _params,
            success: function (res) {
                if(res.code === 1){
                    layer.msg(res.msg);
                    return;
                }
                table.exportFile('datagrid', res.data, 'xlsx')
            }
        })
    });
  });
  </script>
</body>
</html>

