import {
  resumeIntent,
  getSalaryList
} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
import {
  showToast
} from '../../utils/tips.js'
var app = getApp()
Page({
  data: {
    imgServerUrl: imgServerUrl,
    salaryList: [{
        "expectationSalary": null,
        "hightNum": '请选择'
      },
      {
        "expectationSalary": -1,
        "hightNum": '不限'
      },
      {
        "expectationSalary": 0,
        "hightNum": '1000~2000'
      },
      {
        "expectationSalary": 1,
        "hightNum": '2001~3000'
      },
      {
        "expectationSalary": 2,
        "hightNum": '3001~4000'
      },
      {
        "expectationSalary": 3,
        "hightNum": '4001~5000'
      },
      {
        "expectationSalary": 4,
        "hightNum": '5001~6000'
      },
      {
        "expectationSalary": 5,
        "hightNum": '6001~7000'
      },
      {
        "expectationSalary": 6,
        "hightNum": '7001~8000'
      },
      {
        "expectationSalary": 7,
        "hightNum": '8001~9000'
      },
      {
        "expectationSalary": 8,
        "hightNum": '9001~10000'
      }, {
        "expectationSalary": 9,
        "hightNum": '10000以上'
      }
    ],
    expectationPosition: ["普工", "客服", "销售", "服务员", "物流仓储", "司机", "才艺技能", "文员助理", "快递配送", "促销导购", "展会演出", "家教培训", "模特", "贸易采购", "厨师", "编辑", "线上推广", "设计", "技工", "保洁", "其它"],
    region: {
      code: [],
      value: []
    }, //省市区
    name: '',
    expectationPositionIndex: 0,
    salaryIndex: 0,
    hpUserIntentionId: '',
    hpUserResumeId: ''
  },

  onLoad: function(options) {
    console.log(options)
    let {
      hpUserResumeId
    } = options

    this.setData({
      hpUserResumeId: hpUserResumeId
    })

    if (hpUserResumeId) {
      let userResume = wx.getStorageSync('userResume')

      this.data.region.code = this.data.region.code.concat(userResume.expectationProvinceId);
      this.data.region.code = this.data.region.code.concat(userResume.expectationCityId);
      this.data.region.code = this.data.region.code.concat(userResume.expectationCountryId);
      var arr = userResume.expectationAddress.split("-");
      for(var i=0;i<arr.length;i++){
        this.data.region.value = this.data.region.value.concat(arr[i]);
      }
      var expectationArray = userResume.expectationPosition;
      var lengthSalary = this.data.salaryList.length;
      var salaryIndex;
      for (var i = 0; i < lengthSalary; i++) {
        if (userResume.expectationSalary == this.data.salaryList[i].hightNum) {
          salaryIndex = i;
        }
      }
      console.log("ggg", this.data.region)
      if (userResume.id) {
        this.setData({
          hpUserResumeId: userResume.id,
          expectationPositionIndex: expectationArray,
          salaryIndex: salaryIndex,
          region: this.data.region
        })
      }
    }

  },

  //改变薪资水平
  bindSalaryChange(e) {
    console.log('picker改变薪资水平，携带值为', e.detail)
    this.setData({
      salaryIndex: e.detail.value
    })
  },
  //改变期望岗位
  changeName(e) {
    this.setData({
      expectationPositionIndex: e.detail.value
    })
  },
  //改变期望地点
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail)
    this.data.region.code = e.detail.code;
    this.data.region.value = e.detail.value;
    this.setData({
      region: this.data.region
    })
  },
  //提交
  submit() {
    let flag = this.check()
    if (!flag) {
      return
    }
    let {
      expectationPositionIndex,
      salaryIndex,
      region,
      hpUserResumeId,
      salaryList
    } = this.data
    let paramsObj = {
      expectationPosition: expectationPositionIndex,
      expectationSalary: salaryList[salaryIndex].expectationSalary,
      expectationProvinceId: region.code[0],
      expectationCityId: region.code[1],
      expectationCountryId: region.code[2],
      expectationAddress: region.value[0] +'-'+ region.value[1] +'-' +region.value[2],
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    resumeIntent(paramsObj).then(data => {
      console.log(data)
      showToast('保存成功', 'success')
      wx.navigateBack()
    })

  },

  //验证
  check() {
    let {
      region,
      salaryList,
      salaryIndex
    } = this.data
    if (salaryList[salaryIndex].expectationSalary == null) {
      showToast('请选择期望薪资');
      return false
    }
    if (region.value.length == 0) {
      showToast('请选择期望地点');
      return false
    }
    return true
  }
})