
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    activeIndex: 1,
    array: ['编辑任务', '删除任务'],
    isPublisher: true,//是不是发布者
    userId: '',
    id: '',
    isCreator: false,
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "任务详情" });
    this.setData({
      isCreator: app.globalData.isCreator
    })
    if (e.id) {
      // that.getDetail(e.id);
      this.setData({
        id: e.id,
        userId: app.globalData.userData.id
      })
    }
  },
  onShow() {
    if (app.globalData.userData) {
      dd.hideLoading();
      that.getDetail(that.data.id);
    } else {
      app.login(app.globalData.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 () { })
    }
  },
  onShareAppMessage() {
    return {
      title: '任务详情',
      desc:'积分任务:'+ this.data.dataDetail.task_name,
      path: 'pages/statistics/taskDetail/taskDetail?id=' + this.data.id
    };
  },
  bindPickerChange(e) {
    if (this.data.array[e.detail.value] == '删除任务') {
      dd.confirm({
        title: '删除任务',
        content: '删除此任务将会删除其相关记录和积分数据，确认删除吗？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            app.$get("api/integral/task/delete", { task_id: this.data.id }).then((res) => {
              app.globalData.showToast(res.data.msg);
              setTimeout(() => {
                dd.navigateBack({ delta: 1 })
              }, 1000);
            })
          }
        },
      });
    } else if (this.data.array[e.detail.value] == '编辑任务') {//编辑任务
      dd.navigateTo({
        url: '../offerAreward_set/offerAreward_set?item=' + JSON.stringify(this.data.dataDetail)
      })
    }
  },
  //领取任务
  formSubmit() {
    app.$post("api/integral/task", { task_id: this.data.dataDetail.id }).then((res) => {
      app.globalData.showToast('已领取');
      setTimeout(() => {
        dd.navigateBack({ delta: 1 })
      }, 1000);
    })
  },
  //显示图片
  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 })
  },
  openSearch() {
    app.globalData.showToast("暂不支持筛选");
  },
  getDetail(id, func = function () { }) {
    app.$get("api/integral/task", { task_id: id }).then((res) => {
      func();
      var data = res.data.data;
      data.pt_name = app.getTypesItem(data.pt_id).name;
      if (data.status == 1 && data.status_mark == '待领取') {
        this.setData({
          array: ['编辑任务', '删除任务'],
        })
      } else {
        this.setData({
          array: ['删除任务'],
        })
      }
      this.setData({
        dataDetail: data,
        isPublisher: data.owner_id == this.data.userId ? true : false
      })
    })
  },
  openSchedule() {
    dd.navigateTo({
      url: '../schedule/schedule'
    })
  },
});
