import {
  imgServerUrl,
  imgServerUrl_new
} from '../../config/config.js'
import {
  resumeBase,
  eduList,
  groupApply,
  positionApply
} from '../../services/index.js'
import {
  uploadImg
} from '../../services/uploadFile.js'
import {
  showToast
} from '../../utils/tips.js'
import {
  argusToTimestamp
} from '../../utils/util.js'
import {
  updataStorageData
} from '../../utils/storage.js'
const app = getApp();

Page({
  data: {
    imgServerUrl: imgServerUrl,
    imgServerUrl_new: imgServerUrl_new,
    sex: ["男", "女"],
    sex_index: 0,
    eduList: ["请选择", "小学", "中学", "高中", "大专", "本科", "研究生", "博士", "博士后"],
    eduIndex: 0,
    avatar: '',
    headImg: '',
    name: '',
    year: '',
    iphone: '',
    hpUserResumeId: null
  },

  onLoad: function(options) {

    let {
      hpUserResumeId = null, progress = 0
    } = options
    this.setData({
      hpUserResumeId
    })
    this.setData({
      avatar: `${imgServerUrl}/images/avatar/man.png`,
      endDate: new Date().getFullYear(),
      year: '请选择'
    })
    let phone = updataStorageData('phone')
    if (phone) {
      this.setData({
        iphone: phone
      })
    }
    if (hpUserResumeId) {
      let userResume = wx.getStorageSync('userResume')
      if (userResume.id) {
        this.setData({
          hpUserResumeId: userResume.id,
          avatar: userResume.headImg,
          name: userResume.realName,
          sex_index: userResume.gender == 1 ? 0 : 1,
          year: userResume.birthYear,
          iphone: userResume.phone,
          eduIndex: userResume.highestQualification ? Number(userResume.highestQualification) : 0
        })
      }
    }
  },

  //改变学历
  bindPickerChange(e) {
    console.log(e)
    let {
      value
    } = e.detail
    this.setData({
      eduIndex: value
    })
  },
  //改变性别
  bindSexChange(e) {
    console.log(e)
    let {
      value
    } = e.detail
    this.setData({
      sex_index: value
    })
  },
  //姓名改变
  changeName(e) {
    console.log(e)
    let {
      value
    } = e.detail
    this.setData({
      name: value.excludeSpecial().excludeSpace()
    })
  },
  //改变出生年月
  changeYear(e) {
    console.log(e)
    let {
      value
    } = e.detail
    this.setData({
      year: value
    })
  },
  //手机号改变
  changeIphone(e) {
    console.log(e)
    let {
      value
    } = e.detail
    this.setData({
      iphone: value.excludeSpecial().excludeSpace()
    })
  },

  //上传图片
  changeImg() {
    var up_type = '0';
    uploadImg(up_type).then(res => {
      let image = res.data.imgUrl
      this.setData({
        avatar: `${imgServerUrl_new}` + image,
        headImg: image
      })
    })
  },

  //验证
  check() {
    let {
      avatar,
      name,
      year,
      iphone
    } = this.data
    // if (avatar == (imgServerUrl + "/images/avatar/man.png")) {
    //   showToast('请上传头像');
    //   return false;
    // }
    if (name == "") {
      showToast('请输入用户名');
      return false;
    }
    if (this.data.iphone == "" || this.data.iphone.length != 11) {
      showToast('请输入11位手机号');
      return false;
    }
    if (this.data.year == "" || this.data.year == '请选择') {
      showToast('请选择出生年月');
      return false;
    }
    if (this.data.eduIndex == "" || this.data.eduIndex == '0') {
      showToast('请选择您的最高学历');
      return false;
    }
    return true
  },

  //保存
  submit() {
    let flag = this.check()
    if (!flag) {
      return
    }
    let {
      sex_index,
      eduIndex,
      avatar,
      headImg,
      name,
      year,
      iphone,
      hpUserResumeId
    } = this.data
    let paramsObj = {
      highestQualification: eduIndex,
      birthYear: year,
      gender: sex_index == 0 ? 1 : 2,
      realName: name,
      phone: iphone,
      avatar: headImg,
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    resumeBase(paramsObj).then(data => {
      showToast('保存成功', 'success');
      wx.setStorageSync("userResume", data.data.userResume);
      wx.navigateBack();
    })
  },
  onError(err) {
    app.aldstat.sendEvent('报错', {
      'err': err
    });
  },
})