import WxValidate from '../../utils/WxValidate.js'
Page({
  data: {
    form: {
      height: '',
      weight: '',
      education: '',
      income: '',
      marriage: '',
      children: '',
      assetHouse: '',
      assetArea: '',
      assetHouseLoan: '',
      assetCar: '',
      assetCarValue: '',
      assetCarLoan: '',
      isCommen: '',
      controlTime: '',
      workStatus: '',
      companyNature: '',
      company: '',
      parents: '',
      fatherWork: '',
      motherWork: '',
      composition: '',
      motionState: '',
      hobby: ''
    },
    heightArr: [
      '150以下',
      '150-155',
      '155-160',
      '160-165',
      '165-170',
      '170-175',
      '175-185',
      '185-190',
      '190以上'
    ],
    weightArr: [
      "50kg以下", "50-60", "60-70", "70-80", "80-90", "90-100", "100以上"
    ],
    educationArr: ["初中", "中专", "高中", "大专", "本科", "硕士", "博士"],
    incomeArr: ["2000-5000", "5000-10000", "10000-20000", "20000以上"],
    marriageList: [{
        name: '未婚',
        value: '未婚',
        checked: true
      },
      {
        name: '离异',
        value: '离异',
        checked: false
      }
    ],
    childrenArr: [{
        name: '无',
        value: '无',
        checked: true
      },
      {
        name: '有',
        value: '有',
        checked: false
      }, {
        name: '同住',
        value: '同住',
        checked: false
      },
      {
        name: '不同住',
        value: '不同住',
        checked: false
      }
    ],
    deposit: '',
    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: ['有贷款', '无贷款'],
    isCommenArr: ['一起', '没有'],
    controlTimeArr: [{
        name: '不限',
        value: '不限',
        checked: true
      },
      {
        name: '晚18：00以后',
        value: '晚18：00以后',
        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
      }
    ],
    workplaceArr: ['政府机关', '事业单位', '外企企业', '世界500强', '上市公司', '国有企业', '私营企业', '个体'],
    parentsArr: ['爹妈比我都健康,一直很恩爱', '爹妈比我都健康,离婚了', '世事无常'],
    dadworkplaceArr: ['退休', '政府机关', '事业单位', '外企企业', '世界500强', '上市公司', '国有企业', '私营企业', '个体'],
    momworkplaceArr: ['退休', '政府机关', '事业单位', '外企企业', '世界500强', '上市公司', '国有企业', '私营企业', '个体'],
    otherchildArr: [{
        name: '没有唯我独尊',
        value: '没有唯我独尊',
        checked: false
      },
      {
        name: '有姐姐',
        value: '偶有姐姐',
        checked: false
      }, {
        name: '有哥哥',
        value: '有哥哥',
        checked: false
      }, {
        name: '有弟弟',
        value: '有弟弟',
        checked: false
      }, {
        name: '有妹妹',
        value: '有妹妹',
        checked: false
      }
    ],
    figureArr: ['龟息养生从不运动', '三天打鱼偶尔运动', '动如脱兔成天运动']
  },
  heightPicker(e) {
    let height = 'form.height'
    this.setData({
      [height]: this.data.heightArr[e.detail.value]
    })
  },
  weightPicker(e) {
    let weight = 'form.weight'
    this.setData({
      [weight]: this.data.weightArr[e.detail.value]
    })
  },
  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]
    })
  },
  marriageChange: function (e) {
    console.log(e.detail.value)
    const items = this.data.marriageList
    let checked = 'marriageList[0].checked';
    let checked1 = 'marriageList[1].checked'
    this.setData({
      [checked]: false,
      [checked1]: false
    })
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == e.detail.value) {
        items[i].checked = true;
        break
      }
    }
    this.setData({
      marriageList: this.data.marriageList
    })
    let marriage = 'form.marriage'
    this.setData({
      [marriage]: e.detail.value
    })
  },
  childrenChange: function (e) {
    console.log(e.detail.value)
    const items = this.data.childrenArr;
    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({
      childrenArr: this.data.childrenArr
    })
    let children = 'form.children'
    this.setData({
      [children]: e.detail.value
    })
  },
  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]
    })
  },
  isCommenChange(e) {
    let isCommen = 'form.isCommen'
    this.setData({
      [isCommen]: this.data.isCommenArr[e.detail.value]
    })
  },
  controlTimeChange(e) {
    console.log(e.detail.value)
    const items = this.data.controlTimeArr;
    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({
      controlTimeArr: this.data.controlTimeArr
    })
    let controlTime = 'form.controlTime'
    this.setData({
      [controlTime]: 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
    })
  },
  workplacePicker: function (e) {
    let companyNature = 'form.companyNature'
    this.setData({
      [companyNature]: this.data.workplaceArr[e.detail.value]
    })
  },
  parentsPicker(e) {
    let parents = 'form.parents'
    this.setData({
      [parents]: this.data.parentsArr[e.detail.value]
    })
  },
  dadworkplacePicker: function (e) {
    let fatherWork = 'form.fatherWork'
    this.setData({
      [fatherWork]: this.data.dadworkplaceArr[e.detail.value]
    })
  },
  momworkplacePicker: function (e) {
    let motherWork = 'form.motherWork'
    this.setData({
      [motherWork]: this.data.momworkplaceArr[e.detail.value]
    })
  },
  otherchildChange: function (e) {
    console.log(e.detail.value)
    const items = this.data.otherchildArr;
    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({
      otherchildArr: this.data.otherchildArr
    })
    let composition = 'form.composition'
    this.setData({
      [composition]: e.detail.value
    })
  },

  figurePicker(e) {
    let motionState = 'form.motionState'
    this.setData({
      [motionState]: this.data.figureArr[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.navigateTo({
      url: '/pages/targetInfo/targetInfo',
    })
  },
  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      height: {
        required: true
      },
      weight:  {
        required: true
      },
      education:  {
        required: true
      },
      income:  {
        required: true
      },
      marriage:  {
        required: true
      },
      children:  {
        required: true
      },
      assetHouse:  {
        required: true
      },
      assetArea:  {
        required: true
      },
      assetHouseLoan:  {
        required: true
      },
      assetCar:  {
        required: true
      },
      assetCarValue:  {
        required: true
      },
      assetCarLoan:  {
        required: true
      },
      isCommen:  {
        required: true
      },
      controlTime:  {
        required: true
      },
      workStatus:  {
        required: true
      },
      companyNature:  {
        required: true
      },
      company:  {
        required: true
      },
      parents:  {
        required: true
      },
      // fatherWork:  {
      //   required: true
      // },
      // motherWork:  {
      //   required: true
      // },
      composition:  {
        required: true
      },
      motionState:  {
        required: true
      },
      hobby:  {
        required: true
      }
    }
    const messages = {
      // name: {
      //   required: '请填写姓名',
      //   minlength: '请输入正确的名称',
      //   maxlength: '请输入正确的名称'
      // },
      // sex: {
      //   required: '请选择性别'
      // },
      // age: {
      //   required: '请填写年龄'
      // },
      // nativePlace: {
      //   required: '请填写户籍地址'
      // },
      // nation: {
      //   required: '请选择民族'
      // },
      // birthday: {
      //   required: '请填写生日'
      // },
      // constellation: {
      //   required: '请选择星座'
      // }
      height: {
        required: '请选择身高'
      },
      weight:  {
        required: '请选择体重'
      },
      education:  {
        required: '请选择教育程度'
      },
      income:  {
        required: '请选择收入'
      },
      marriage:  {
        required: '请选择婚姻状态'
      },
      children:  {
        required: '请选择有没有孩子'
      },
      assetHouse:  {
        required: '请选择是否买房子'
      },
      assetArea:  {
        required: '请选择房子多大面积'
      },
      assetHouseLoan:  {
        required: '请选择房子贷款了没'
      },
      assetCar:  {
        required: '请选择车子情况'
      },
      assetCarValue:  {
        required: '请选择车子的价值'
      },
      assetCarLoan:  {
        required: '请选择车子贷款了没'
      },
      isCommen:  {
        required: '请选择是否与父母同住'
      },
      controlTime:  {
        required: '请选择您的空闲时间'
      },
      workStatus:  {
        required: '请选择您的工作状态'
      },
      companyNature:  {
        required: '请选择您的单位性质'
      },
      company:  {
        required: '请输入您的单位具体名称'
      },
      parents:  {
        required: '请选择您的父母情况'
      },
      // fatherWork:  {
      //   required: 
      // },
      // motherWork:  {
      //   required: 
      // },
      composition:  {
        required: '请选择有没有兄弟姐妹'
      },
      motionState:  {
        required: '请选择您是否喜欢运动'
      },
      hobby:  {
        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 () {

  }
})