$(function () {
  var ERROK = 200;
  var contextPath = $("#contextPath").val();
  var billId = $("#billId").val();
  var orgId = '';
  var api = {
    init: function () {
      this.render();
      this.bindEvent();
    },
    render: function() {
      this.itemAjax();
    },
    bindEvent: function() {
      var _this = this;
      layui.use('laydate', function() {
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
          elem: '#beginTime' //提交开始时间
        });
        laydate.render({
          elem: '#endTime' //提交结束时间
        });
        //执行一个laydate实例
        laydate.render({
          elem: '#orderTime' //提交开始时间
        });
        laydate.render({
          elem: '#endOrderTime' //提交结束时间
        });
      });
    },
    itemAjax: function () {
      let _this = this;
      _this.ajaxDetail(billId, function (data) {
        orgId = data.orgId;
        $("#org-name").val(_this.userDetailData(data.orgName));
        $("#vir-name").val(_this.userDetailData(data.virName));
        $("#dept-str").val(_this.userDetailData(data.deptStr));
        $("#mobile-deta").val(_this.userDetailData(data.mobile));
        $("#user-name").val(_this.userDetailData(data.userName));
        $("#integrale-deta").val(_this.userDetailData(data.integrale));
        $("#address-str").val(_this.userDetailData(data.addressStr));

        let strRole = '无';
        if (data.roleType == 1){
          strRole = '管理员';
        }
        if (data.roleType == 2){
          strRole = '个人账号';
        }
        if (data.roleType == 3){
          strRole = '可被代订';
        }
        $("#role-type").val(strRole);
        _this.rendertable();
      })
    },
    //空数据处理
    userDetailData: function (strData) {
      if(strData==null||strData.length==0){
        return '无'
      }
      else {
        return strData
      }
    },
    /**
     * 请求详情
     * @param id
     * @param callback
     */
    ajaxDetail: function (id, callback) {
      $.ajax({
        type: "post",
        url: contextPath + "/admin/appUser/getConsumerInfo",
        data: {
          id: id
        },
        success: function (data) {
          if (data.code == ERROK) {
            data.data.id = id;
            callback(data.data)
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    rendertable: function () {
      var _this = this;
      layui.use('table', function () {
        table = layui.table;
        //方法级渲染
        table.render({
          elem: '#LAY_table_user',
          url: contextPath + "/admin/appUser/getAllIntegralRecodeList",
          method: "post",
          skin: 'nob' //行边框风格
          ,
          even: true //开启隔行背景
          ,
          size: 'lg' //小尺寸的表格
          ,
          cols: [
            [
              {
                field: 'index',
                title: '序号',
                minWidth: '100',
                templet: '#indexTpl'
              }, {
              field: 'createTime',
              title: '时间',
              minWidth: '200'
            },  {
              field: 'opTypeStr',
              title: '类型',
              minWidth: '150'
            },  {
              field: 'exchangeIntegral',
              title: '积分',
              minWidth: '200',
              templet: function (data) {
                if (Number(data.exchangeIntegral)<0){
                  return data.exchangeIntegral;
                } else {
                  return '+'+data.exchangeIntegral;
                }
              }
            }, {
              field: 'creatorName',
              title: '操作人',
              minWidth: '150'
            }, {
              field: 'newIntegral',
              title: '最新余额',
              minWidth: '224'
            },
            ]
          ],
          id: 'integralReload',
          page: true,
          response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,
            statusCode: 200 //成功的状态码，默认：0
            ,
            countName: "count" //数据总数的字段名称，默认：count
            ,
            dataName: "data" //数据列表的字段名称，默认：data
            ,
            msgName: 'msg' //规定状态信息的字段名称，默认：msg
          },
          request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,
            limitName: 'pageSize' //每页数据量的参数名，默认：limit
          },
          where: {
            orgId:orgId,
            mobile:$("#mobile-deta").val(),
            type:'',
            beginTime:'',
            endTime:''
          },
          done: function (res, curr, count) {
            console.log('res',res);
          }
        });
        //查询
        $('#search-data').on('click', function () {
          _this.againRenderTable();
        });
        //重置
        $("#set-data").on("click", function () {
          var $dom = $("#roleType,#beginTime,#endTime");
          $dom.val("").removeAttr();

          _this.againRenderTable({type:1});
        });
        //导出Excel
        $("#export-excel").on("click", function () {
          _this.exportAjax();
        });
      });
    },
    /*重新渲染table*/
    againRenderTable: function(option) {
      var obj = $.extend(true, {}, option);
      var type =  obj.type || ''
      if(type == 1){
        table.reload('integralReload', {
          where: {
            orgId:orgId,
            mobile:$("#mobile-deta").val(),
            type:'',
            beginTime:'',
            endTime:''
          },
          page: {
            curr: 1 //重新从第 1 页开始
          }
        });
      }else{
        table.reload('integralReload', {
          where: {
            orgId:orgId,
            mobile:$("#mobile-deta").val(),
            type:$("#roleType").val(),
            beginTime:$("#beginTime").val(),
            endTime:$("#endTime").val()
          },
          page: {
            curr: 1 //重新从第 1 页开始
          }
        });
      }
    },
    //导出
    exportAjax: function () {
      var _this = this;
      let pageNo = $(".layui-laypage-em").next().html();
      let pageSize = $(".layui-laypage-limits").find("option:selected").val();
      $.ajax({
        type: "post",
        url: contextPath + "/admin/appUser/getAllIntegralRecodeListExcel",
        data: {
          pageNo: pageNo,
          pageSize: pageSize,
          orgId: orgId,
          mobile: $("#mobile-deta").val(),
          type:$("#roleType").val(),
          beginTime:$("#beginTime").val(),
          endTime:$("#endTime").val(),
        },
        success: function (data) {
          if (data.code == ERROK) {
            // location.href = data.data;
            location.href = $("#contextPath").val() + "/upload/Integral.xls";
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    }
  };
  api.init();
});
