import WxValidate from '../../utils/WxValidate.js'
Page({
  data: {
    form: {
      ageMin: '',
      ageMax: '',
      heightMin: '',
      heightMax: '',
      education: '',
      income: '',
      nativePlace: '',
      marriage: '',
      assetHouse: '',
      assetArea: '',
      assetHouseLoan: '',
      assetCar: '',
      assetCarValue: '',
      assetCarLoan: '',
      constellation: '',
      workStatus: '',
      companyNature: '',
      parents: '',
      composition: '',
      isCommen: ''
    },
    heightArr: [
      '都行',
      '150-155',
      '155-160',
      '160-165',
      '165-170',
      '170-175',
      '175-185',
      '185-190'
    ],
    educationArr: ["都行", "初中", "中专", "高中", "大专", "本科", "硕士", "博士"],
    incomeArr: ["挣不挣都行", "2000-5000", "5000-10000", "10000-20000", "20000以上"],
    nativePlaceArr: ['咋的都行', '本地', '外地'],
    marriageArr: [
      ["不行", "行"],
      []
    ],
    constellationArr: [{
        name: '不限',
        value: '不限',
        checked: true
      },
      {
        name: '白羊座',
        value: '白羊座',
        checked: false
      },
      {
        name: '金牛座',
        value: '金牛座',
        checked: false
      },
      {
        name: '双子座',
        value: '双子座',
        checked: false
      },
      {
        name: '巨蟹座',
        value: '巨蟹座',
        checked: false
      },
      {
        name: '狮子座',
        value: '狮子座',
        checked: false
      },
      {
        name: '处女座',
        value: '处女座',
        checked: false
      },
      {
        name: '天秤座',
        value: '天秤座',
        checked: false
      },
      {
        name: '天蝎座',
        value: '天蝎座',
        checked: false
      },
      {
        name: '射手座',
        value: '射手座',
        checked: false
      },
      {
        name: '摩羯座',
        value: '摩羯座',
        checked: false
      },
      {
        name: '水瓶座',
        value: '水瓶座',
        checked: false
      },
      {
        name: '双鱼座',
        value: '双鱼座',
        checked: false
      }
    ],
    // 工作时间自由、朝九晚五、偶尔加班、经常加班、偶尔出差、经常出差、常有应酬
    workStatusArr: [{
        name: '工作时间自由',
        value: '工作时间自由',
        checked: true
      },
      {
        name: '朝九晚五',
        value: '朝九晚五',
        checked: false
      }, {
        name: '偶尔加班',
        value: '偶尔加班',
        checked: false
      }, {
        name: '经常加班',
        value: '经常加班',
        checked: false
      },
      {
        name: '偶尔出差',
        value: '偶尔出差',
        checked: false
      }, {
        name: '经常出差',
        value: '经常出差',
        checked: false
      }, {
        name: '常有应酬',
        value: '常有应酬',
        checked: false
      }
    ],
    assetHouseArr: ['租房', '购房'],
    assetAreaArr: ['80平以下', '80-120平', '120平以上'],
    assetHouseLoanArr: ['有', '没有'],
    assetCarArr: ['有', '没有'],
    assetCarValueArr: ['10万以下', '10-20万', '20-30万', '30-40万', '40-50万', '50-60万', '60-70万', '70-80万', '80-90万', '90-100万', '100万以上'],
    assetCarLoanArr: ['有贷款', '无贷款'],
    companyNatureArr: ['政府机关', '事业单位', '外企企业', '世界500强', '上市公司', '国有企业', '私营企业', '个体'],
    parentsArr: ['均有退休金', '均无退休金', '父亲有退休金', '母亲有退休金'],
    compositionArr: ['老几都行', '必须独苗一根'],
    isCommenArr: ['愿意', '不愿意']
  },
  educationPicker(e) {
    let education = 'form.education'
    this.setData({
      [education]: this.data.educationArr[e.detail.value]
    })
  },
  incomePicker(e) {
    let income = 'form.income'
    this.setData({
      [income]: this.data.incomeArr[e.detail.value]
    })
  },
  nativePlacePicker(e) {
    let nativePlace = 'form.nativePlace'
    this.setData({
      [nativePlace]: this.data.nativePlaceArr[e.detail.value]
    })
  },
  marriageChange(e) {
    let marriage = 'form.marriage'
    this.setData({
      [marriage]: [this.data.marriageArr[0][e.detail.value[0]], this.data.marriageArr[1][e.detail.value[1]]]
    })
    this.setData({
      marriagemultiIndex: e.detail.value
    })
  },
  marriagePickerColumnChange(e) {
    console.log(e)
    if (e.detail.column == 0 && e.detail.value == 1) {
      this.setData({
        marriageArr: [
          ["行", "不行"],
          ["有小祖宗不行", "有小祖宗也行"]
        ]
      })
    }
    if (e.detail.column == 0 && e.detail.value == 0) {
      this.setData({
        marriageArr: [
          ["行", "不行"],
          []
        ]
      })
    }
  },
  assetHousePickerChange(e) {
    console.log(e)
    let assetHouse = 'form.assetHouse'
    this.setData({
      [assetHouse]: this.data.assetHouseArr[e.detail.value]
    })
  },
  assetAreaPickerChange(e) {
    console.log(e)
    let assetArea = 'form.assetArea'
    this.setData({
      [assetArea]: this.data.assetAreaArr[e.detail.value]
    })
  },
  assetHouseLoanPickerChange(e) {
    console.log(e)
    let assetHouseLoan = 'form.assetHouseLoan'
    this.setData({
      [assetHouseLoan]: this.data.assetHouseLoanArr[e.detail.value]
    })
  },
  assetCarPickerChange(e) {
    console.log(e)
    let assetCar = 'form.assetCar'
    this.setData({
      [assetCar]: this.data.assetCarArr[e.detail.value]
    })
  },
  assetCarValuePickerChange(e) {
    console.log(e)
    let assetCarValue = 'form.assetCarValue'
    this.setData({
      [assetCarValue]: this.data.assetCarValueArr[e.detail.value]
    })
  },
  assetCarLoanPickerChange(e) {
    console.log(e)
    let assetCarLoan = 'form.assetCarLoan'
    this.setData({
      [assetCarLoan]: this.data.assetCarLoanArr[e.detail.value]
    })
  },
  constellationPicker(e) {
    console.log(e.detail.value)
    const items = this.data.constellationArr;
    const values = e.detail.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    this.setData({
      constellationArr: this.data.constellationArr
    })
    let constellation = 'form.constellation'
    this.setData({
      [constellation]: e.detail.value
    })

  },
  workStatusChange(e) {
    console.log(e.detail.value)
    const items = this.data.workStatusArr;
    const values = e.detail.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    this.setData({
      workStatusArr: this.data.workStatusArr
    })
    let workStatus = 'form. workStatus'
    this.setData({
      [workStatus]: e.detail.value
    })
  },
  companyNaturePicker: function (e) {
    let companyNature = 'form.companyNature'
    this.setData({
      [companyNature]: this.data.companyNatureArr[e.detail.value]
    })
  },
  parentsPicker(e) {
    let parents = 'form.parents'
    this.setData({
      [parents]: this.data.parentsArr[e.detail.value]
    })
  },
  compositionPicker(e) {
    let composition = 'form.composition'
    this.setData({
      [composition]: this.data.compositionArr[e.detail.value]
    })
  },
  isCommenPicker(e) {
    let isCommen = 'form.isCommen'
    this.setData({
      [isCommen]: this.data.isCommenArr[e.detail.value]
    })
  },
  formSubmit(e) {
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      ageMin: {
        required: true
      },
      ageMax:{
        required: true
      },
      heightMin:{
        required: true
      },
      heightMax:{
        required: true
      },
      education:{
        required: true
      },
      income:{
        required: true
      },
      nativePlace:{
        required: true
      },
      marriage:{
        required: true
      },
      assetHouse:{
        required: true
      },
      assetArea:{
        required: true
      },
      assetHouseLoan:{
        required: true
      },
      assetCar:{
        required: true
      },
      assetCarValue:{
        required: true
      },
      assetCarLoan:{
        required: true
      },
      constellation:{
        required: true
      },
      workStatus:{
        required: true
      },
      companyNature:{
        required: true
      },
      parents:{
        required: true
      },
      composition:{
        required: true
      },
      isCommen: {
        required: true
      }
    }
    const messages = {
      ageMin: {
        required: '请输入您能接受的最小年纪'
      },
      ageMax:{
        required: '请输入您能接受的最大年纪'
      },
      heightMin:{
        required: '请输入您能接受的最低身高'
      },
      heightMax:{
        required: '请输入您能接受的最高身高'
      },
      education:{
        required: '请输入您能接受的教育程度'
      },
      income:{
        required: '请选择您能接受的收入'
      },
      nativePlace:{
        required: '请选择本地或者外地'
      },
      marriage:{
        required: '请选择您能接受的婚姻状态'
      },
      assetHouse:{
        required: '请选择您可以接受的房子状态'
      },
      assetArea:{
        required: '请选择您可以接受的房子面积'
      },
      assetHouseLoan:{
        required: '请选择您可以接受的房子是否有贷款'
      },
      assetCar:{
        required: '请选择您可以接受的车子状态'
      },
      assetCarValue:{
        required: '请选择您可以接受的车子价值'
      },
      assetCarLoan:{
        required: '请选择您是否可以接受的车子有贷款'
      },
      constellation:{
        required: '请选择您的优先选择星座'
      },
      workStatus:{
        required: '请选择您能接受的工作状态'
      },
      companyNature:{
        required: '请选择您能接受的单位性质'
      },
      parents:{
        required: '请选择您能接受的父母状态'
      },
      composition:{
        required: '请选择您可以接受的家庭构成'
      },
      isCommen: {
        required: '请选择您是否接受与父母同住'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  onLoad: function (options) {
    this.initValidate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})