
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    minuteIndex: 0,//条件选择 0-全部 1-迟到 2-早退 6-准时打卡 20-上班缺卡 21-下班缺卡 16-加班 10-缺勤
    minuteArray: [
      { id: 0, name: '总分' },
      { id: 1, name: '迟到' },
      { id: 2, name: '早退' },
      { id: 6, name: '准时打卡' },
      { id: 20, name: '上班缺卡' },
      { id: 21, name: '下班缺卡' },
      { id: 16, name: '加班' },
      { id: 10, name: '缺勤' },
    ],
    showDate: false,//是否显示时间组件
    selectDateVal: '时间范围',
    TisrrIndex: 0,//条件选择
    page: 1,
    getData: '',
    tisrrArr:[],
    dateObj:[],
    defaultDate:{type:"2",date:[app.globalData.day,app.globalData.day]},
  },
  onLoad() {
    var lastDay= new Date(app.globalData.year,app.globalData.month_tow,0).getDate()
    this.setData({
      defaultDate:{type:"2",date:[app.globalData.month+'-01',app.globalData.month+'-'+lastDay]},
      dateObj:[app.globalData.month+'-01',app.globalData.month+'-'+lastDay]
    })
    console.log(this.data.defaultDate)
    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={
      employee_id:item.id,
      type:type,
      item:item,
      date_type:'2',
      start_date:that.data.dateObj[0],
      end_date:that.data.dateObj[1],
    }
    dd.navigateTo({
      url: '../checkDetail/checkDetail?item='+JSON.stringify(obj),
    })
  },
  getData(is) {
    dd.showLoading();
    var data = {
      event_type: that.data.minuteArray[that.data.minuteIndex].id,
      page: that.data.page,
      page_size: 20,
      start_date:that.data.dateObj[0]|| app.globalData.day,
      end_date:that.data.dateObj[1]|| app.globalData.day,
      group_id:that.data.tisrrArr.length>0? that.data.tisrrArr[that.data.TisrrIndex].id:''
    }
    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);
    }
  },

  //选择时间
  onConfirmDate(data) {
    that.setData({
      defaultDate:data,
      dateObj: data.date,
      page: 1,
      selectDateVal: '时间范围',
    })
    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] })
        }
      // }
      // arr.unshift({id:'',name:'全部分组'})
      this.setData({
        tisrrArr: arr
      })
      this.getData()
    })
  },
});
