
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    activeIndex: 1,
    id: '',
    userId: '',
    process: [],
    array: ['编辑任务', '删除任务'],
    isPublisher: true,//是不是发布者
    corpId: '',
    isNot:false,
    keepTheScore:false,//是否展示记分
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "工作详情" });
    if (e.id) {
      this.setData({
        id: parseInt(e.id),
        userId: app.globalData.userData.id,
        isNot:e.isNot?true:false,
      })
    }
    if (e.corpId) {
      that.setData({ corpId: e.corpId });
      app.globalData.corpId = e.corpId
    }

  },
  onShow() {
    if (app.globalData.userData) {
      dd.hideLoading();
      that.getDetail(that.data.id);
    } else {
      let corpId = that.data.corpId ? that.data.corpId : app.globalData.corpId
      app.login(corpId, function (is) {
        if (is) {
          dd.hideLoading();
          that.setData({
            userId: app.globalData.userData.id
          })
          that.getDetail(that.data.id);
        } else {
          dd.reLaunch({
            url: '../noJurisdiction/noJurisdiction'
          })
        }
      }, function () { })
    }
  },
  // 判断当前账号与发布工作者是否同一人；
  current_none(data) {
    var actions = []
    // 这里比较复杂，因为团队绩效的工作详情跟我的发布的工作详情都调用这个页面
    if (data.status == 1) {
      if (data.pt_id != 1) {
        actions.push('编辑任务')
        actions.push('删除任务')
      } else {
        actions.push('删除任务')
      }
    } else {
      actions.push('删除任务')
    }
    this.setData({ array: actions })
  },
  bindPickerChange(e) {
    var page = getCurrentPages()[getCurrentPages().length - 2]//上一个页面的数据
    if (this.data.array[e.detail.value] == '删除任务') {
      dd.confirm({
        title: '删除任务',
        content: '删除此任务将会删除其相关记录和积分数据，确认删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            app.$get("api/integral/work/delete", { work_id: this.data.id }).then((res) => {
              app.globalData.showToast(res.data.msg);
              if (page) {
                page.filtrationList(2, this.data.id);
                setTimeout(() => {
                  dd.navigateBack({ delta: 1 })
                }, 1000);
              } else {
                setTimeout(() => {
                  dd.reLaunch({
                    url: '/pages/workbench/index/index'
                  })
                }, 1000);
              }
            })
          }
        },
      });
    } else if (this.data.array[e.detail.value] == '编辑任务') {//编辑任务
      dd.navigateTo({
        url: '../assaign_set/assaign_set?item=' + JSON.stringify(this.data.dataDetail)
      })
    }
  },
  //显示图片
  showImg(e) {
    var index = e.target.dataset.index;
    var item = e.target.dataset.item
    dd.previewImage({
      current: index,
      urls: item
    });
  },
  activeItem(e) {
    var index = e.target.dataset.index;
    this.setData({ activeIndex: index })
  },
  openJyt() {
    dd.navigateTo({
      url: '../workpoints/workpoints?item=' + JSON.stringify(this.data.process) + '&id=' + this.data.dataDetail.id + '&isJf=' + false
    })
  },
  openJf(e) {
    dd.navigateTo({
      url: '../workpoints/workpoints?item=' + JSON.stringify(this.data.process) + '&id=' + this.data.dataDetail.id + '&isJf=' + true
    })
  },
  employee_me(data){
    let params = {
			id: data.employee_id
    }
    app.$get('api/employee/info', params).then(res =>{
      this.setData({
        keepTheScore: this.keepTheScore(data,res.data.data)
      })
    })
  },
  keepTheScore(cer,data) {
    if(cer.status > 2){
      return false
    }
    if(this.data.userId == cer.reviewer_id){
      return true
    }
    return data.employee_detail.superior_list.some(x =>{
      if(this.data.userId == x.id){
        return true
      }
    })
  },
  deleteItem(e) {
    dd.confirm({
      title: '提示',
      content: '确定永久删除此项?',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm) {
          var index = e.target.dataset.index;
          var data = {
            work_id: this.data.id,
            process: this.data.process
          }
          data.process.splice(index, 1)
          if (data.process.length == 0) {
            data.process = "[1]"
          } else {
            data.process = JSON.stringify(data.process);
          }
          app.$post("api/integral/work", data).then((res) => {
            app.globalData.showToast(res.data.msg);
            this.getDetail(this.data.id);
          })
        }
      },
    });
  },
  getDetail(id, fun = function () { }) {
    this.setData({
      total: 0,
      process: []//清空数据，避免头像组件未刷新造成的视图问题
    })
    app.$get("api/integral/work", { work_id: id }).then((res) => {
      fun();
      this.current_none(res.data.data)
      var process = res.data.data.process.list || [];
      var data = res.data.data;
      data.jf = [];
      data.gc = [];
      process.forEach(item => {
        if (item.recorder_id == data.publisher_id) {
          data.jf.push(item)
        } else {
          data.gc.push(item)
        }
      })
      this.employee_me(data)
      this.setData({
        dataDetail: data,
        total: res.data.data.process.total,
        process: process,
        isPublisher: data.publisher_id == this.data.userId ? true : false,
        // keepTheScore: that.keepTheScore(data)
      })
    })
  },
  openWc() {
    dd.navigateTo({
      url: '../../workbench/approve/approve?id=' + this.data.dataDetail.review_id
    })
  },
  // 完成任务
  openPerform() {
    dd.navigateTo({
      url: '../openPerform/openPerform?id=' + this.data.dataDetail.id + '&is=detail'
    })
  },
  openSchedule() {
    dd.navigateTo({
      url: '../schedule/schedule?id=' + this.data.dataDetail.id + "&progress=" + this.data.dataDetail.progress
    })
  },
});
