const app = getApp();

Page({

  data: {
    // 上传的案例图片集合
    uploadImages: [],
    // 设置上传案例图片的最大数目
    maxImages: 1,
    // 案例图片数目是否达到了最大数目
    isMaxImagesNum: false,
    uploadimg: true,

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

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

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

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

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

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

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

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

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

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


  },



  onLoad: function () {
    var that = this

    // 获取区域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
          })

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

    })


    //  年龄 ajax
    let Age_url = 'public/paramsgather?type=age';
    let Age_data = {
      // pid: '0'
    };
    app.postRequest(Age_url, Age_data, function (res) {      //  年龄 ajax
      if (res.code == 201) {
        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'
    };
    app.postRequest(height_url, height_data, function (res) {      //  身高 ajax
      if (res.code == 201) {
        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'
    };
    app.postRequest(Education_url, Education_data, function (res) {      //  学历 ajax
        if (res.code == 201) {
        // console.log(res.data);
        that.setData({
          EducationArray: res.data
        })
        }
    })


    //  婚史 ajax
    let Marriage_url = 'public/paramsgather?type=marriage&marriage=1';
    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
        })

      }
   
    })

    //  收入 ajax
    let income_url = 'public/paramsgather?type=shouru';
    let income_data = {
      // pid: '0'
    };
    app.postRequest(income_url, income_data, function (res) {      //  收入 ajax
      if (res.code == 201) {
        // console.log(res.data);
        that.setData({
          incomeArray: res.data
        })
      }

    })

    //  职业 ajax 
    let Occupation_url = 'public/paramsgather?type=zhiye';
    let Occupation_data = {
      pid: '0'
    };
    app.postRequest(Occupation_url, Occupation_data, function (res) {      //  职业 ajax
      if (res.code == 201) {
          // console.log(res);
          that.setData({
            OccupationArray: 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
    //   })
    // })


    this.getProvince();   //地址  ajax


  },


  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
    })
  },

  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)
    // console.log(this.data.MarriageArray[e.detail.value].id);
    this.setData({
      Marriageindex: e.detail.value,
      marry: this.data.MarriageArray[e.detail.value].id
    })
  },

  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   // 区
    })
  },




  // 上传图片
  chooseImageTap: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
  
        console.log(res.tempFilePaths[0]);
        // tempFilePath可以作为img标签的src属性显示图片
        _this.setData({
          uploadImages: res.tempFilePaths,
          isMaxImagesNum: true,
          uploadimg: false,
          photo: res.tempFilePaths[0],
        });

      }
    })


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

  //下一步事件
  register: function (e) {
    // console.log(this.data.photo);    //  头像
    // console.log(this.data.nickname);    //  名字  
    // console.log(this.data.sex);    //  性别  
    // console.log(this.data.age);   //  年龄 
    // console.log(this.data.height);     //  身高 
    // // console.log(this.data.city);     //  区域 -市-县
    // console.log(this.data.education);      //  学历  
    // console.log(this.data.marry);       //   婚史 
    // console.log(this.data.income);       //  收入 

    // if (this.data.photo != ''){
    //   console.log(1);
    // }

var photo = this.data.photo            //头像
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  // 职业



    if (photo == '') {
      var _photo = '请上传头像'
      getApp().Tips(_photo)
    } else if (nickname == '') {
      var _nickname = '请填写姓名'
      getApp().Tips(_nickname)
    } else if (sex == '') {
      var _sex = '请选择性别'
      getApp().Tips(_sex)
    } else if (age == '') {
      var _age = '请选择年龄'
      getApp().Tips(_age)
    } else if (height == '') {
      var _height = '请选择身高'
      getApp().Tips(_height)
    } else if (province == '' && city == ''  && area=='') {
      var _province = '请选择住址'
      getApp().Tips(_province)
    }else if (education == '') {
      var _education = '请选择学历'
      getApp().Tips(_education)
    } else if (marry == '') {
      var _marry = '请选择婚史'
      getApp().Tips(_marry)
    } else if (income == '') {
      var _income = '请选择收入'
      getApp().Tips(_income)
    } else if (occupation == '') {
      var _occupation = '请选择职业'
      getApp().Tips(_occupation)
    }else{
   
        wx.navigateTo({
          url: '/pages/register/fillTel/fillTel?nickname=' + nickname + '&photo=' + photo + '&sex=' + sex + '&age=' + age + '&height=' + height + '&education=' + education + '&marry=' + marry + '&income=' + income + '&city=' + city + '&area=' + area + '&occupation=' + occupation + '&province=' + province +''
        })

    }



   
    // let income_url = 'public/signup';
    // let income_data = {

    //   // openid: _openid,
    // };
    // app.postRequest(income_url, { info: income_data}, function (res) {    
    //   console.log(res);
    //   // that.setData({
    //   //   incomeArray: res.data
    //   // })
    // })


  },

})

function upload(that, id) {
  if (that.data.imageList.length == 0) {
    return;
  }
  wx.uploadFile({
    url: 'public/photo', //仅为示例，非真实的接口地址
    filePath: that.data.imageList[0],
    name: 'photo',
    formData: {
      'id': id
    },
    success: function (res) {
      var data = res.data
      console.log(data);

    }
  })
}
