import Utils from "../../../utils/util"
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topBarH: app.globalData.CustomBar,
    postData: {},
    userInfo:{}
  },
  /**
   * 输入内容
   */
  inputContent: function (e) {
    const name = e.currentTarget.dataset.name;
    this.data.postData[name] = e.detail.value;
  },
  /**
   * 发布
   */
  push: function () {
    if (!this.data.postData.content) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      })
      return
    };
    let params= {
      ...this.data.postData,
      difficultyId:this.data.postData.difficultyId||'',
      userId:this.data.userInfo.userId,
    };
    app.axios("POST", "app", "/difficultyAppeal/addDifficulty", params).then(res => {
      if (res.code == 1) {
        wx.showToast({
          title: '发布成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500);
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  //撤销上报
  cancellation:function () {
    let id = this.data.postData.difficultyId;
    let userId = this.data.userInfo.userId;
    wx.showModal({
      title: '撤销上报',
      content: '是否确认撤销上报',
      success (res) {
        if (res.confirm) {
          app.axios("PUT", "app", `/difficultyAppeal/cancelDifficulty/${id}/${userId}`).then(res => {
            if (res.code == 1) {
              wx.showToast({
                title: '撤销上报成功',
                icon: 'none'
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 1500);
            }
          });
        } else if (res.cancel) {
        }
      }
    })
    
  },
  //删除详情
  delete:function (params) {
    let id = this.data.postData.difficultyId;
    let userId = this.data.userInfo.userId;
    wx.showModal({
      title: '删除',
      content: '是否确认取消',
      success (res) {
        if (res.confirm) {
          app.axios("DELETE", "app", `/difficultyAppeal/deleteDifficulty/${id}/${userId}`).then(res => {
            if (res.code == 1) {
              wx.showToast({
                title: '删除成功',
                icon: 'none'
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 1500);
            }
          });
        } else if (res.cancel) {
        }
      }
    })
  },
  //获取详情
  getDetail:function (id) {
    app.axios("GET", "app", `/difficultyAppeal/difficultyDetail/${id}`).then(res => {
      this.data.postData = res.data;
      this.setData(this.data);
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.difficultyId) {
      this.getDetail(options.difficultyId);
    }
    // this.data.isUserDetail = options.user;
    // this.data.uuid = Utils.uuid();
    // this.setData(this.data)
    this.data.userInfo = app.globalData.userInfo;
    this.setData(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})