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

    showDate: false,//是否显示时间组件
    selectDateVal: app.globalData.month,

    showSx: false,//显示sx选择
    sxVal: '筛选',

    showRuleTwo: false,//显示规则组件
    ruleVal: "规则分类",

    showTissue: false,//显示部门组件
    TissueVal: '全公司',

    sort:'DESC',
    position:'all',
    year:app.globalData.year,
    page:1,
    dateIndex:'3',
    popupCx: '',
    pt_id:'3',
  },
  onShareAppMessage() {
    return {
      title: '阶段排名',
      desc: '积分排名有变动了，快来看看！',
      path: 'pages/statistics/sectionRanking/sectionRanking'
    };
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "阶段排名" });
  },
  onShow() {
    if (app.globalData.userData) {
      if (that.data.page == 1) {
        dd.hideLoading();
        that.getData();
      }
    } else {
      app.login(app.globalData.corpId, function (is) {
        if (is) {
          if (that.data.page == 1) {
            dd.hideLoading();
            that.getData();
          }
        } else {
          dd.reLaunch({
            url: '../../noJurisdiction/noJurisdiction'
          })
        }
      },function(){})
    }

  },
  getData(is) {  
    var data = {
      pt_id:that.data.pt_id,
      dept_id:that.data.dept_id,
      page: that.data.page,
      rule_id: that.data.rule_id,
      sort:that.data.sort,
      position:that.data.position,
      page_size:20
    }
    if(that.data.dateIndex=='1'){
      data.year=that.data.year;
    }else if(that.data.dateIndex=='2'){
      data.quarter=that.data.quarter
    }else if(that.data.dateIndex=='4'){
      data.start_date=that.data.custom.start_date
      data.end_date=that.data.custom.end_date
    }else{
      data.month=that.data.month||app.globalData.month;
    }
    app.$get('api/integral/statistics/ranking',data,'application/vnd.test.v2+json').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);
    }
  },
  // //打开兴趣
  // openDetail(e) {
  //   var item = e.target.dataset.item;
  //   console.log(item)
  //   dd.navigateTo({
  //     url: '../prizeBuckleDetail/prizeBuckleDetail?id=' + item.employee_id
  //   })
  // },
  //显示规则组件
  selectRule() {
    this.setData({
      popupCx: 'container__mask',
      showRuleTwo: true
    })
  },
  //显示选择时间组件
  selectDate() {
    this.setData({
      popupCx: 'container__mask',
      showDate: true
    })
  },
  //显示选择员工
  selectSx() {
    this.setData({
      popupCx: 'container__mask',
      showSx: true
    })
  },
  //选择时间
  onConfirmDate(data) {
    this.setData({
        dateObj: data,
    })
    if (data.type == '1') {
      this.setData({
        selectDateVal:data.date,
        year:data.date,
        dateIndex:data.type
      })
    } else if(data.type == '2'){
      this.setData({
        selectDateVal:data.date+'季度',
        quarter: data.date,
        dateIndex:data.type
      })
    } else if(data.type == '4'){
      this.setData({
        selectDateVal: data.date.start_date + '--' + data.date.end_date,
        custom: data.date,
        dateIndex:data.type
      })
    }else{
       this.setData({
        selectDateVal: data.date,
        month:data.date,
        dateIndex:data.type
      })
    }
    this.setData({page:1})
    this.getData();
  },
  //关闭弹窗
  onClose() {
    this.setData({
      showDate: false,
      showSx: false,
      showRuleTwo: false,
      showTissue: false,
      popupCx: ''  
    })
  },
  //选择筛选
  onConfirmSx(data) {
    console.log(data)
   this.setData({sxObj:data, sort:data[1],position:data[0],pt_id:data[2],page:1})
   this.getData();
  },
  //选择规则
  onConfirmRule(data) {
    this.setData({
      rule_id: data.id ? data.id : '',
      page: 1,
      ruleVal: data.name
    })
    this.getData();
  },
  //显示部门选择组件
  selectTissue() {
    this.setData({ showTissue: !this.data.showTissue,popupCx: 'container__mask', })
  },
  //选择部门
  onConfirmTissue(data) {
    this.setData({ dept_id: data.id,page:1,TissueVal:data.name })
    this.getData();
  },
});
