
var app = getApp()
var that;
Page({
  data: {
    month: app.globalData.month,
    activeIndex: 0,
    array: [{ name: '奖分次数最多', value: 1 }, { name: '扣分次数最多', value: 0 }],
    showTissue: false,
    page: 1,
    tissueValName: '全公司'
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "管理者奖扣执行情况" });
    this.getData();
  },
  sotrs(str) {
    return function (a, b) {
      var val = a[str];
      var val2 = b[str];
      return val2 - val
    }
  },
  getData(is) {
    var data;
    data = {
      page: that.data.page,
      page_size:20,
      month: that.data.month,
      dept_id: that.data.tissueVal,
    }
    app.$get('api/integral/statistics/prize/list',data).then((res) => {
      var data = res.data.data.list || [];
      if (that.data.activeIndex == 0) {
        data.sort(that.sotrs('reward_count'));
      } else {
        data.sort(that.sotrs('deduct_count'));
      }
      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: '../../workbench/prize_buckle/prize_buckle?id='+item.id+'&month='+that.data.month
    })
  },
  bindPickerChange(e) {
    var list=that.data.list;
    if (e.detail.value == 0) {
      list.sort(that.sotrs('reward_count'));
    } else {
      list.sort(that.sotrs('deduct_count'));
    }
   this.setData({
      activeIndex: e.detail.value,
      list:list
    });
  },
  openDate() {
    dd.datePicker({
      format: 'yyyy-MM',
      currentDate: this.data.month,
      success: (res) => {
        this.setData({
          month: res.date,
          page: 1
        })
        that.getData();
      },
    });

  },
  //显示部门选择组件
  selectTissue(item) {
    this.setData({ showTissue: !this.data.showTissue })
  },
  //关闭弹窗
  onClose() {
    this.setData({ showDate: false, showTissue: false })
  },
  //选择部门
  onConfirmTissue(data) {
    this.setData({ tissueValName: data.name, tissueVal: data.id, page: 1 });
    this.getData();
  },
});
