
var app = getApp()
var that;
Page({
  data: {
    month: app.globalData.month,
    showRuleTwo: false,//显示规则组件
    ruleVal: "规则分类",
    selectDateVal: app.globalData.month,
    isPx: true,
    id: '',
    page: 1,
    dateType: 1,
    dateObj: {
      date: '',
      type: ''
    },
    parameter: {}
  },
  onLoad(e) {
    that = this;
    this.setData({ parameter: e })
    if (e.date_interval == 3) {
      this.setData({
        [`dateObj.type`]: "1",
        [`dateObj.date`]: app.globalData.year,
        month: app.globalData.year,
        selectDateVal: app.globalData.year,

      })
    } else if (e.date_interval == 2) { // 传进入如果是季度
      this.setData({
        [`dateObj.type`]: e.date_interval,
        [`dateObj.date`]: app.globalData.year + String(this.getJ() + 1),
        month: app.globalData.year + String(this.getJ() + 1),
        selectDateVal: app.globalData.year + String(this.getJ() + 1) + "季度",
        dateType: e.date_interval
      })
    } else {
      var str = String(app.globalData.month_tow);
      if (app.globalData.month_tow < 10) {
        str = "0" + String(app.globalData.month_tow)
      }
      this.setData({
        [`dateObj.type`]: "3",
        [`dateObj.date`]: app.globalData.month,
        month: app.globalData.year + String(str),
        dateType: e.date_interval,
        selectDateVal: app.globalData.month,
      })
    }
    if (app.globalData.userData) {
      if (e.id) {
        that.setData({ id: e.id });
        dd.setNavigationBar({ title: e.name });
        that.getData();
      }
    } else {
      app.login(app.globalData.corpId, function (is) {
        if (is) {
          if (e.id) {
            that.setData({ id: e.id });
            dd.setNavigationBar({ title: e.name });
            that.getData();
          }
        } else {
          dd.reLaunch({
            url: '../noJurisdiction/noJurisdiction'
          })
        }
      })
    }
  },
  onShareAppMessage() {
    return {
      title: this.data.parameter.name,
      desc: this.data.parameter.name,
      path: 'pages/statistics/C_ranking/C_ranking?id=' + this.data.parameter.id + '&name=' + this.data.parameter.name + '&date_interval=' + this.data.parameter.date_interval
    };
  },
  //选择时间
  onConfirmDate(data) {
    this.setData({
      dateObj: data,
    })
    if (data.type == '1') {
      this.setData({
        selectDateVal: data.date,
        month: data.date,
        dateIndex: data.type
      })
    } else if (data.type == '2') {
      this.setData({
        selectDateVal: data.date + '季度',
        month: data.date,
        dateIndex: data.type
      })
    } else {
      var str = data.date.slice(0, 4)
      var str2 = data.date.slice(data.date.length - 2)
      this.setData({
        selectDateVal: data.date,
        month: str + str2,
        dateIndex: data.type
      })
    }
    this.setData({ page: 1 })
    this.getData();
  },

  // 获取当前是第几季
  getJ() {
    var getYearWeek = function (a, b, c) {
      var date1 = new Date(a, parseInt(b) - 1, c), date2 = new Date(a, 0, 1),
        d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000)
      return Math.ceil((d + ((date2.getDay() + 1) - 1)) / 7)
    }
    var today = new Date()// 获取当前时间
    var y = today.getFullYear()
    var m = today.getMonth() + 1
    var d = today.getDate()
    var week, quarter = ''
    var result = getYearWeek(y, m, d)
    if (m < 4) {
      quarter = 1
      week = result
    } else if (m < 7) {
      quarter = 2
      week = result - getYearWeek(y, 4, 1)
      var day = new Date(y, 4, 1)
      if (day.getDay() > 1) {
        week += 1
      }
    } else if (m < 10) {
      quarter = 3
      week = result - getYearWeek(y, 7, 1)
      var day = new Date(y, 7, 1)
      if (day.getDay() > 1) {
        week += 1
      }
    } else {
      quarter = 4
      week = result - getYearWeek(y, 10, 1)
      var day = new Date(y, 10, 1)
      if (day.getDay() > 1) {
        week += 1
      }
    }
    return quarter - 1
  },
  getData(is) {
    var data = {
      date: that.data.month,
      page: that.data.page,
      page_size: 10,
      sort: that.data.isPx ? 'asc' : 'desc',
      // type: 'normal',
      group_id: that.data.id,
    }
    app.$get('api/integral/statistics/groups/rank', data).then((res) => {
      var data = res.data.data.list;
      if (is) {
        if (data.length == 0) {
          return;
        }
        var list = that.data.list;
        this.setData({
          list: list.concat(data)
        })
      } else {
        this.setData({
          list: data
        })
      }
    })
  },
  //上啦加载
  onScrollToLower() {
    this.setData({ page: ++that.data.page })
    this.getData(true);
  },
  //显示选择时间组件
  selectDate() {
    this.setData({
      showDate: true
    })
  },
  selectIsPx() {
    this.setData({
      isPx: !this.data.isPx,
      page: 1
    })
    this.getData();
  },
  //打开详情
  openDetail(e) {
    var item = e.target.dataset.item;
    dd.navigateTo({
      url: '../prizeBuckleDetail/prizeBuckleDetail?item=' + item
    })
  },
  //关闭弹窗
  onClose() {
    this.setData({
      showDate: false
    })
  },
});
