
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    page: 1,
    toList: [], // 放今天待完成
    list: [],
    showZp: false,//显示sx选择
    parameter: { //我指派的参数
      pt_id: 0,//类型
      status: 'running',//状态
      sort: 'publish',//排序
    },
    
    showStaff: false,//显示员工选择
    staffVal: '执行人',
    minuteIndex: 0,//条件选择
    minuteArray: [
      { id: 0, name: '待完成' },
      { id: 1, name: '待审批' },
      { id: 2, name: '已审批' },
    ],

    popupCx: '',  
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "我审批的" });
  },
  onShow() {
    if (app.globalData.userData) {
      if (this.data.page == 1) {
        dd.hideLoading();
        that.getData();
      }
    } else {
      app.login(app.globalData.corpId, function (is) {
        if (is) {
          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);
  },
  //显示选择员工
  selectStaff() {
    this.setData({
      popupCx: 'container__mask',
      showStaff: true
    })
  },
  selectSx() {
    this.setData({ showZp: true })
  },
    //选择员工
  onConfirmStaff(data) {
    if (!data) {
      this.setData({ staffVal: '执行人', employee_ids: '', page: 1 })
      this.getData();
      return false;
    }
    if (data.length > 0) {
      if (data.length > 1) {
        var str = "";
        var arr = [];
        data.forEach((item) => {
          str += item.name + ','
          arr.push(item.id);
        })
        var employee_ids = arr.join(',');
        this.setData({ staffVal: str.substring(0, str.length - 1), employee_ids: employee_ids, page: 1 })
      } else {
        this.setData({ staffVal: data[0].name, employee_ids: [data[0].id], page: 1 })
      }
    } else {
      this.setData({ staffVal: '执行人', employee_ids: [], page: 1 })
    }
    this.getData();
  },
  //关闭弹窗
  onClose() {
    this.setData({
      showZp: false,
      showStaff: false,
      popupCx: ''
    })
  },
  //选择筛选
  onConfirmSx(data) {
    this.setData({
      sxObj: data,
      page: 1,
      parameter: { //我指派的参数
        pt_id: data[0],//类型
        status: this.data.parameter.status,
        sort: data[2]
      }
    })
    this.getData();
  },
  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-指派时间倒序
      employee_id:this.data.employee_ids,
      content:that.data.searchName,
    }
    app.$get('api/integral/work/list/reviewer', 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
        })
      }
    })
  },
  openDetail2(e){
    var id = e.target.dataset.item.review_id;
    dd.navigateTo({
      url: '../../workbench/approveDetail/approveDetail?id=' + id
    })
  },
  activeItem(e) {
    var index = Number(e.detail.value)+1;
    var status = "";
    if (index == 1) {
      status = "running"
    } else if (index == 2) {
      status = "complete"
    } else {
      status = "reviewed"
    }
    this.setData({
      minuteIndex: e.detail.value,
      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) {

    } else if (index == 2 && this.data.page != 1) {
      var list = this.data.list;
      for (let i in list) {
        if (list[i].review_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);
    }
  },
});
