const app = getApp();

Page({

  data: {

    // selectArray: [], // 性别-数据
    // index: '',  // 性别-下标、

    AgeArray: [], // 年龄-数据
    Ageindex: '',  // 年龄-下标

    heightArray: [], // 身高-数据
    heightindex: '',  // 身高-下标

    EducationArray: [], // 学历-数据
    Educationindex: '',  // 学历-下标

    MarriageArray: [], // 婚史-数据
    Marriageindex: '',  // 婚史-下标

    incomeArray: [], // 收入-数据
    incomeindex: '',  // 收入-下标

    OccupationArray: [], // 职业-数据
    Occupationindex: '',  // 职业-下标

    // contactArray: [], // 允许有意者联系我-数据
    // contactindex: '',  // 允许有意者联系我-下标

    multiArray: [],   // 地址-数据
    step: 0,  // 地址
    multiIndex: [0, 0, 0],  // 地址-下标

    nickname: '',  //姓名
    // sex: '',     //  性别  
    age: '',      //  年龄  
    height: '',      //  身高  
    province: '',     // 市
    city: '',     // 市
    area: '',     // 区
    education: '',    //  学历  
    marry: '',    //   婚史 
    income: '',     //  收入 
    occupation: '',     //  职业  
    // openid:'',  //授权


    personacodel:''


  },




  onLoad: function () {
    var that = this

    getApp().showLoading();   //提示加载中

    //  获取个人信息
    let personal_url = 'my/personaldata/personal';
    let personal_data = {
    };
    app.postRequest(personal_url, personal_data, function (res) {
      if (res.code == 201) {
    
        that.setData({
          personal: res.data,    //获取全部数据
        })
      }
    })



    //  默认个人信息
    let editinfo_url = 'my/personaldata/editinfo';
    let editinfo_data = {
    };
    app.postRequest(editinfo_url, editinfo_data, function (res) {

      //  年龄 ajax
      let Age_url = 'public/paramsgather?type=age';
      let Age_data = {
        // pid: '0'
      };

      let _age = res.data.age;
      app.postRequest(Age_url, Age_data, function (res) {      //  年龄 ajax
        if (res.code == 201) {
          for (let i = 0; i < res.data.length; i++) {    //已选中的数据处理
                if (res.data[i] == _age) {
                    that.setData({
                      Ageindex: i,
                    })
                }
          }

          let dictObject = res.data;
          // 对象转成数组
          var createArr = []
          for (let i in dictObject) {
            createArr.push(dictObject[i] + "岁");
          }
          // console.log(createArr);
          that.setData({
            AgeArray: createArr
          })

        }

      })




      //  身高 ajax
      let height_url = 'public/paramsgather?type=height';
      let height_data = {
        pid: '0'
      };
      let _height = res.data.height;
      app.postRequest(height_url, height_data, function (res) {      //  身高 ajax
        if (res.code == 201) {

          for (let i = 0; i < res.data.length; i++) {    //已选中的数据处理
            if (res.data[i] == _height) {
              that.setData({
                heightindex: i,
              })
            }
          }

          let dictObject = res.data;
          // 对象转成数组
          var createArr = []
          for (let i in dictObject) {
            createArr.push(dictObject[i] + "cm");
          }
          // console.log(createArr);

          that.setData({
            heightArray: createArr
          })

        }

      })



      //  学历 ajax
      let Education_url = 'public/paramsgather?type=xueli';
      let Education_data = {
        // pid: '0'
      };
      let _education = res.data.education;
      app.postRequest(Education_url, Education_data, function (res) {      //  学历 ajax
        if (res.code == 201) {

          for (let i = 0; i < res.data.length; i++) {    //已选中的数据处理
            if (res.data[i].id == _education) {
              that.setData({
                Educationindex: i,
              })
            }
          }

          that.setData({
            EducationArray: res.data
          })
        }
      })


      //  职业 ajax 
      let Occupation_url = 'public/paramsgather?type=zhiye';
      let Occupation_data = {
        pid: '0'
      };
      let _occupation = res.data.occupation;
      app.postRequest(Occupation_url, Occupation_data, function (res) {      //  职业 ajax
        if (res.code == 201) {

          for (let i = 0; i < res.data.length; i++) {    //已选中的数据处理
            if (res.data[i].id == _occupation) {
              that.setData({
                Occupationindex: i,
              })
            }
          }
          
          that.setData({
            OccupationArray: res.data
          })
        }
      })

      //  收入 ajax 
      let income_url = 'public/paramsgather?type=shouru';
      let income_data = {
        // pid: '0'
      };
      let _income = res.data.income;

      app.postRequest(income_url, income_data, function (res) {      //  收入 ajax
        if (res.code == 201) {

          for (let i = 0; i < res.data.length; i++) {    //已选中的数据处理
            if (res.data[i].id == _income) {

                that.setData({
                  incomeindex: i,
                })
            }
          }

          that.setData({
            incomeArray: res.data
          })
        }
      })



      //  婚史 ajax
      let Marriage_url = 'public/paramsgather?type=marriage';
      let Marriage_data = {
        pid: '0'
      };
      app.postRequest(Marriage_url, Marriage_data, function (res) {      //  婚史 ajax
        if (res.code == 201) {
          // console.log(res.data);
          that.setData({
            MarriageArray: res.data
          })

        }

      })



      if (res.code == 201) {
        // console.log(res.data);
        that.setData({

          nickname: res.data.nickname,  //姓名
          // sex: res.data.sex,     //  性别  
          age: res.data.age,      //  年龄  
          height: res.data.height,      //  身高  
          province: res.data.province,     // 市
          city: res.data.city,     // 市
          area: res.data.area,     // 区
          education: res.data.education,    //  学历  
          marry: res.data.marry,    //   婚史 
          income: res.data.income,     //  收入 
          occupation: res.data.occupation,     //  职业

          Marriageindex: res.data.marry,    //   婚史 
          personacodel: res,  //获取code 前台判断
    
          
        })
      }

      that.getProvince();   //地址  ajax

    })


    // 获取区域ID
    // var _openid = 0;
    // try {
    //   var value = wx.getStorageSync('openid');
    //   if (value) {
    //     _openid = value

    //     that.setData({
    //       openid: _openid
    //     })


    //   }
    //   console.log(_openid);
    // } catch (e) {
    //   // Do something when catch error
    // }



    //  性别 ajax
    // let p_url = 'public/paramsgather?type=sex';
    // let p_data = {
    //   // pid: '0'
    // };
    // app.postRequest(p_url, p_data, function (res) {      //  性别 ajax
    //   if (res.code == 201) {
    //     // console.log(res.data);
    //     that.setData({
    //       selectArray: res.data
    //     })

    //   }


    // })


    //  允许有意者联系我 ajax

    // let contact_url = 'public/citypid';
    // let contact_data = {
    //   pid: '0'
    // };
    // app.postRequest(contact_url, contact_data, function (res) {      //  允许有意者联系我 ajax
    //   // console.log(res);
    //   that.setData({
    //     contactArray: res.data
    //   })
    // })

  },


  GenderEvent: function (e) {   //  性别    单项选择器
    // console.log(this);
    // console.log(e);
    // console.log(this.data.selectArray[e.detail.value]);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    // this.setData({
    //   index: e.detail.value,
    //   sex: e.detail.value
    // })

    wx.showModal({
      title: '性别确定之后不可更改',
      content: '如需更改请联系管理员',
      showCancel: false,
    })
  },

  AgeEvent: function (e) {   //  年龄    单项选择器
    // console.log(this);
    // console.log(e);
    // console.log(this.data.AgeArray[e.detail.value]);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      Ageindex: e.detail.value,
      age: this.data.AgeArray[e.detail.value]
    })
  },

  heightEvent: function (e) {   //  身高    单项选择器
    // console.log(this);
    // console.log(e);
    // console.log(this.data.heightArray[e.detail.value]);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      heightindex: e.detail.value,
      height: this.data.heightArray[e.detail.value]
    })
  },

  EducationEvent: function (e) {   //  学历    单项选择器
    // console.log(this);
    // console.log(e);
    // console.log(this.data.EducationArray[e.detail.value].id);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      Educationindex: e.detail.value,
      education: this.data.EducationArray[e.detail.value].id
    })
  },

  MarriageEvent: function (e) {   //  婚史    单项选择器
    // console.log(this);
    // console.log(e);
    // console.log(this.data.MarriageArray[e.detail.value]);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      Marriageindex: e.detail.value,
      marry: e.detail.value
    })
  },

  incomeEvent: function (e) {   //  收入    单项选择器
    // console.log(this);
    // console.log(e);
    // console.log(this.data.incomeArray[e.detail.value].id);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      incomeindex: e.detail.value,
      income: this.data.incomeArray[e.detail.value].id
    })
  },

  OccupationEvent: function (e) {   //  职业    单项选择器
    // console.log(this);
    // console.log(e);
    // console.log(this.data.OccupationArray[e.detail.value].id);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      Occupationindex: e.detail.value,
      occupation: this.data.OccupationArray[e.detail.value].id
    })
  },

  // contactEvent: function (e) {   //  允许有意者联系我    单项选择器
  //   // console.log(this.data.selectArray[e.detail.value].area_name)
  //   // console.log('picker发送选择改变,携带值为', e.detail.value)
  //   console.log(this);
  //   console.log(e);
  //   console.log(this.data.contactArray[e.detail.value]);
  //   console.log('picker发送选择改变,携带值为', e.detail.value)
  //   this.setData({
  //     contactindex: e.detail.value
  //   })
  // },



  getProvince() {    //  多项选择器

    let _this = this;
    let d_url = 'public/citypid';
    app.postRequest(d_url, { pid: '0' }, function (res) {

      if (res.code == '201') {
        var provinceList = res.data;
        var provinceArr = [];
        provinceArr = _this.mapArray(res.data);
        _this.setData({
          multiArray: [provinceArr, [], []],
          provinceList,
          provinceArr,
        })

        // console.log(provinceList);
        // console.log(provinceArr);

        var defaultCode = provinceList[0]['area_id']
        // console.log(provinceList[0]['area_id']);
        if (defaultCode) {
          _this.setData({
            currnetProvinceKey: defaultCode
          })
          _this.getCity(defaultCode)
        }
      }


    })


  },


  getCity(codes) {
    //  多项选择
    let _this = this;
    let d_url = 'public/citypid';
    app.postRequest(d_url, { pid: codes }, function (res) {
      var cityList = res.data;
      var cityArr = [];
      if (res.code == '201') {
        cityArr = _this.mapArray(res.data);
        _this.setData({
          multiArray: [_this.data.provinceArr, cityArr, []],
          cityArr,
          cityList

        })

        var defaultCode = cityList[0]['area_id']

        if (defaultCode) {
          _this.setData({
            currnetProvinceKey: defaultCode
          })
          _this.getArea(defaultCode)
        }
      }

    })
  },
  getArea(codes) {
    //  多项选择
    let _this = this;
    let d_url = 'public/citypid';
    app.postRequest(d_url, { pid: codes }, function (res) {
      var areaList = res.data;
      var areaArr = [];
      if (res.code == '201') {
        areaArr = _this.mapArray(res.data);
      }

      // console.log(areaList);
      // console.log(areaArr);

      _this.setData({
        multiArray: [_this.data.provinceArr, _this.data.cityArr, areaArr],
        areaList,
        areaArr
      })
    })
  },
  mapArray: function (data) {
    let cityArray = []
    if (data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].area_name) {
          cityArray[i] = data[i].area_name;

        }
      }
    }



    return cityArray;
  },
  columnchange(e) {  // 滚动选择器 触发的事件
    var column = e.detail.column  // 当前改变的列
    // console.log(column);
    // console.log(this.data.multiIndex);
    // console.log(JSON.parse(JSON.stringify(this.data.multiIndex)));
    var data = {
      multiIndex: this.data.multiIndex,
      multiArray: this.data.multiArray
    }
    data.multiIndex[column] = e.detail.value;  // 第几列改变了就是对应multiIndex的第几个,更新它
    switch (column) { // 处理不同的逻辑
      case 0:   // 第一列更改 就是省级的更改
        var currentProvinceKey = this.data.provinceList[e.detail.value].area_id
        if (currentProvinceKey != this.data.currnetProvinceKey) {  // 判断当前的key是不是真正的更新了
          this.getCity(currentProvinceKey)  // 获取当前key下面的市级数据
        }
        data.multiIndex[1] = 0  // 将市默认选择第一个
        break;
      case 1:  // 市发生变化
        var currentCitykey = this.data.cityList[e.detail.value].area_id
        if (currentCitykey != this.data.currnetCityKey) {  // 同样判断
          this.getArea(currentCitykey)   // 获取门店
        }
        data.multiIndex[2] = 0
        break;
    }
    this.setData(data)  // 更新数据
  },
  pickchange(e) {
    // console.log(this.data.provinceList[e.detail.value[0]].area_id);
    // console.log(this.data.cityList[e.detail.value[1]].area_id);
    // console.log(this.data.areaList[e.detail.value[2]].area_id);
    this.setData({
      step: 1,  // 更新,用来选择用户选中的门店
      multiIndex: e.detail.value,  // 更新下标字段
      province: this.data.provinceList[e.detail.value[0]].area_id,  // 市
      city: this.data.cityList[e.detail.value[1]].area_id,  // 市
      area: this.data.areaList[e.detail.value[2]].area_id   // 区
    })
  },







  // 名字
  getPhone: function (e) {
    var nickname = e.detail.value;
    // console.log(nickname);
    this.setData({
      nickname: nickname
    });
  },

  //下一步事件
  register: function (e) {

    console.log(this.data.nickname);       //  名字  
    // console.log(this.data.sex);                 //  性别  
    console.log(this.data.age);              //  年龄 
    console.log(this.data.height);           //  身高 
    console.log(this.data.education);      //  学历  
    console.log(this.data.marry);          //   婚史 
    console.log(this.data.income);        //  收入 
    console.log(this.data.province);       //  省 
    console.log(this.data.city);              //  市 
    console.log(this.data.area);              //  区 



    var nickname = this.data.nickname  //姓名
    // var sex = this.data.sex                     //  性别  
    var age = this.data.age                     //  年龄  
    var height = this.data.height             //  身高  
    var province = this.data.province      // 市
    var city = this.data.city                       // 市
    var area = this.data.area                     // 区
    var education = this.data.education  //  学历  
    var marry = this.data.marry                 //   婚史 
    var income = this.data.income             //  收入 
    var occupation = this.data.occupation  //  收入 occupation

    // wx.navigateTo({
    //   url: '/pages/register/fillTel/fillTel?nickname=' + nickname +  '&sex=' + sex + '&age=' + age + '&height=' + height + '&education=' + education + '&marry=' + marry + '&income=' + income + '&city=' + city + '&area=' + area + '&occupation=' + occupation + '&province=' + province + ''
    // })

  

    //  修改个人信息
    let editinfoform_url = 'my/personaldata/editinfoform';
    let editinfoform_data = {

      nickname: this.data.nickname,  //姓名
      age: this.data.age,      //  年龄  
      height: this.data.height,      //  身高  
      province: this.data.province,     // 市
      city: this.data.city,     // 市
      area: this.data.area,     // 区
      education: this.data.education,    //  学历  
      marry: this.data.marry,    //   婚史 
      income: this.data.income,     //  收入 
      occupation: this.data.occupation,     //  职业

      };
    app.postRequest(editinfoform_url, editinfoform_data, function (res) {

          if (res.code == 201) {
                wx.showToast({
                  title: '修改成功',
                  icon: 'success',
                  duration: 2000
                })

          }

      })

  },

})