
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: '全公司',
    popupCx: '',  
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "管理者奖扣执行情况" });
    this.getData();
  },
  showText() {
    dd.alert({
        title: '温馨提示',
        content: '管理者奖扣任务均为B分，对A分不做要求',
        buttonText: '我知道了',
        success: (result) => {

        },
      });
  },
  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,'application/vnd.test.v3+json').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);
    }
  },
  openDetail(e) {
    var item = e.target.dataset.item;
    dd.navigateTo({
      url: '../../workbench/prize_buckle/prize_buckle?id=' + item.id + '&month=' + that.data.month
    })
  },
  // showPicker(){
  //  this.setData({
  //     showPicker: true
  //   });
  // },
  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,
      showPicker: false
    });
  },
  openDate() {
    this.setData({ showDate: true })
    dd.datePicker({
      format: 'yyyy-MM',
      currentDate: this.data.month,
      success: (res) => {
        if (res.date) {
          this.setData({
            month: res.date,
            page: 1
          })
          that.getData();
        }
      },
      complete: (res) => {
        that.setData({
          showDate: false,
          popupCx: 'container__mask'
        })
      }
    });

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