// pages/upload/upload.js
const app = getApp()
const host = app.globalData.host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    idnoimg_face: "", //正面照
    idnoimg_back: "", //反面照
    halfbody_img: "", //半身照
    education_img: "", //学历照
    oldcard_img:'',
    health_img:"",//体检报告
    title_file:"",//任职文件
    job_exp:"",//从业经历
    id:'',
    train_type:'',
    subject_item:"",//报名科目
    has_healthimg:false,
    has_jobimg:false,
    img:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:JSON.parse(options.id),
      openid: app.globalData.openid
    })
    wx.request({
      url: host + '/api/wx/signup',
      method: 'GET',
      data: { id: options.id },
      success:res=>{
        this.setData({
          idnoimg_face: res.data.data.idnoimg_face, //正面照
          idnoimg_back: res.data.data.idnoimg_back, //反面照
          halfbody_img: res.data.data.halfbody_img, //半身照
          education_img: res.data.data.education_img, //学历照
          oldcard_img: res.data.data.oldcard_img,
          train_type: res.data.data.train_type,
          health_img: res.data.data.health_img,
          subject_item: res.data.data.subject_item,
          has_healthimg:res.data.data.subject_item.split("|")[0].indexOf("特种设备")!=-1,
          has_jobimg:res.data.data.subject_item.split("|")[0].indexOf("安全生产知识和管理")!=-1,
          title_file:res.data.data.title_file,
          job_exp:res.data.data.job_exp
        })
        if (!res.data.data.idnoimg_face){
          wx.request({
            url: host + '/api/wx/authinfo',
            header: {
              openid: app.globalData.openid
            },
            success: res => {
              this.setData({
                idnoimg_face: res.data.data.idnoimg_face, //正面照
                idnoimg_back: res.data.data.idnoimg_back, //反面照
                halfbody_img: res.data.data.halfbody_img, //半身照
                education_img: res.data.data.education_img, //学历照
                oldcard_img: res.data.data.oldcard_img,
                health_img: res.data.data.health_img,
              })
            }
          })
        }
      }
    })

  },
  post:function(){
    var formData ={}
    formData.id=this.data.id
    
    formData.idnoimg_face = this.data.idnoimg_face
    formData.idnoimg_back = this.data.idnoimg_back
    formData.halfbody_img = this.data.halfbody_img
    formData.education_img = this.data.education_img
    formData.oldcard_img = this.data.oldcard_img
    formData.health_img = this.data.health_img
    formData.title_file = this.data.title_file
    formData.job_exp = this.data.job_exp
    if (!formData.idnoimg_face) {
      wx.showToast({
        title: '正面照不存在！',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!formData.idnoimg_back) {
      wx.showToast({
        title: '反面照不存在！',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!formData.halfbody_img) {
      wx.showToast({
        title: '半身照不存在！',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!formData.education_img) {
      wx.showToast({
        title: '学历照不存在！',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (this.data.train_type == '换证' || this.data.train_type == '复审') {
      if (!formData.oldcard_img){
        wx.showToast({
          title: '换证/复审证件照不存在！',
          icon: 'none',
          duration: 2000
        })
        return false
      }     
    }
    if (this.data.subject_item.split("|")[0].indexOf("特种设备") != -1) {
      if (!formData.health_img) {
        wx.showToast({
          title: '体检证明照片不存在！',
          icon: 'none',
          duration: 2000
        })
        return false
      }
    }
    if (this.data.has_jobimg) {
      if (!formData.title_file){
        wx.showToast({
          title: '请上传任职文件！',
          icon: 'none',
          duration: 2000
        })
        return false
      }     
    }
    if (this.data.has_jobimg) {
      if (!formData.job_exp){
        wx.showToast({
          title: '请上传从业经验文件！',
          icon: 'none',
          duration: 2000
        })
        return false
      }     
    }
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: host+'/api/wx/signup',
      method:'PUT',
      header: {
        openid: this.data.openid
      },
      data: formData,
      success:res=>{
        if(res.data.code == 0){
          wx.hideLoading()
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function(){
              wx.switchTab({
                url: '../apply/apply',
              })
          },2000)
        }
      },
      fail:res=>{
        wx.hideLoading()
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  showImg:function(e){
    this.setData({
      show: true,
      img: '../../images/zj' + e.target.dataset.id + '.jpg'
    })
  },
  hideImg: function (e) {
    this.setData({
      show: false
    })
  },
  takePhoto:function(e){
    var id=e.target.dataset.id
    var _this=this
    wx.chooseImage({
      count:1,
      success: function(res) {
        
        wx.uploadFile({
          url: host+'/api/wx/uploadfile',
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'file': res.tempFilePaths[0]
          },
          success:res=>{
            wx.hideLoading()
            var data=JSON.parse(res.data)
            wx.showToast({
              title: '上传成功',
              icon: 'none',
              duration: 2000
            })
            if(id == 1){  //正面
              _this.setData({
                idnoimg_face: data.data.url
              })
            }
            if (id == 2) {  //反面
              _this.setData({
                idnoimg_back: data.data.url
              })
            }
            if (id == 3) {  //半身照
              _this.setData({
                halfbody_img: data.data.url
              })
            }
            if (id == 4) {  //学历照
              _this.setData({
                education_img: data.data.url
              })
            }
            if (id == 5) {
              _this.setData({
                oldcard_img: data.data.url
              })
            }
            if (id == 6) {
              _this.setData({
                health_img: data.data.url
              })
            }
            // 任职文件
            if (id == 7) {
              _this.setData({
                title_file: data.data.url
              })
            }
            // 从业经验
            if (id == 8) {
              _this.setData({
                job_exp: data.data.url
              })
            }
          },
          fail: res => {
            wx.hideLoading()
            wx.showToast({
              title: res,
              icon: 'none',
              duration: 2000
            })
          }
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})