import {
  resumeExp
} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
import {
  showToast
} from '../../utils/tips.js'
import {
  formatNumber,
  formateYM,
  formatetime,
} from '../../utils/util.js'
const app = getApp();
let end_time = [];
Page({
  data: {
    imgServerUrl: imgServerUrl,
    comName: '',
    jobName: '',
    startDate: '请选择',
    endDate: '请选择',
    startDateEnd: '',
    endTime: formatetime(new Date(), '-', false),
    hpUserExpId: '', //求职者工作经验表
    hpUserResumeId: '', //用户简历表id 
    years: [],
    months: [],
    pick_view: true,
    touch_end: true,
    disabled: false,
    show_delete: false
  },
  onLoad: function (options) {
    this.pushYear();
    // this.pushMonths();
    let {
      hpUserResumeId,
      index
    } = options
    if (typeof index != "undefined") {
      let expList = wx.getStorageSync('expList')[index]
      this.setData({
        comName: expList.workCompany,
        jobName: expList.workPosition,
        hpUserResumeId: expList.resumeId,
        hpUserExpId: expList.id,
        startDate: expList.workStartDate ? expList.workStartDate : "请选择",
        endDate: expList.workEndDate ? expList.workEndDate : "请选择",
        show_delete: true
      }, _ => {
        this.check()
      })
    } else {
      this.setData({
        hpUserResumeId
      }, _ => {
        this.check()
      })
    }
  },
  pushYear() {
    const year = new Date().getFullYear();
    const mouth = new Date().getMonth();
    const _len = year - 2015;
    const years = this.data.years;
    for (let i = 0; i <= _len; i++) {
      years.push(2015 + i + '年')
    }
    years.push('至今');
    end_time[0] = years.length - 2;
    end_time[1] = mouth;
    this.setData({
      years,
    }, _ => {
      this.pushMonths(end_time);
    })
  },
  pushMonths(end_time) {
    // const end_time = this.data.end_time;
    const mouth = new Date().getMonth() + 1;
    const months = [];
    const _len = this.data.years.length;
    if (this.data.years[end_time[0]] === '至今') {
      this.setData({
        months: []
      }, _ => {
        this.setData({
          end_time
        })
      })
      return
    }
    if (end_time[0] === _len - 2) {
      for (let i = 0; i < mouth; i++) {
        months.push(i + 1)
      }
      this.setData({
        months: months.map(formatNumber)
      }, _ => {
        this.setData({
          end_time
        })
      })
      return
    }
    this.setData({
      months: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
    }, _ => {
      this.setData({
        end_time
      })
    })
  },
  showEndDate() {
    this.setData({
      pick_view: false
    })
  },
  endDateCancel() {
    this.setData({
      pick_view: true
    })
  },
  endDateSure() {
    if (this.data.touch_end) {
      const years = this.data.years;
      const months = this.data.months;
      if (end_time[0] === years.length - 1) {
        this.setData({
          endDate: years[years.length - 1],
          pick_view: true,
        }, _ => {
          this.check()
        })
      } else {
        this.setData({
          endDate: `${years[end_time[0]]}${Number(months[end_time[1]])}月`,
          pick_view: true,
        }, _ => {
          this.check()
        })
      }
    }
  },
  catchtap() {

  },
  touchEnd() {
    this.setData({
      touch_end: true
    })
  },
  touchStart() {
    this.setData({
      touch_end: false
    })
  },
  //入职时间
  startDateChange(e) {
    const time = e.detail.value.split('-');
    this.setData({
      startDate: `${time[0]}年${time[1]}月`
    }, _ => {
      this.check()
    })
  },
  //离职时间
  endDateChange(e) {
    end_time = e.detail.value;
    if (end_time[1] > this.data.months.length - 1) {
      end_time[1] = this.data.months.length - 1
    }
    if (end_time[1] < 0) {
      end_time[1] = 0
    }
    // this.setData({
    //   end_time: end_time,
    // })
    this.pushMonths(end_time);
    // this.setData({
    //   endDate: formateYM(e.detail.value)
    // })
  },
  //公司名称
  changeComName(e) {
    this.setData({
      comName: e.detail.value.excludeSpecial().excludeSpace()
    }, _ => {
      this.check()
    })
  },
  //工作职位
  changeJobName(e) {
    this.setData({
      jobName: e.detail.value.excludeSpecial().excludeSpace()
    }, _ => {
      this.check()
    })
  },
  delete() {

  },
  //验证
  check(status = false) {
    let {
      comName,
      jobName,
      startDate,
      endDate
    } = this.data;
    this.setData({
      disabled: false
    })
    if (comName == "") {
      status && showToast('请填写公司名称')
      return false
    }
    if (jobName == "") {
      status && showToast("请填写职位")
      return false
    }
    if (startDate == '请选择') {
      status && showToast("请选择入职时间")
      return false
    }
    if (endDate == '请选择') {
      status && showToast("请选择离职时间")
      return false
    }
    this.setData({
      disabled: true
    })
    return true
  },
  submit() {
    let flag = this.check(true)
    if (!flag) {
      return
    }
    let {
      comName,
      jobName,
      hpUserResumeId,
      hpUserExpId,
      startDate,
      endDate
    } = this.data
    let paramsObj = {
      resumeId: hpUserResumeId,
      workCompany: comName,
      workPosition: jobName,
      workStartDate: startDate,
      workEndDate: endDate,
      workExperienceId: hpUserExpId,
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    resumeExp(paramsObj).then(data => {
      showToast('保存成功', 'success')
      wx.navigateBack()
    })
  },
})