
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    minuteIndex: 0,//条件选择
    minuteArray: [
      { id: 1, name: '总分' },
      { id: 2, name: '早到' },
      { id: 3, name: '迟到' },
      { id: 4, name: '加班' },
      { id: 5, name: '请假' },
      { id: 8, name: '早退' },
      { id: 9, name: '缺卡' },
      { id: 10, name: '缺勤' },
    ],
    showDate: false,//是否显示时间组件
    selectDateVal: '本月',
    TisrrIndex: 0,//条件选择
    page: 1,
    date_type: 3,
    getData: '',
    tisrrArr:[],
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "考勤积分排名" });
    this.getTisrr();
  },
  openDetail(e) {
    var item = e.target.dataset.item;
    var type = that.data.minuteArray[that.data.minuteIndex].id
    var obj={
      id:item.id,
      type:type,
      date_type:that.data.date_type,
      item:item,
    }
    if (that.data.date_type == 2) {
      obj.start_date = that.data.dateObj[0];
      obj.end_date = that.data.dateObj[1];
    } else {
      obj.month_date = that.data.dateObj || that.data.date;
    }
    dd.navigateTo({
      url: '../checkDetail/checkDetail?item='+JSON.stringify(obj),
    })
  },
  getData(is) {
    dd.showLoading();
    var data = {
      type: that.data.minuteArray[that.data.minuteIndex].id,
      date_type: that.data.date_type,
      page: that.data.page,
      page_size: 20,
      group_id:that.data.tisrrArr.length>0? that.data.tisrrArr[that.data.TisrrIndex].id:''
    }
    if (that.data.date_type == 2) {
      data.start_date = that.data.dateObj[0];
      data.end_date = that.data.dateObj[1];
    } else {
      data.month_date = that.data.dateObj || that.data.date;
    }
    app.$post('api/ad/rank', data).then((res) => {
      var data = res.data.data.list;
      dd.hideLoading();
      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("已无更多咯");
    }
  },

  //选择时间
  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 })
  },
  //条件选择
  bingMinute(e) {
    this.setData({
      minuteIndex: e.detail.value,
      page: 1
    });
    this.getData();
  },
  bingMinute2(e) {
    this.setData({
      TisrrIndex: e.detail.value,
      page: 1
    });
    this.getData();
  },

  activeItem(e) {
    var index = e.target.dataset.index;
    this.setData({ activeIndex: index })
  },

  //显示时间选择组件
  selectDate() {
    this.setData({ showDate: !this.data.showDate })
  },

  getTisrr() {
    app.$get('api/ad/groups', {}).then((res) => {
      var arr = [];
      var data = res.data.data.list;
      if(data.length>0){
        for (let k in data) {
          arr.push({ id: k, name: data[k] })
        }
      }
      this.setData({
        tisrrArr: arr
      })
      this.getData()
    })
  },
});
