import {
  getResume
} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
import {
  showToast
} from '../../utils/tips.js'
import {
  formatTime
} from '../../utils/util.js'
const app = getApp();

Page({
  data: {
    imgServerUrl: imgServerUrl,
    hpUserResumeId: '', //是否有简历
    eduList: ["请选择", "小学", "中学", "高中", "大专", "本科", "研究生", "博士", "博士后"],
    eduName: '', //学历
    expYear: "", //工作几年
    hpUserResumeId: '', //用户简历表id
    expectationPosition: ["普工", "客服", "销售", "服务员", "物流仓储", "司机", "才艺技能", "文员助理", "快递配送", "促销导购", "展会演出", "家教培训", "模特", "贸易采购", "厨师", "编辑", "线上推广", "设计", "技工", "保洁", "其它"],
    salaryList: [{
      "expectationSalary": -1,
      "hightNum": '不限'
    },
    {
      "expectationSalary": 0,
      "hightNum": '1000~2000'
    },
    {
      "expectationSalary": 1,
      "hightNum": '2000~3000'
    },
    {
      "expectationSalary": 2,
      "hightNum": '3000~4000'
    },
    {
      "expectationSalary": 3,
      "hightNum": '4000~5000'
    },
    {
      "expectationSalary": 4,
      "hightNum": '5000~6000'
    },
    {
      "expectationSalary": 5,
      "hightNum": '6000~7000'
    },
    {
      "expectationSalary": 6,
      "hightNum": '7000~8000'
    },
    {
      "expectationSalary": 7,
      "hightNum": '8000~9000'
    },
    {
      "expectationSalary": 9,
      "hightNum": '10000以上'
    }
    ],
  },
  onLoad: function (options) {
    var hpUserResumeId = options.hpUserResumeId;
    this.setData({
      hpUserResumeId
    })
    if (!hpUserResumeId) {
      wx.navigateTo({
        url: '../user-info/index',
      })
    }
  },
  onShow: function (options) {
    this.fetchData();
  },
  //获取用户简历详情
  fetchData() {
    let paramsObj = {
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    getResume(paramsObj).then(data => {
      if (!data.data.userResume) {
        wx.navigateTo({
          url: '../user-info/index',
        })
      }
      let {
        userResume
      } = data.data
      var eduName = this.data.eduList[userResume.highestQualification];
      var hpUserResumeId = userResume.hpUserResumeId;
      var expList = userResume.hpResumeWorkExperienceList;
      wx.setStorageSync("expList", expList);
      if (!userResume.headImg) {
        userResume.headImg = `${imgServerUrl}/images/avatar/man.png`;
      }
      if (userResume.expectationSalary) {
        var salaryList = this.data.salaryList;
        for (var i = 0; i < salaryList.length; i++) {
          if (salaryList[i].expectationSalary == userResume.expectationSalary) {
            userResume.expectationSalary = salaryList[i].hightNum
          }
        }
      }
      var year = userResume.birthYear;
      if (year) {
        var date = new Date;
        var year1 = date.getFullYear();
        year = year1 - year + 1;
      }
      wx.setStorageSync('userResume', userResume)
      userResume.expectationAddress = userResume.expectationAddress ? userResume.expectationAddress.split('-').join(' ') : ''
      userResume.expectationSalary = userResume.expectationSalary ? userResume.expectationSalary.split('~').join('-') : ''

      this.setData({
        userResume,
        eduName: eduName,
        expList: expList,
        year: year
      })
    })
  },

  // 修改基本信息
  toUserInfo() {
    wx.navigateTo({
      url: '../user-info/user-info?hpUserResumeId=' + this.data.hpUserResumeId,
    })
  },
  // 修改求职意向
  toUserJob(e) {
    wx.navigateTo({
      url: '../user-job/index?hpUserResumeId=' + this.data.hpUserResumeId,
    })
  },
  // 修改工作经验
  toUserWorks(e) {
    let {
      type,
      index
    } = e.detail;
    if (type == "edit") {
      wx.navigateTo({
        url: '../user-works/index?hpUserResumeId=' + this.data.hpUserResumeId + "&index=" + index,
      })
    } else {
      wx.navigateTo({
        url: '../user-works/index?hpUserResumeId=' + this.data.hpUserResumeId
      })
    }
  },
  //修改教育背景
  toUserEdu(e) {
    let {
      type,
      index
    } = e.currentTarget.dataset
    if (type == "edit") {
      wx.navigateTo({
        url: '../user-education/index?hpUserResumeId=' + this.data.hpUserResumeId + "&index=" + index,
      })
    } else {
      wx.navigateTo({
        url: '../user-education/index?hpUserResumeId=' + this.data.hpUserResumeId,
      })
    }
  }
})