
var app = getApp()
var that;
var typeArr=[];
Page({
  data: {
    date: app.globalData.month,
    minuteIndex: 0,//条件选择
    minuteArray: [
      { id: 0, name: '总分' },
      { id: 1, name: '加班' },
      { id: 2, name: '迟到' },
    ],

    showDate: false,//是否显示时间组件
    selectDateVal: '本月',

    showStaff: false,//显示员工选择
    staffVal: '员工',

    types: '',//积分分类
    typesIndex: '0',

    showRuleTwo: false,
    ruleVal: "规则分类",

    page: 1,

  },
  onLoad(e) {
    that = this;
    typeArr=[{ id: 0, name: '全部' }];
    var arr=typeArr.concat(app.globalData.types);
    this.setData({types:arr})
    var userData=app.globalData.userData;
    dd.setNavigationBar({ title: "积分事件" });
    if (e.month) {
      var typesIndex = "0";
      if (e.type) {
        arr.map((item, index) => {
          if (item.code == e.type) {
            typesIndex = index
          }
        })
      }
      this.setData({ 
        date: e.month,
        typesIndex: typesIndex,
        employee_ids :[userData.id],
        staffVal:userData.name,
        selectDateVal:e.month==app.globalData.month? '本月':'月',
        defaultDate:{type:'1',date:e.month},
      })
    }
    this.getData();
  },
  getData(is) {
    var data = {
      page: that.data.page,
      page_size: 20,
      rule_id: that.data.rule_id,
      pt_id: that.data.types[that.data.typesIndex].id,
      employee_ids: that.data.employee_ids
    }
    if (that.data.date_type == 2) {
      data.start_day = that.data.dateObj[0];
      data.end_day = that.data.dateObj[1];
    } else {
      data.month = that.data.dateObj || that.data.date;
    }
    app.$get('api/integral/statistics/integral', data).then((res) => {
      var data = res.data.data.list;
      if (is) {
        if (data.length == 0) {
          this.setData({
            isData: true
          })
          return;
        }
        var list = that.data.list;
        this.setData({
          list: list.concat(data)
        })
      } else {
        this.setData({ list: [] });
        this.setData({
          list: data,
          isData: false
        })
      }
    })
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({ page: ++that.data.page })
      this.getData(true);
    } else {
      app.globalData.showToast("已无更多咯");
    }
  },
  //打开兴趣
  openDetail(e) {
    var item = e.target.dataset.item;
    dd.navigateTo({
      url: '../prizeBuckleDetail/prizeBuckleDetail?id=' + item.id
    })
  },
  //显示规则组件
  selectRule() {
    this.setData({
      showRuleTwo: true
    })
  },
  //显示选择时间组件
  selectDate() {
    this.setData({
      showDate: true
    })
  },
  //显示选择员工
  selectStaff() {
    this.setData({
      showStaff: true
    })
  },
  //选择时间
  onConfirmDate(data) {
    that.setData({
      defaultDate:data,
      dateObj: data.date,
      date_type: data.type == 1 ? 3 : data.type,
      page: 1,
      selectDateVal: data.type == 1 ? (data.date==that.data.date? '本月':'月') : '日',
    })
    this.getData();
  },
  //关闭弹窗
  onClose() {
    this.setData({
      showDate: false,
      showStaff: false,
      showRuleTwo: false
    })
  },
  //选择积分分类
  bingIntegralType(e) {
    this.setData({
      typesIndex: e.detail.value,
      page: 1
    });
    this.getData();
  },
  //选择员工
  onConfirmStaff(data) {
    if (!data) {
      this.setData({ staffVal: '员工', employee_ids: '', page: 1 })
      this.getData();
      return false;
    }
    if(data.length>0){
      if (data.length >1 ) {
        var str = "";
        var arr = [];
        data.forEach((item) => {
          str += item.name + ','
          arr.push(item.id);
        })
        var employee_ids = arr.join(',');
        this.setData({ staffVal: str.substring(0, str.length - 1), employee_ids:employee_ids, page: 1 })
      } else {
        this.setData({ staffVal: data[0].name, employee_ids: [data[0].id], page: 1 })
      }
    }else{
       this.setData({ staffVal: '员工', employee_ids: [], page: 1 })
    }


    this.getData();
  },
  //选择规则
  onConfirmRule(e) {
    var data = e.join(',');
    this.setData({
      rule_id: data ? data : '',
      page: 1
    })
    this.getData();
  },
});
