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

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topBarH: app.globalData.CustomBar,
    postData: {},
    userInfo:{},
    uuid:'',
    files:[],
    baseUrl:app.FILE_SERVER_URL,
  },
  /**
   * 输入内容
   */
  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||this.data.uuid,
      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.getFiles();
      this.setData(this.data);
    });
  },
    /**
   * 选择文件
   */
  chooseFile: function () {
    wx.chooseImage({
      count: 1,
      type: 'all',
      success: (res) => {
        const tempFilePaths = res.tempFiles;
        const path = tempFilePaths[0].path;
        this.postImg(path)
      }
    })
  },
  chooseVideo: function () {
    let that = this
    wx.chooseVideo({  
      count: 1,
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。需大于等于3s  
      camera: 'back', // 默认拉起后置摄像头，front为前置摄像头  
      success (res) {  
        // tempFilePath可以作为video标签的src属性显示视频  
        const tempFilePath = res.tempFilePath 
        that.postImg(tempFilePath) 
      }  
    })
  },
    /**
   * 上传图片
   */
  postImg(filePath) {
    wx.showLoading({
      title: '文件上传中...',
    })
    app.uploadFile(this.data.uuid, app.FileType.resumptionAcs, filePath).then(res => {
      wx.hideLoading();
      this.getFiles();
    })
  },

  /**
   * 获取图片
   */
  getFiles: function () {
    app.axios("GET", "common", "/upload/getFile", {
      otcid: this.data.postData.difficultyId || this.data.uuid,
      otctype: app.FileType.resumptionAcs
    }).then(res => {
      if (res.code == 1) {
        this.data.files = res.data;
        this.setData(this.data)
      }
    })
  },

  /**
   * 删除图片
   * @param {*} params 
   */
  deleteFile: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '删除提示',
      content: '是否要删除该附件？',
      success: (res) => {
        if (res.confirm) {
          app.axios("GET", 'common', "/upload/delFile", {
            documentId: id,
          }).then(res => {
            if (res.code == 1) {
              this.getFiles();
            }
          })
        }
      }
    })
  },
    /**
   * 状态切换
   */
  changeHtstate(e) {
    var htstate = e.currentTarget.dataset.htstate
    this.data.postData.dangerState = htstate
    this.setData(this.data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.difficultyId) {
      this.getDetail(options.difficultyId);
    }
    // this.data.isUserDetail = options.user;
    // this.setData(this.data)
    this.data.uuid = Utils.uuid();
    this.data.userInfo = app.globalData.userInfo;
    this.setData(this.data);
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})