// pages/signOrg/signOrg.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sourceUrl: app.baseUrls.testUrl + app.baseUrls.baseImgUrl,
    cultrueAll: [],
    cultrueList: [],
    cultrueIdx: 0,
    cultrueId: '',
    signInfo: {
      applyFenlei: 1,
      applyName: '',
      applySex: '',
      applyCardNumber: '',
      applyPhone: '',
      applyUnitName: '',
      applyWorkTypeId: '',
      applyInstitutionId: '',
      idCardFrontPhoto: '',
      idCardBackPhoto: '',
      educationPhoto: '',
      applyUserCardPhoto: '',
      applyTakeCardPhoto: '',
      takeCardNumber: '',
      cardTimeLimit: '2021-01-01',
      rePeiXunDate: '2021-01-01',
      applyRePeiXunPhoto: '',
      applyUnitType: '',
      applyUnitBelong: '',
      applySchool: '',
      applySubject: '',
      applyLeiBie: '',
      applyCultureLevel: ''
    },
    pageType: ''
  },
  getCulture: function () {
    var self = this
    app.restAjax.get(app.restAjax.path(app.apis.getCultrue, [app.baseUrls.testUrl]), {}, {
      headers: {
        token: wx.getStorageSync('token')
      }
    },
    function (code, data) {
      var result = ['请选择文化程度']
      for (let i = 0; i < data.length; i++) {
        result.push(data[i].dataName)
      }
      self.setData({
        cultrueAll: data,
        cultrueList: result
      })
    },
    function (code, err) {
      console.log(err)
    })
  },
  bindPickerChange: function (e) {
    var self = this
    self.setData({
      cultrueIdx: e.detail.value,
      cultrueId: self.data.cultrueAll[e.detail.value - 1].dataId,
      ['signInfo.applyCultureLevel']: self.data.cultrueAll[e.detail.value - 1].dataId
    })
  },
  changeType: function (e) {
    if (e.detail.value != this.data.signInfo.applyFenlei) {
      this.setData({
        ['signInfo.applyFenlei']: e.detail.value
      })
      if (e.detail.value == '1') {
        this.setData({
          ['signInfo.applyRePeiXunPhoto']: '',
          ['signInfo.applyTakeCardPhoto']: ''
        })
      } else if (e.detail.value == '3') {
        this.setData({
          ['signInfo.applyRePeiXunPhoto']: ''
        })
      }
    }
  },
  // 获取个人信息
  getPersonInfo: function () {
    var self = this
    app.restAjax.get(app.restAjax.path(app.apis.gerPersonInfo, [app.baseUrls.testUrl]), {}, {
        headers: {
            token: wx.getStorageSync('token')
        }
    },function (code, data) {
        for (let i = 0; i < self.data.cultrueAll.length; i++) {
            if (self.data.cultrueAll[i].dataId == data.education) {
                self.setData({
                    cultrueIdx: i + 1,
                    ['signInfo.applyCultureLevel'] : self.data.cultrueAll[i].dataId
                })
            }
        }
        self.setData({
          ['signInfo.applyName']: data.fullName,
          ['signInfo.applySex']: data.gender,
          ['signInfo.applyCardNumber']: data.idCardNumber,
          ['signInfo.applyPhone']: data.telephone,
          ['signInfo.applyUserCardPhoto']: data.userPhotoId,
          ['signInfo.educationPhoto']: data.educationPhoto,
          ['signInfo.idCardBackPhoto']: data.idCardBack,
          ['signInfo.idCardFrontPhoto']: data.idCardFront,
        })
    },
    function (code, err) {
        console.log(err)
    })
  },
  editInfo: function (e) {
    var self = this
    var type = e.currentTarget.dataset.type
    if (type == 'name') {
      self.setData({
        ['signInfo.applyName']: e.detail.value
      })
    } else if (type == 'card') {
      self.setData({
        ['signInfo.applyCardNumber']: e.detail.value
      })
    } else if (type == 'phone') {
      self.setData({
        ['signInfo.applyPhone']: e.detail.value
      })
    } else if (type == 'work') {
      self.setData({
        ['signInfo.applyUnitName']: e.detail.value
      })
    } else if (type == 'take') {
      self.setData({
        ['signInfo.takeCardNumber']: e.detail.value
      })
    } else if (type == 'type') {
      self.setData({
        ['signInfo.applyUnitType']: e.detail.value
      })
    } else if (type == 'position') {
      self.setData({
        ['signInfo.applyUnitWork']: e.detail.value
      })
    } else if (type == 'belong') {
      self.setData({
        ['signInfo.applyUnitBelong']: e.detail.value
      })
    } else if (type == 'school') {
      self.setData({
        ['signInfo.applySchool']: e.detail.value
      })
    } else if (type == 'subject') {
      self.setData({
        ['signInfo.applySubject']: e.detail.value
      })
    } else if (type == 'zhicheng') {
      self.setData({
        ['signInfo.applyZhiCheng']: e.detail.value
      })
    } else if (type == 'leibie') {
      self.setData({
        ['signInfo.applyLeiBie']: e.detail.value
      })
    }
  },
  submitSignOrg: function () {
    var self = this
    if (!self.data.signInfo.applyFenlei) {
      wx.showToast({
        title: '请选择培训类型！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyName) {
      wx.showToast({
        title: '姓名不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyCultureLevel) {
      wx.showToast({
        title: '请选择文化程度！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyCardNumber) {
      wx.showToast({
        title: '身份证号不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyPhone) {
      wx.showToast({
        title: '手机号不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyUnitName) {
      wx.showToast({
        title: '工作单位不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyRePeiXunPhoto && self.data.signInfo.applyFenlei == '2') {
      wx.showToast({
        title: '请上传再培训证明照片！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyTakeCardPhoto && self.data.signInfo.applyFenlei == '2') {
      wx.showToast({
        title: '请上传证书个人信息页！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyTakeCardPhoto && self.data.signInfo.applyFenlei == '3') {
      wx.showToast({
        title: '请上传证书个人信息页！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.takeCardNumber && self.data.signInfo.applyFenlei != '1') {
      wx.showToast({
        title: '证件号码不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyLeiBie && self.data.pageType == '530279c5-32a9-44db-bbf0-a04ef1cdae66') {
      wx.showToast({
        title: '培训类别不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyUnitType && self.data.pageType == '530279c5-32a9-44db-bbf0-a04ef1cdae66') {
      wx.showToast({
        title: '单位类型不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyUnitWork && self.data.pageType == '530279c5-32a9-44db-bbf0-a04ef1cdae66') {
      wx.showToast({
        title: '职务不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyUnitBelong && self.data.pageType == '530279c5-32a9-44db-bbf0-a04ef1cdae66') {
      wx.showToast({
        title: '单位所在区域不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applySchool && self.data.pageType == '530279c5-32a9-44db-bbf0-a04ef1cdae66') {
      wx.showToast({
        title: '毕业院校不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applySubject && self.data.pageType == '530279c5-32a9-44db-bbf0-a04ef1cdae66') {
      wx.showToast({
        title: '所学专业不能为空！',
        icon: 'error'
      })
      return
    }
    if (!self.data.signInfo.applyZhiCheng && self.data.pageType == '530279c5-32a9-44db-bbf0-a04ef1cdae66') {
      wx.showToast({
        title: '职称不能为空！',
        icon: 'error'
      })
      return
    }
    app.restAjax.post(app.restAjax.path(app.apis.signOrg, [app.baseUrls.testUrl]), self.data.signInfo, {
        headers: {
            token: wx.getStorageSync('token')
        }
      },function (code, data) {
        if (code == '200') {
          wx.showToast({
            title: '报名成功！',
            icon: 'success'
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '../index/index',
            })
          }, 1500)
        }
      },function (code, err) {
          console.log(err)
    })
  },
  // 上传图片
  uploadImg: function (e) {
    var self = this
    var from = e.currentTarget.dataset.type
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        app.restAjax.file(app.restAjax.path(app.apis.doUploadImg, [app.baseUrls.testUrl]), res.tempFilePaths[0],'image', {
            headers: {
              token: wx.getStorageSync('token')
            }
          },
          function (code, data) {
            var result = JSON.parse(data).data
            if (from == 'take') {
              self.setData({
                ['signInfo.applyTakeCardPhoto']: result
              })
            } else if (from == 'photo') {
              self.setData({
                ['signInfo.applyUserCardPhoto']: result
              })
            } else if (from == 'edu') {
              self.setData({
                ['signInfo.educationPhoto']: result
              })
            } else if (from == 'cardF') {
              self.setData({
                ['signInfo.idCardFrontPhoto']: result
              })
            } else if (from == 'cardB') {
              self.setData({
                ['signInfo.idCardBackPhoto']: result
              })
            } else if (from == 'person') {
              self.setData({
                ['signInfo.applyTakeCardPhoto']: result
              })
            } else {
              self.setData({
                ['signInfo.applyRePeiXunPhoto']: result
              })
            }
          },
          function (code, err) {
            console.log(err)
          }
        )
      }
    })
},
  bindDateChange: function (e) {
    var self = this
    if (e.currentTarget.dataset.type == 'limit') {
      self.setData({
        ['signInfo.cardTimeLimit']: e.detail.value
      })
    } else {
      self.setData({
        ['signInfo.rePeiXunDate']: e.detail.value
      })
    }
    
  },
  getPageType: function () {
    var self = this
    app.restAjax.get(app.restAjax.path(app.apis.getPageType, [app.baseUrls.testUrl]), {
      workerCatalog: self.data.signInfo.applyWorkTypeId
    }, {
      headers: {
          token: wx.getStorageSync('token')
      }
    },function (code, data) {
      self.setData({
        pageType: data.parentWorkerCatalog
      })
    },function (code, err) {
        console.log(err)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ['signInfo.applyWorkTypeId']: options.applyWorkTypeId,
      ['signInfo.applyInstitutionId']: options.applyInstitutionId
    })
    this.getCulture()
    this.getPersonInfo()
    this.getPageType()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})