
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    activeIndex: 1,
    page: 1,
    toList: [], // 放今天待完成
    list: [],
    showZp: false,//显示sx选择
    parameter: { //我指派的参数
      pt_id: 0,//类型
      status: 'running',//状态
      sort: 'publish',//排序
    },
  },
  onLoad(e) {
    that = this;
    if (e.index) {
      this.setData({
        activeIndex: e.index,
      })
    }
    dd.setNavigationBar({ title: "我的任务" });
  },
  onShow() {
    if (app.globalData.userData) {
      if (this.data.page == 1) {
        dd.hideLoading();
        that.getToDay();
        that.getData();
      }
    } else {
      app.login(app.globalData.corpId, function (is) {
        if (is) {
          that.getToDay();
          if (that.data.page == 1) {
            dd.hideLoading();
            that.getData();
          }
        } else {
          dd.reLaunch({
            url: '../noJurisdiction/noJurisdiction'
          })
        }
      },function(){})
    }
  },
  onReset() {
    this.setData({
      page: 1,
      isVal: false,
      searchName: ''
    })
    this.getData(this.data.activeIndex);
  },
  bindKeyInput(e) {
    this.setData({
      page: 1,
      isVal: e.detail.value ? true : false,
      searchName: e.detail.value ? e.detail.value : '',
    })
    this.getData(this.data.activeIndex);
    this.getToDay()
  },
  //显示选择员工
  selectSx() {
    this.setData({ showZp: true })
  },
  //关闭弹窗
  onClose() {
    this.setData({
      showZp: false,
    })
  },
  //选择筛选
  onConfirmSx(data) {
    this.setData({
      sxObj: data,
      page: 1,
      parameter: { //我指派的参数
        pt_id: data[0],//类型
        status: this.data.parameter.status,
        sort: data[2]
      }
    })
    this.getData();
  },
  //今天完成的任务
  getToDay() {
    app.$get('api/integral/work/list', { status: 'running', today: '1', page: '1', page_size: '50', sort: 'publish', source_type: 0, pt_id: 0 ,content: that.data.searchName,}).then((res) => {
      this.setData({
        toList: res.data.data.list
      })
    })
  },

  getData(is) {
    var parameter = this.data.parameter;
    var data = {
      page: that.data.page,
      page_size: 20,
      source_type: 0,//作来源种类 0-全部 1-直接指派(任务池领取以外的类型) 2-任务池领取 3-任务模板指派 4-每月绩效任务包
      pt_id: parameter.pt_id,//类型
      status: parameter.status,//工作状态 all-全部 running-进行中 complete-工作已完成 refuse-拒绝 reviewed-已审批 revocation-发布者撤回
      sort: parameter.sort,//排序种类 expire-过期时间倒序 publish-指派时间倒序
      content:that.data.searchName,
    }
    app.$get('api/integral/work/list', data).then((res) => {
      var data = res.data.data.list;
      data.forEach(item => {
        item.isCs = item.expire_time < app.globalData.allDate;
      })
      if(that.data.page==1){
        this.setData({
            list: []
        })
      }
      var list = that.data.list;
      data.forEach(element => {
        if (element.pt_id) {
          element.pt_name = app.getTypesItem(element.pt_id).name
        }
      });
      if (is) {
        if (data.length < 20) {
          this.setData({
            list: list.concat(data),
            isData: true
          })
        } else {
          this.setData({
            list: list.concat(data)
          })
        }
      } else {
        this.setData({
          list: data,
          isData: false
        })
      }
    })
  },
  activeItem(e) {
    var index = e.target.dataset.index;
    var status = "";
    if (index == 1) {
      status = "running"
    } else if (index == 2) {
      status = "complete"
    } else {
      status = "reviewed"
    }
    this.setData({
      activeIndex: index,
      page: 1,
      list: [],
      sxObj: [],
      parameter: {
        pt_id: 0,//类型
        status: status,//状态
        sort: 'publish',//排序
      }
    })
    this.getData();
  },
  openPerform(e) {
    var id = e.target.dataset.item.id;
    var is = e.target.dataset.is ? true : false
    dd.navigateTo({
      url: '../openPerform/openPerform?id=' + id + '&is=' + is
    })
  },
  //过滤List,通常用于删除了某条数据，刷新列表
  filtrationList(index, id) {
    if (index == 1) {
      this.getToDay()
    } else if (index == 2 && this.data.page != 1) {
      var list = this.data.list;
      for (let i in list) {
        if (list[i].id == id) {
          list.splice(i, 1)
        }
      }
      this.setData({ list: list })
    }
  },
  openDetail(e) {
    var id = e.target.dataset.item.id;
    dd.navigateTo({
      url: '../workDetail/workDetail?id=' + id
    })
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({ page: ++that.data.page })
      this.getData(true);
    }
  },
});
