import WxValidate from '../../utils/WxValidate.js';
const app = require('../../utils/util.js');
const request = require('../../utils/request.js');
const COS = require('../../utils/cos.js');
const tool = require('../../utils/tool.js');
Page({
  data: {
    form: {
      phone: '',
      height: '',
      weight: '',
      education: '',
      income: '',
      marriage: '',
      children: '',
      assetHouse: '',
      assetHouseArea: '',
      assetHouseLoan: '',
      assetCar: '',
      assetCarValue: '',
      assetCarLoan: '',
      isCommon: '',
      controlTime: '',
      workStatus: '',
      companyNature: '',
      company: '',
      parents: '',
      fatherWork: '',
      matherWork: '',
      parentAssets: '',
      composition: '',
      motionState: '',
      hobby: ''
    },
    heightArr: [],
    weightArr: [],
    educationArr: [],
    incomeArr: [],
    marriageList: [],
    childrenArr: [],
    assetHouseArr: [],
    assetAreaArr: [],
    assetHouseLoanArr: [],
    assetCarArr: [],
    assetCarValueArr: [],
    assetCarLoanArr: [],
    isCommonArr: ['没有', '一起'],
    controlTimeArr: [],
    workStatusArr: [],
    companyNatureArr: [],
    parentsArr: [],
    fatherWorkArr: [],
    matherWorkArr: [],
    parentAssetsArr: [],
    compositionArr: [],
    motionStateArr: [],
    hobbyArr: []
  },
  phoneChange(e) {
    let phone = 'form.phone'
    this.setData({
      [phone]: e.detail.value
    })
  },
  heightPicker(e) {
    let height = 'form.height'
    this.setData({
      heightLabel: this.data.heightArr[e.detail.value],
      [height]: this.data.heightArrValue[e.detail.value]
    })
  },
  weightPicker(e) {
    let weight = 'form.weight'
    this.setData({
      weightLabel: this.data.weightArr[e.detail.value],
      [weight]: this.data.weightArrValue[e.detail.value]
    })
  },
  educationPicker(e) {
    let education = 'form.education'
    this.setData({
      educationLabel: this.data.educationArr[e.detail.value],
      [education]: this.data.educationArrValue[e.detail.value]
    })
  },
  incomePicker(e) {
    let income = 'form.income'
    this.setData({
      incomeLabel: this.data.incomeArr[e.detail.value],
      [income]: this.data.incomeArrValue[e.detail.value]
    })
  },
  marriageChange(e) {
    let marriage = 'form.marriage'
    this.setData({
      marriageLabel: this.data.marriageList[e.detail.value],
      [marriage]: this.data.marriageListValue[e.detail.value]
    })
  },
  childrenChange(e) {
    let children = 'form.children'
    this.setData({
      childrenLabel: this.data.childrenArr[e.detail.value],
      [children]: this.data.childrenArrValue[e.detail.value]
    })
  },
  assetHousePickerChange(e) {
    console.log(e)
    let assetHouse = 'form.assetHouse'
    this.setData({
      assetHouseLabel: this.data.assetHouseArr[e.detail.value],
      [assetHouse]: this.data.assetHouseArrValue[e.detail.value]
    })
  },
  assetAreaPickerChange(e) {
    console.log(e)
    let assetHouseArea = 'form.assetHouseArea'
    this.setData({
      assetAreaLabel: this.data.assetAreaArr[e.detail.value],
      [assetHouseArea]: this.data.assetAreaArrValue[e.detail.value]
    })
  },
  assetHouseLoanPickerChange(e) {
    console.log(this.data.assetHouseLoanArr)
    console.log(this.data.assetHouseLoanArrValue)
    let assetHouseLoan = 'form.assetHouseLoan'
    this.setData({
      assetHouseLoanLabel: this.data.assetHouseLoanArr[e.detail.value],
      [assetHouseLoan]: this.data.assetHouseLoanArrValue[e.detail.value]
    })
  },
  assetCarPickerChange(e) {
    console.log(e)
    let assetCar = 'form.assetCar'
    this.setData({
      assetCarLabel: this.data.assetCarArr[e.detail.value],
      [assetCar]: this.data.assetCarArrValue[e.detail.value]
    })
  },
  assetCarValuePickerChange(e) {
    console.log(e)
    let assetCarValue = 'form.assetCarValue'
    this.setData({
      assetCarValueLabel: this.data.assetCarValueArr[e.detail.value],
      [assetCarValue]: this.data.assetCarValueArrValue[e.detail.value]
    })
  },
  assetCarLoanPickerChange(e) {
    console.log(e)
    let assetCarLoan = 'form.assetCarLoan'
    this.setData({
      assetCarLoanLabel: this.data.assetCarLoanArr[e.detail.value],
      [assetCarLoan]: this.data.assetCarLoanArrValue[e.detail.value]
    })
  },
  isCommonChange(e) {
    let isCommon = 'form.isCommon'
    this.setData({
      [isCommon]: e.detail.value,
      isCommonLabel: this.data.isCommonArr[e.detail.value],
    })
  },
  controlTimeChange(e) {
    const items = this.data.controlTimeArr;
    const values = e.detail.value;
    let controlTime = 'form.controlTime'
    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,
      [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
        }
      }
    }
    let workStatus = 'form.workStatus'
    this.setData({
      [workStatus]: e.detail.value,
      workStatusArr: this.data.workStatusArr
    })
  },
  companyNaturePicker(e) {
    let companyNature = 'form.companyNature'
    this.setData({
      companyNatureLabel: this.data.companyNatureArr[e.detail.value],
      [companyNature]: this.data.companyNatureArrValue[e.detail.value]
    })
  },
  companyChange(e) {
    let company = 'form.company'
    this.setData({
      [company]: e.detail.value
    })
  },
  parentsPicker(e) {
    let parents = 'form.parents'
    this.setData({
      parentsLabel: this.data.parentsArr[e.detail.value],
      [parents]: this.data.parentsArrValue[e.detail.value]
    })
  },
  fatherworkPicker(e) {
    let fatherWork = 'form.fatherWork'
    this.setData({
      fatherWorkLabel: this.data.fatherWorkArr[e.detail.value],
      [fatherWork]: this.data.fatherWorkArrValue[e.detail.value]
    })
  },
  matherworkPicker(e) {
    let matherWork = 'form.matherWork'
    this.setData({
      matherWorkLabel: this.data.matherWorkArr[e.detail.value],
      [matherWork]: this.data.matherWorkArrValue[e.detail.value]
    })
  },
  parentAssetsPicker(e) {
    let parentAssets = 'form.parentAssets'
    this.setData({
      parentAssetsLabel: this.data.parentAssetsArr[e.detail.value],
      [parentAssets]: this.data.parentAssetsArrValue[e.detail.value]
    })
  },
  compositionChange(e) {
    console.log(e.detail.value)
    const items = this.data.compositionArr;
    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
        }
      }
    }
    let composition = 'form.composition'
    this.setData({
      compositionArr: this.data.compositionArr,
      [composition]: e.detail.value
    })
  },
  figurePicker(e) {
    let motionState = 'form.motionState'
    this.setData({
      motionStateLabel: this.data.motionStateArr[e.detail.value],
      [motionState]:this.data.motionStateArrValue[e.detail.value]
     
    })
  },
  hobbyChange(e) {
    console.log(e.detail.value)
    const items = this.data.hobbyArr;
    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
        }
      }
    }
    console.log(this.data.hobbyArrLabel)
    let indexArr = [];
    for (let i = 0; i < e.detail.value.length; i++) {
      indexArr.push(this.data.hobbyArrLabel.indexOf(e.detail.value[i]))
    }
    let value = [];
    for (let i = 0; i < indexArr.length; i++) {
      value.push(this.data.hobbyArrValue[indexArr[i]])
    }
    let str = '';
    for (let i = 0; i < value.length; i++) {
      str += value[i] + ','
    }
    let hobby = 'form.hobby'
    this.setData({
      hobbyArr: this.data.hobbyArr,
      [hobby]: str.slice(0, str.length - 1)
      // [hobby]: e.detail.value
    })
  },
  // 获取手机号
  getPhoneNumber(e) {
    console.log(e)
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: app.globalData.publicUrl + '/wx/user/wxbfa171fdd4000e03/phone',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          sessionKey: this.data.sessionkey,
          signature: this.data.signature,
          rawData: this.data.rawData,
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        },
        method: "POST",
        success: (res) => {
          console.log(res, '我是解析的手号');
          let phone = 'form.phone'
          if (res.data.phoneNumber) {
            this.setData({
              [phone]: res.data.phoneNumber
            })
          } else {
            this.setData({
              [phone]: 0
            })
          }
        }
      })
    }
  },
  getInfos() {
    wx.showLoading();
    wx.request({
      url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
      method: "get",
      success: (res) => {
        console.log(res, '我是获取的信息')
        let phone = 'form.phone'
        let height = 'form.height'
        let weight = 'form.weight'
        let education = 'form.education'
        let income = 'form.income'
        let marriage = 'form.marriage'
        let children = 'form.children'
        let assetHouse = 'form.assetHouse'
        let assetHouseArea = 'form.assetHouseArea'
        let assetHouseLoan = 'form.assetHouseLoan'
        let assetCar = 'form.assetCar'
        let assetCarValue = 'form.assetCarValue'
        let assetCarLoan = 'form.assetCarLoan'
        let isCommon = 'form.isCommon'
        let controlTime = 'form.controlTime'
        let workStatus = 'form.workStatus'
        let companyNature = 'form.companyNature'
        let company = 'form.company'
        let parents = 'form.parents'
        let fatherWork = 'form.fatherWork'
        let matherWork = 'form.matherWork'
        let parentAssets = 'form.parentAssets'
        let composition = 'form.composition'
        let motionState = 'form.motionState'
        let hobby = 'form.hobby'
        tool.formDetails('he_body_height').then(result => {
          this.setData({
            heightArr: result[0],
            heightArrValue: result[1]
          })
          if (res.data.info.height) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.height) {
                this.setData({
                  heightLabel: result[0][i],
                  [height]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_body_weight').then(result => {
          this.setData({
            weightArr: result[0],
            weightArrValue: result[1]
          })
          if (res.data.info.weight) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.weight) {
                this.setData({
                  weightLabel: result[0][i],
                  [weight]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_education').then(result => {
          this.setData({
            educationArr: result[0],
            educationArrValue: result[1]
          })
          if (res.data.info.education) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.education) {
                this.setData({
                  educationLabel: result[0][i],
                  [education]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_income').then(result => {
          this.setData({
            incomeArr: result[0],
            incomeArrValue: result[1]
          })
          if (res.data.info.income) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.income) {
                this.setData({
                  incomeLabel: result[0][i],
                  [income]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_marriage').then(result => {
          this.setData({
            marriageList: result[0],
            marriageListValue: result[1]
          })
          if (res.data.info.marriage) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.marriage) {
                this.setData({
                  marriageLabel: result[0][i],
                  [marriage]: result[1][i]
                })
              }
            }
          }
        })
        tool.formDetails('he_children').then(result => {
          this.setData({
            childrenArr: result[0],
            childrenArrValue: result[1]
          })
          if (res.data.info.children) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.children) {
                this.setData({
                  childrenLabel: result[0][i],
                  [children]: result[1][i]
                })
              }
            }
          }
        })
        tool.formDetails('he_asset_house').then(result => {
          this.setData({
            assetHouseArr: result[0],
            assetHouseArrValue: result[1]
          })
          if (res.data.info.assetHouse) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.assetHouse) {
                this.setData({
                  assetHouseLabel: result[0][i],
                  [assetHouse]: result[1][i],

                })
              }
            }
          }
        })
        tool.formDetails('he_asset_house_area').then(result => {
          this.setData({
            assetAreaArr: result[0],
            assetAreaArrValue: result[1]
          })
          if (res.data.info.assetHouseArea) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.assetHouseArea) {
                this.setData({
                  assetAreaLabel: result[0][i],
                  [assetHouseArea]: result[1][i],

                })
              }
            }
          }
        })
        tool.formDetails('he_asset_house_loan').then(result => {
          this.setData({
            assetHouseLoanArr: result[0],
            assetHouseLoanArrValue: result[1]
          })
          if (res.data.info.assetHouseLoan) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.assetHouseLoan) {
                this.setData({
                  assetHouseLoanLabel: result[0][i],
                  [assetHouseLoan]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_asset_car').then(result => {
          this.setData({
            assetCarArr: result[0],
            assetCarArrValue: result[1]
          })
          if (res.data.info.assetCar) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.assetCar) {
                this.setData({
                  assetCarLabel: result[0][i],
                  [assetCar]: result[1][i],

                })
              }
            }
          }
        })
        tool.formDetails('he_asset_car_value').then(result => {
          this.setData({
            assetCarValueArr: result[0],
            assetCarValueArrValue: result[1]
          })
          if (res.data.info.assetCarValue) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.assetCarValue) {
                this.setData({
                  assetCarValueLabel: result[0][i],
                  [assetCarValue]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_asset_car_loan').then(result => {
          this.setData({
            assetCarLoanArr: result[0],
            assetCarLoanArrValue: result[1]
          })
          if (res.data.info.assetCarLoan) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.assetCarLoan) {
                this.setData({
                  assetCarLoanLabel: result[0][i],
                  [assetCarLoan]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_control_time').then(result => {
          let controlTimeArr = result[0].map((item, index) => {
            return Object.assign({}, {
              'name': item,
              'value': item,
              'checked': false
            })
          })
          if (res.data.info.controlTime) {
            let resControlTime = res.data.info.controlTime.split(",");
            let resControlTimeIndexArr = [];
            for (let i = 0; i < resControlTime.length; i++) {
              for (let j = 0; j < result[1].length; j++) {
                if (resControlTime[i] == result[1][j]) {
                  resControlTimeIndexArr.push(i)
                }
              }
            }
            let resControlTimeLabel = [];
            for (let i = 0; i < resControlTimeIndexArr.length; i++) {
              resControlTimeLabel.push(result[0][resControlTimeIndexArr[i]])
            }
            for (let i = 0; i < resControlTimeLabel.length; i++) {
              if (resControlTimeLabel[i] == controlTimeArr[i].value) {
                controlTimeArr[i].checked = true;
              }
            }
            this.setData({
              [controlTime]: resControlTimeLabel
            })
          }
          this.setData({
            controlTimeArr: controlTimeArr,
            controlTimeArrLabel: result[0],
            controlTimeArrValue: result[1],
          })
        })
        tool.formDetails('he_work_status').then(result => {
          let workStatusArr = result[0].map((item, index) => {
            return Object.assign({}, {
              'name': item,
              'value': item,
              'checked': false
            })
          })
          if (res.data.info.workStatus) {
            let resWorkStatus = res.data.info.workStatus.split(",");
            let resWorkStatusIndexArr = [];
            for (let i = 0; i < resWorkStatus.length; i++) {
              for (let j = 0; j < result[1].length; j++) {
                if (resWorkStatus[i] == result[1][j]) {
                  resWorkStatusIndexArr.push(i)
                }
              }
            }
            let resWorkStatusLabel = [];
            for (let i = 0; i < resWorkStatusIndexArr.length; i++) {
              resWorkStatusLabel.push(result[0][resWorkStatusIndexArr[i]])
            }
            for (let i = 0; i < resWorkStatusLabel.length; i++) {
              if (resWorkStatusLabel[i] == workStatusArr[i].value) {
                workStatusArr[i].checked = true;
              }
            }
            this.setData({
              [workStatus]: resWorkStatusLabel
            })
          }
          this.setData({
            workStatusArr: workStatusArr,
            workStatusArrLabel: result[0],
            workStatusArrValue: result[1]
          })
        })
        tool.formDetails('he_company_nature').then(result => {
          if (res.data.info.companyNature) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.companyNature) {
                this.setData({
                  companyNatureLabel: result[0][i],
                  [companyNature]: result[1][i]
                })
              }
            }
          }
          if (res.data.info.fatherWork) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.fatherWork) {
                this.setData({
                  fatherWorkLabel: result[0][i],
                  [fatherWork]: result[1][i]
                })
              }
            }
          }
          if (res.data.info.matherWork) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.matherWork) {
                this.setData({
                  matherWorkLabel: result[0][i],
                  [matherWork]: result[1][i]
                })
              }
            }
          }
          this.setData({
            companyNatureArr: result[0],
            companyNatureArrValue: result[1],
            fatherWorkArr: result[0],
            fatherWorkArrValue: result[1],
            matherWorkArr: result[0],
            matherWorkArrValue: result[1]
          })
        })
        if (res.data.info) {
          this.setData({
            [phone]: res.data.info.tel,
            [company]: res.data.info.company,
            [hobby]: res.data.info.hobby,
            [isCommon]: res.data.info.isCommon,
            isCommonLabel: this.data.isCommonArr[res.data.info.isCommon],
          })
        }
        tool.formDetails('he_parents').then(result => {
          this.setData({
            parentsArr: result[0],
            parentsArrValue: result[1]
          })
          if (res.data.info.parents) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.parents) {
                this.setData({
                  parentsLabel: result[0][i],
                  [parents]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_parent_assets').then(result => {
          this.setData({
            parentAssetsArr: result[0],
            parentAssetsArrValue: result[1]
          })
          if (res.data.info.parentAssets) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.parentAssets) {
                this.setData({
                  parentAssetsLabel: result[0][i],
                  [parentAssets]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_composition').then(result => {
          let compositionArr = result[0].map((item, index) => {
            return Object.assign({}, {
              'name': item,
              'value': item,
              'checked': false
            })
          })
          if (res.data.info.composition) {
            let resComposition = res.data.info.composition.split(",");
            let resCompositionIndexArr = [];
            for (let i = 0; i < resComposition.length; i++) {
              for (let j = 0; j < result[1].length; j++) {
                if (resComposition[i] == result[1][j]) {
                  resCompositionIndexArr.push(i)
                }
              }
            }
            let resCompositionLabel = [];
            for (let i = 0; i < resCompositionIndexArr.length; i++) {
              resCompositionLabel.push(result[0][resCompositionIndexArr[i]])
            }
            for (let i = 0; i < resCompositionLabel.length; i++) {
              if (resCompositionLabel[i] == compositionArr[i].value) {
                compositionArr[i].checked = true;
              }
            }
            this.setData({
              [composition]: resCompositionLabel
            })
          }
          this.setData({
            compositionArr: compositionArr,
            compositionArrLabel: result[0],
            compositionArrValue: result[1],

          })
        })
        tool.formDetails('he_sports').then(result => {
          console.log(result)
          this.setData({
            motionStateArr: result[0],
            motionStateArrValue: result[1]
          })
          if (res.data.info.motionState) {
            for (let i = 0; i < result[0].length; i++) {
              if (result[1][i] == res.data.info.motionState) {
                console.log(i)
                this.setData({
                  motionStateLabel: result[0][i],
                  [motionState]: result[1][i],
                })
              }
            }
          }
        })
        tool.formDetails('he_hobby').then(result => {
          let hobbyArr = result[0].map((item, index) => {
            return Object.assign({}, {
              'name': item,
              'value': item,
              'checked': false
            })
          })
          if (res.data.info.hobby) {
            let reshobby = res.data.info.hobby.split(",");
            let reshobbyIndexArr = [];
            for (let i = 0; i < reshobby.length; i++) {
              for (let j = 0; j < result[1].length; j++) {
                if (reshobby[i] == result[1][j]) {
                  reshobbyIndexArr.push(i)
                }
              }
            }
            let reshobbyLabel = [];
            for (let i = 0; i < reshobbyIndexArr.length; i++) {
              reshobbyLabel.push(result[0][reshobbyIndexArr[i]])
            }
            for (let i = 0; i < reshobbyLabel.length; i++) {
              if (reshobbyLabel[i] == hobbyArr[i].value) {
                hobbyArr[i].checked = true;
              }
            }
            this.setData({
              [hobby]: reshobbyLabel
            })
          }
          this.setData({
            hobbyArr: hobbyArr,
            hobbyArrLabel: result[0],
            hobbyArrValue: result[1]
          })
        })
        // tool.formDetails('he_hobby').then(result=>{
        //   console.log(result)
        //   let hobbyArr = result[0].map((item, index) => {
        //     return Object.assign({}, {
        //       'name': item,
        //       'value': item,
        //       'checked': false
        //     })
        //   })
        //   if (res.data.info.hobby) {
        //     let resHobby = res.data.info.hobby.split(",");
        //     let resHobbyIndexArr = [];
        //     for (let i = 0; i < resHobby.length; i++) {
        //       for (let j = 0; j < result[1].length; j++) {
        //         if (resHobby[i] == result[1][j]) {
        //           resHobbyIndexArr.push(i)
        //         }
        //       }
        //     }
        //     let resHobbyLabel = [];
        //     for (let i = 0; i < resHobbyIndexArr.length; i++) {
        //       resHobbyLabel.push(result[0][resHobbyIndexArr[i]])
        //     }
        //     for (let i = 0; i < resHobbyLabel.length; i++) {
        //       if (resHobbyLabel[i] == hobbyArr[i].value) {
        //         hobbyArr[i].checked = true;
        //       }
        //     }
        //     this.setData({
        //       [hobby]: resHobbyLabel
        //     })
        //   }
        //   this.setData({
        //     hobbyArr: hobbyArr,
        //     HobbyLabel: result[0],
        //     HobbyValue: result[1]
        //   })
        //   console.log(result[0])
        //   console.log(this.data.HobbyLabel)
        // })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  // 提交
  formSubmit(e) {
    console.log(e)
    console.log(this.data.HobbyLabel)
    const params = e.detail.value
    let controlTime = 'form.controlTime';
    let workStatus = 'form.workStatus';
    let composition = 'form.composition'
    let indexArr = [];
    for (let i = 0; i < params.controlTime.length; i++) {
      indexArr.push(this.data.controlTimeArrLabel.indexOf(params.controlTime[i]))
    }
    let value = [];
    for (let i = 0; i < indexArr.length; i++) {
      value.push(this.data.controlTimeArrValue[indexArr[i]])
    }
    let str = '';
    for (let i = 0; i < value.length; i++) {
      str += value[i] + ','
    }

    let indexArr2 = [];
    for (let i = 0; i < params.workStatus.length; i++) {
      indexArr2.push(this.data.workStatusArrLabel.indexOf(params.workStatus[i]))
    }
    let value2 = [];
    for (let i = 0; i < indexArr2.length; i++) {
      value2.push(this.data.workStatusArrValue[indexArr2[i]])
    }
    let str2 = '';
    for (let i = 0; i < value2.length; i++) {
      str2 += value2[i] + ','
    }


    console.log(this.data.compositionArrLabel)
    let indexArr3 = [];
    for (let i = 0; i < params.composition.length; i++) {
      indexArr3.push(this.data.compositionArrLabel.indexOf(params.composition[i]))
    }
    let value3 = [];
    for (let i = 0; i < indexArr3.length; i++) {
      value3.push(this.data.compositionArrValue[indexArr3[i]])
    }
    let str3 = '';
    for (let i = 0; i < value3.length; i++) {
      str3 += value3[i] + ','
    }
    this.setData({
      [controlTime]: str.slice(0, str.length - 1),
      [workStatus]: str2.slice(0, str2.length - 1),
      [composition]: str3.slice(0, str3.length - 1),
      // [hobby]: str4.slice(0, str4.length - 1)
    })
    console.log(this.data.form)
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
    wx.request({
      url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: this.data.form,
      success: (res) => {
        console.log(res)
        if (res.data.code == 0) {
          wx.showModal({
            title: '上传成功',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/InfoType/InfoType',
                })
              }
            }
          });
        }
      }
    })
  },
  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      phone: {
        required: true
      },
      height: {
        required: true
      },
      weight: {
        required: true
      },
      education: {
        required: true
      },
      income: {
        required: true
      },
      marriage: {
        required: true
      },
      children: {
        required: true
      },
      assetHouse: {
        required: true
      },
      // assetHouseArea: {
      //   required: true
      // },
      // assetHouseLoan: {
      //   required: true
      // },
      assetCar: {
        required: true
      },
      // assetCarValue: {
      //   required: true
      // },
      // assetCarLoan: {
      //   required: true
      // },
      isCommon: {
        required: true
      },
      controlTime: {
        required: true
      },
      workStatus: {
        required: true
      },
      companyNature: {
        required: true
      },
      company: {
        required: true
      },
      parents: {
        required: true
      },
      composition: {
        required: true
      },
      motionState: {
        required: true
      },
      hobby: {
        required: true
      }
      // photoUrl: {
      //   required: true
      // }
    }
    const messages = {
      phone: {
        required: '请输入联系方式呦'
      },
      height: {
        required: '请选择身高呦'
      },
      weight: {
        required: '请选择体重呦'
      },
      education: {
        required: '请选择教育程度呦'
      },
      income: {
        required: '请选择收入呦'
      },
      marriage: {
        required: '请选择婚姻状态呦'
      },
      children: {
        required: '请选择您有没有孩子呦'
      },
      assetHouse: {
        required: '请选择是否买房子呦'
      },
      // assetHouseArea: {
      //   required: '请选择房子多大面积'
      // },
      // assetHouseLoan: {
      //   required: '请选择房子贷款了没'
      // },
      assetCar: {
        required: '请选择车子情况呦'
      },
      // assetCarValue: {
      //   required: '请选择车子的价值'
      // },
      // assetCarLoan: {
      //   required: '请选择车子贷款了没'
      // },
      isCommon: {
        required: '请选择是否与父母同住呦'
      },
      controlTime: {
        required: '请选择您的空闲时间呦'
      },
      workStatus: {
        required: '请选择您的工作状态呦'
      },
      companyNature: {
        required: '请选择您的单位性质呦'
      },
      company: {
        required: '请输入您的单位具体名称呦'
      },
      parents: {
        required: '请选择您的父母情况呦'
      },
      composition: {
        required: '请选择有没有兄弟姐妹呦'
      },
      motionState: {
        required: '请选择您是否喜欢运动呦'
      },
      hobby: {
        required: '请选择您的爱好呦'
      }
      // photoUrl: {
      //   required: '请上传您的照片'
      // }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  async onLoad(options) {
    this.initValidate();
    tool.openidStatus().then(result => {
      this.setData({
        openid: result[0],
        sessionkey: result[1]
      })
      // this.getForm();
      this.getInfos();
      wx.getSetting({
        success: res => {
          console.log(res)
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                this.setData({
                  rawData: res.rawData,
                  signature: res.signature
                })
                if (this.userInfoReadyCallback) {
                  this.userInfoReadyCallback(res)
                }
              }
            })
          } else {
            // wx.showModal({
            //   title: '您还未登录',
            //   showCancel: false,
            //   success(res) {
            //     if (res.confirm) {
            //       wx.redirectTo({
            //         url: '/pages/personal/personal',
            //       })
            //     }
            //   }
            // });
          }
        }
      })
      // wx.getUserInfo({
      //   success: (res) => {
      //     console.log(res)
      //     this.setData({
      //       rawData: res.rawData,
      //       signature: res.signature
      //     })
      //   }
      // })
    })
  },
  // 上传头像
  uploadPhoto() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['', 'camera'],
      success: (res) => {
        console.log(res)
        let path = res.tempFilePaths[0];
        console.log(path)
        this.setData({
          carWin_img: path
        })
        this.toCos();
      }
    })
  },
  // 传cos
  toCos() {
    var Bucket = 'yuehe-1257653330';
    var Region = 'ap-beijing';
    var ForcePathStyle = false;
    var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
    if (ForcePathStyle) {
      prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
    }
    var stsCache;
    var getCredentials = (callback) => {
      if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
        callback(data.credentials);
        return;
      }
      wx.request({
        method: 'POST',
        url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/getCosSts',
        // dataType: 'json',
        success: function (result) {
          console.log(result, '222222222222')
          var credentials = result.data.credential.credentials;
          if (credentials) {
            stsCache = result.data.credential
          } else {
            wx.hideLoading()
            wx.showModal({
              title: '临时密钥获取失败',
              content: JSON.stringify(data),
              showCancel: false
            });
          }
          callback(stsCache && stsCache.credentials);
        },
        error: function (err) {
          wx.hideLoading()
          wx.showModal({
            title: '临时密钥获取失败',
            content: JSON.stringify(err),
            showCancel: false
          });
        }
      });
    };
    // 计算签名
    var getAuthorization = function (options, callback) {
      getCredentials(function (credentials) {
        callback({
          XCosSecurityToken: credentials.sessionToken,
          Authorization: COS({
            SecretId: credentials.tmpSecretId,
            SecretKey: credentials.tmpSecretKey,
            Method: options.Method,
            Pathname: options.Pathname,
          })
        });
      });
    };
    // 上传文件
    var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名
    var signPathname = '/';
    if (ForcePathStyle) {
      signPathname = '/' + Bucket + '/';
    }
    getAuthorization({
      Method: 'POST',
      Pathname: signPathname
    }, (AuthData) => {
      var requestTask = wx.uploadFile({
        url: prefix,
        name: 'file',
        filePath: this.data.carWin_img,
        formData: {
          'key': "face/" + Key,
          'success_action_status': 200,
          'Signature': AuthData.Authorization,
          'x-cos-security-token': AuthData.XCosSecurityToken,
          'Content-Type': '',
        },
        success: (res) => {
          console.log(res.header.Location)
          let photoUrl = 'form.photoUrl'
          this.setData({
            [photoUrl]: res.header.Location
          })
          // this.tj(params, res.header.Location)
        },
        fail: function () {
          wx.hideLoading()
          wx.showModal({
            title: '上传失败',
            showCancel: false
          });
        }
      });
      requestTask.onProgressUpdate(function (res) {
        console.log('进度:', res.progress);
      });
    });
  }
})