let app = getApp();
const Http = require("../../utils/HttpBasics");
const util = require("../../utils/util.js");
const config = require("../../config/config");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    content: '',
    phone: '',
    date: '2000-01-01',
    dateEnd: '',
    dateFlag: 1,
    gender: '',
    sexArry: ['男', '女'],
    sexFlag: 1,
    fileName: '',
    filePath: '',
    currentID: '',
    isTextleft: false
  },

  bindDateChange(e) {
    this.setData({
      date: e.detail.value,
      dateFlag: 2
    })
  },

  bindGenderChange(e) {
    this.setData({
      gender: e.detail.value,
      sexFlag: 2
    })
  },

  showPhoneMsg() {
    wx.showToast({
      title: '手机号无法更改',
      icon: "error"
    })
  },

  textareaInput(e) {
    const text = e.detail.value
    this.setData({
      isTextleft: text ? true : false
    })
    console.log(text);
    console.log(this.data.isTextleft);
  },

  uploadFile() {
    wx.chooseMessageFile({
      count: 1, // 选择一个文件
      type: 'file', // 文件类型，可以是 image、video、file
      success: (res) => {
        console.log(res, 'res');
        wx.showLoading({
          title: '上传中',
        })
        const fileName = res.tempFiles[0].name;
        const tempFilePath = res.tempFiles[0].path;
        this.goUpload(fileName, tempFilePath);
      },
    });
  },

  goUpload(fileName, filePath) {
    const that = this
    wx.uploadFile({
      url: config.url + config.api.resumeFileUpload,
      filePath,
      name: 'file',
      header: {
        'token': app.globalData.token
      },
      success: (res) => {
        const response = JSON.parse(res.data)
        console.log(response, 'response')

        wx.hideLoading()
        wx.showToast({
          title: '上传成功',
          icon: 'success'
        });
        that.setData({
          fileName,
          filePath: response.data.url
        })
      },
      fail: (err) => {
        wx.hideLoading()
        wx.showToast({
          title: '上传失败',
          icon: 'error'
        });
        console.error(err);
      },
    });
  },

  cancelUpload() {
    const that = this
    wx.showModal({
      title: '提示',
      content: '删除文件后需要重新上传',
      success(res) {
        if (res.confirm) {
          that.setData({
            fileName: "",
            filePath: ""
          })
        } else if (res.cancel) {
        }
      }
    })

  },

  formSubmit(e) {
    const that = this
    const data = {
      userName: e.detail.value.userName,
      sex: 1 * that.data.gender + 1,
      birthDay: that.data.date + " 00:00:00",
      content: e.detail.value.content,
    }
    console.log(data, 'data');

    if (!data.userName) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (data.sex == 0) {
      wx.showToast({
        title: '请选择性别',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (!data.content) {
      wx.showToast({
        title: '请输入求职内容',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (that.data.fileName) data.fileName = that.data.fileName
    if (that.data.filePath) data.filePath = that.data.filePath
    if (that.data.currentID) data.id = that.data.currentID

    Http.post({
      url: config.api.resumeAdd,
      data,
    }).then(res => {
      console.log(res.code);
      wx.showToast({
        title: "提交成功!",
        icon: 'success',
        duration: 1500,
      })
      this.getResumeDetail()
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000,
      })
    })
  },

  getUserPhone() {
    const that = this
    Http.get({
      url: config.api.getScore,
      data: {}
    }).then(res => {
      console.log(res.data);
      if (res.data.phone) {
        that.setData({
          phone: res.data.phone
        })
      }
    })
      .catch(err => {
        wx.showModal({
          title: '提示',
          content: err.errMsg,
          showCancel: false
        })
      })
  },

  getResumeDetail() {
    const that = this
    Http.get({
      url: config.api.resumeDetail,
    }).then(res => {
      const data = res.data
      that.setData({
        userName: data.userName,
        sexFlag: 2,
        gender: data.sex - 1,
        dateFlag: 2,
        date: data.birthDayStr,
      })

      if (data.content) {
        that.setData({
          content: data.content,
          isTextleft: true
        })
      }

      if (data.id) {
        that.setData({
          currentID: data.id
        })
      }

      if (data.fileName) {
        that.setData({
          fileName: data.fileName
        })
      }

      if (data.filePath) {
        that.setData({
          filePath: data.filePath
        })
      }

    }).catch(err => {

    })
  },

  getPhoneStatus() {
    Http.get({
      url: config.api.checkPhoneStatus,
    }).then(res => {

    }).catch(err => {
      app.globalData.skip = "redirectTo"
      const skipUrl = "/pages3/resume/resume"
      app.globalData.skipUrl = skipUrl
      wx.redirectTo({
        url: `/pages/getphoneInfo/index?skipUrl=1`
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPhoneStatus()
    this.getUserPhone()
    this.getResumeDetail()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  // },
})