import {
  imgServerUrl,
  imgServerUrl_new
} from '../../config/config.js'
import {
  resumeBase} from '../../services/index.js'
import {
  uploadImg
} from '../../services/uploadFile.js'
import {
  showToast
} from '../../utils/tips.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,
    wechatNumber: '',
    disabled: false
  },

  onLoad: function (options) {
    let {
      hpUserResumeId = null    } = 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,
          wechatNumber:userResume.wechatNumber,
          eduIndex: userResume.highestQualification ? Number(userResume.highestQualification) : 0
        }, () => {
          this.check()
        })
      }
    }
  },

  //改变学历
  bindPickerChange(e) {
    let {
      value
    } = e.detail
    this.setData({
      eduIndex: value
    }, _ => {
      this.check()
    })
  },
  //改变性别
  bindSexChange(e) {
    let {
      value
    } = e.detail
    this.setData({
      sex_index: value
    }, _ => {
      this.check()
    })
  },
  //姓名改变
  changeName(e) {
    let {
      value
    } = e.detail
    this.setData({
      name: value.excludeSpecial().excludeSpace()
    }, _ => {
      this.check()
    })
  },
  //改变出生年月
  changeYear(e) {
    let {
      value
    } = e.detail
    this.setData({
      year: value
    }, _ => {
      this.check()
    })
  },
  //手机号改变
  changeIphone(e) {
    let {
      value
    } = e.detail
    this.setData({
      iphone: value.excludeSpecial().excludeSpace()
    }, _ => {
      this.check()
    })
  },
  changeWechatNumber(e) {
    let {
      value
    } = e.detail
    this.setData({
      wechatNumber: value
    }, _ => {
      this.check()
    })
  },
  //上传图片
  changeImg() {
    var up_type = '0';
    uploadImg(up_type).then(res => {
      let image = res.data.imgUrl
      this.setData({
        avatar: `${imgServerUrl_new}` + image,
        headImg: image
      })
    })
  },

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

  //保存
  submit() {
    let flag = this.check(true)
    if (!flag) {
      return
    }
    let {
      sex_index,
      eduIndex,
      headImg,
      name,
      year,
      iphone,
      wechatNumber    } = this.data
    let paramsObj = {
      highestQualification: eduIndex,
      birthYear: year,
      gender: sex_index == 0 ? 1 : 2,
      realName: name,
      phone: iphone,
      wechatNumber,
      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
    });
  },
})