var app = getApp();
var that;
var animation;
import { returnStr,setCache,getCache,_debounce } from '../../../utils/util'
Page({
  data: {
    tabIndex: 1,//条件选择
    list: [],
    minuteIndex: 0,//条件选择
    minuteArray: [
      { value: 0, name: '全部' },
      { value: 1, name: '我的团队' },
      { value: 2, name: '我协助的' },
    ],
    selectGlData: {}, // 当前选中项
    keyword: '',
    page: 1,
    defaultRuleId: '',
    isData: false,
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "我参与的" });
    this.getRange();
  },
  onShow() {
    animation = dd.createAnimation({
      duration: 200,
      timeFunction: "linear",
    });
    if(this.data.page==1&&this.data.defaultRuleId){
      this.data.tabIndex==1 ? this.getManagement():this.getScorerRecord()
    }
  },
  openDetail(e) {
    let item=e.target.dataset.item
    dd.navigateTo({
      url: `../performanceDetails/performanceDetails?id=${item.record_id}&tel=${item.package_name}`
    })
  },
  //选择弹窗
  openSelect(e) {
    animation.translateY(0).step();
    that.setData({
      isShowType: true,
      animationInfo: animation.export(),
      popupCx: 'container__mask',
    });
  },
  //关闭规则
  onClose() {
    animation.translateY(1200).step();
    that.setData({
      isShowType: false,
      animationInfo: animation.export(),
      popupCx: '',
    });
  },
  onSelectItem(e) {
    that.setData({
      defaultRuleId: e.id,
      jxName: e.name,
    });
    if (that.data.tabIndex == 1) {
      this.getManagement();
    } else {
      this.getScorerRecord();
    }
  },
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({ page: ++that.data.page })
      if (that.data.tabIndex == 1) {
        this.getManagement(true);
      } else {
        this.getScorerRecord(true);
      }
    }
  },
  //管理记录列表
  getManagement(e) {
    if (!e) {
      this.setData({ page: 1 })
    }
    if (!this.data.defaultRuleId) {
     return false
    }
    let range = this.data.minuteArray[this.data.minuteIndex].value;
    this.setData({ list: [] })
    app.$get('api/per/package/management_record', { package_id: this.data.defaultRuleId, range: range, name: this.data.keyword, page: this.data.page, page_size: 20 }).then(res => {
      let list = res.data.data.list;
      list.forEach(item => {
        item.returnStr = returnStr(item.action_update)
        if (item.employee_id&&app.globalData.usersList[item.employee_id]) {
          //当是导入导出时，显示登录者
          item.userInfo = app.globalData.usersList[item.employee_id];
          item.dept_list = app.globalData.usersList[item.employee_id].dept_list
        }
      });
      if (this.data.page !== 1) {
        list = this.data.list.concat(list);
      }
      this.setData({
        list: list,
        isData: list.length == res.data.data.total
      })
    })
  },
  //我评分的
  getScorerRecord(e) {
    if (!e) {
      this.setData({ page: 1 })
    }
    this.setData({ list: [] })
    let range = this.data.minuteArray[this.data.minuteIndex].value;
    app.$get('api/per/package/score_record', { package_id: this.data.defaultRuleId, range: range, name: this.data.keyword, page: this.data.page, page_size: 20 }).then(res => {
      let list = res.data.data.list;
      list.forEach(item => {
        item.returnStr = returnStr(item.action_update)
        if (item.employee_id&&app.globalData.usersList[item.employee_id]) {
          //当是导入导出时，显示登录者
          item.userInfo = app.globalData.usersList[item.employee_id];
          item.dept_list = app.globalData.usersList[item.employee_id].dept_list
        }
      });
      if (this.data.page !== 1) {
        list = this.data.list.concat(list);
      }
      this.setData({
        list: list,
        isData: list.length == res.data.data.total
      })
    })
  },
  onReset() {
    this.setData({
      isVal: false,
      keyword: ''
    })
    if (that.data.tabIndex == 1) {
      this.getManagement();
    } else {
      this.getScorerRecord();
    }
  },
  bindKeyInput:_debounce(function(e){
    this.setData({
      isVal: e.detail.value ? true : false,
      keyword: e.detail.value ? e.detail.value : '',
    })
    if (that.data.tabIndex == 1) {
      this.getManagement();
    } else {
      this.getScorerRecord();
    }
  }),
  activeItem(e) {
    this.setData({ minuteIndex: e.detail.value, })
    if (that.data.tabIndex == 1) {
      this.getManagement();
    } else {
      this.getScorerRecord();
    }
  },
  tabAction(e) {
    let index = e.target.dataset.index
    this.setData({
      tabIndex: index
    })
    if (that.data.tabIndex == 1) {
      this.getManagement();
    } else {
      this.getScorerRecord();
    }
  },
  getRange() {
    app.$get("api/per/package/tree").then((res) => {
      var list = res.data.data
      let obj;
      list.some(item => {
        if (item.list.length > 0) {
          obj = item.list
          return true;
        }
      });
      that.setData({
        defaultRuleId:obj? obj[0].id:'',
        jxName:obj? obj[0].name:'',
      })
      this.getManagement();
    })
  },
});
