import {
  resumeIntent
} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
import {
  showToast
} from '../../utils/tips.js'
let app = getApp()
Page({
  data: {
    imgServerUrl: imgServerUrl,
    salaryList: [{
      "expectationSalary": null,
      "hightNum": '请选择'
    },
    {
      "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": 8,
      "hightNum": '9000~10000'
    }, {
      "expectationSalary": 9,
      "hightNum": '10000以上'
    }
    ],
    expectationPosition: [{ name: "普工" }, { name: "客服" }, { name: "销售" }, { name: "服务员" }, { name: "物流仓储" }, { name: "司机" }, { name: "才艺技能" }, { name: "文员助理" }, { name: "快递配送" }, { name: "促销导购" }, { name: "展会演出" }, { name: "家教培训" }, { name: "模特" }, { name: "贸易采购" }, { name: "厨师" }, { name: "编辑" }, { name: "线上推广" }, { name: "设计" }, { name: "技工" }, { name: "保洁" }, { name: "其它" }],
    region: {
      code: [],
      value: []
    }, //省市区
    name: '',
    expectationPositionIndex: [],
    salaryIndex: 0,
    hpUserIntentionId: '',
    hpUserResumeId: '',
    disabled: false,
    cover: true,
    position: true,
    address: true,
    salary: true
  },

  onLoad: function (options) {
    let {
      hpUserResumeId
    } = options

    this.setData({
      hpUserResumeId: hpUserResumeId
    })

    if (hpUserResumeId) {
      let userResume = wx.getStorageSync('userResume')
      let region = {
        code: [],
        value: []
      };

      let salaryIndex = 0;
      let expectationArray = [];
      const expectationPosition = this.data.expectationPosition;

      if (userResume && userResume.id) {
        if (userResume.expectationAddress) {
          region.code.push(userResume.expectationProvinceId);
          region.code.push(userResume.expectationCityId);
          region.code.push(userResume.expectationCountryId);
          let arr = userResume.expectationAddress.split("-");
          for (let i = 0; i < arr.length; i++) {
            region.value.push(arr[i]);
          }
        }
        if (userResume.expectationPosition) {
          expectationArray = userResume.expectationPosition.split(',');
          let lengthSalary = this.data.salaryList.length;
          for (let i = 0; i < lengthSalary; i++) {
            if (userResume.expectationSalary == this.data.salaryList[i].hightNum) {
              salaryIndex = i;
            }
          }
          expectationArray.forEach(value => {
            if(value){
              expectationPosition[value].active = true
            }
          })
        }
        this.setData({
          hpUserResumeId: userResume.id,
          expectationPositionIndex: expectationArray,
          salaryIndex: salaryIndex,
          region,
          expectationPosition
        }, _ => {
          this.check()
        })
      }
    }
  },
  changeSalary() {
    this.setData({
      cover: false,
      salary: false
    })
  },
  changeCover(e) {
    const { type } = e.currentTarget.dataset;
    if (type) {
      this.setData({
        [type]: !this.data[type]
      })
    } else {
      this.setData({
        position: true,
        address: true,
        salary: true
      })
    }
    this.setData({
      cover: !this.data.cover
    })
  },
  //改变薪资水平
  bindSalaryChange(e) {
    this.setData({
      salaryIndex: e.detail.value
    }, _ => {
      this.check()
    })
  },
  //改变期望岗位
  changeName(e) {
    const { index } = e.currentTarget.dataset;
    let expectationPositionIndex = this.data.expectationPositionIndex;
    const expectationPosition = this.data.expectationPosition;
    if (expectationPosition[index].active) {
      expectationPosition[index].active = false;
      const _index = expectationPositionIndex.findIndex(value => {
        return Number(value) === index;
      })
      expectationPositionIndex.splice(_index, 1)
    } else {
      expectationPosition[index].active = true;
      expectationPositionIndex.push(index)
    }
    expectationPositionIndex.forEach((value, index) => {
      if (!value && value !== 0) {
        expectationPositionIndex.splice(index, 1)
      }
    })
    this.setData({
      expectationPositionIndex: expectationPositionIndex,
      expectationPosition
    }, _ => {
      this.check()
    })
  },
  //改变期望地点
  bindRegionChange() {
    // 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
    // }, _ => {
    //   this.check()
    // })
    wx.navigateTo({
      url: `/pages/choose-city/index?title=期望地点&&region=${JSON.stringify(this.data.region)}`,
    })
  },
  //提交
  submit() {
    let flag = this.check(true)
    if (!flag) {
      return
    }
    let {
      expectationPositionIndex,
      salaryIndex,
      region,
      salaryList
    } = this.data
    let paramsObj = {
      expectationPosition: expectationPositionIndex.join(','),
      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 => {
      showToast('保存成功', 'success')
      wx.navigateBack()
    })

  },

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