const app = getApp();

Page({
  data: {
    Ageid: 0,
    AgeArray: [],    //  年龄-数据
    AgeIndex: [0, 0],    //  年龄-下标


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

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

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

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

    inputValue: '',      //  id 

    age: '',      //  年龄  
    height: '',      //  身高  
    marry: '',    //   婚史 
    idmarry: 0,
    education: '',    //  学历  
    educationid: 0,
    income: 0,     //  收入 
    Zero:0, //处理不限
    Zero1: 0,  //处理不限
    heightZero: 0, //处理不限
    heightZero1: 0,  //处理不限
    incomeZero:0,
    incomeZero1:0,
    // min = '';
    // max = '';


    // 省市区
    city: 0,     // 市  要传的数据
    provinceName: '',     // 省 要传的数据
    cityName: '',     // 市  要传的数据
    areaName: '',     // 区 要传的数据

    provincedata: '', //省数据
    provinceid: 1,   //省选中ID
    provincetext: '北京', //省 名字
    citydata: '', //市数据
    cityidx: '', //市选中ID
    citytext: '', //市 名字
    areadata: '', //区数据

    scrolltop: '', //市区滚动条
    hiddenName: true, //显示隐藏


  },


  onLoad: function (option) {
    var that = this

    // console.log(option)

    //先清空要传的数据
    app.globalData.Selection = 0;    //  是否选中  
    app.globalData.age = 0;    //  年龄  
    app.globalData.height = 0;    //  身高  
    app.globalData.marry = 0  //  婚史  
    app.globalData.education = 0  //  学历  
    app.globalData.income = 0;     //  收入 

    

    var _city = option.city      //  市  

    var _provincename = option.provinceName      //  省  
    var _cityname = option.cityName      //  市  
    var _areaname = option.areaName      //  区  

 

    var _idmarry = option.idmarry
    var _educationid = option.educationid    


    that.setData({
      city: _city,
      provinceName: _provincename,  // 省
      cityName: _cityname, // 市
      areaName: _areaname,  // 区

      idmarry: _idmarry,
      educationid: _educationid,

    })


    // console.log(that.data.city)
    // console.log(that.data.provinceName)
    // console.log(that.data.cityName)
    // console.log(that.data.areaName)



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

    var _age = option.age      //  年龄  

    var agenewArry = _age.split("-");  //转成数组
    var _age_min = agenewArry[0]
    var _age_max = agenewArry[1]
    // console.log(_age_min)
    // console.log(_age_max)
    that.setData({
      age: _age
    })


    app.postRequest(Age_url, Age_data, function (res) {   //  年龄 ajax
      if (res.code == 201) {

        let newarray = ['不限'];
        var res_data = newarray.concat(res.data);      //在原有的数据上 凭借上 不限

        let newarray1 = ['以上'];
        var res_data1 = newarray1.concat(res.data);      //在原有的数据上 凭借上 以上


        let min = data_processing(res_data, _age_min);      //已选中的数据处理  min
        let max = data_processing(res_data1, _age_max);   //已选中的数据处理  max

      if (min == 0 && max == 0){
          that.setData({
            AgeIndex: [min, max],
            Zero:1,
          })
       }else{
        that.setData({
          AgeIndex: [min, max],
          Zero:0,
        })
       }


        let dictObject = res.data;    //对象不能直接使用  以下转成数组
        // 对象转成数组
        var createArr = []
        for (let i in dictObject) {
          createArr.push(dictObject[i] + "岁");      //在数据后面凭借  岁
        }

        let bx = ['不限'];
        var Unlimited = bx.concat(createArr);   //在原有的数据上 凭借上   不限

        let ys = ['以上'];
        var Above = ys.concat(createArr);       //在原有的数据上 凭借上    以上



        that.setData({
          AgeArray: [Unlimited, Above],
        })

        // console.log(createArr);           
      }

    })



    //  身高 ajax
    let height_url = 'public/paramsgather?type=height';
    let height_data = {
      pid: '0'
    };

    var _height = option.height      //  身高  

    var heightnewArry = _height.split("-");  //转成数组
    var heightSmall = heightnewArry[0]
    var heightlarge = heightnewArry[1]
    // console.log(heightSmall)
    // console.log(heightlarge)
    that.setData({
      height: _height
    })
    app.postRequest(height_url, height_data, function (res) {      //  身高 ajax
      if (res.code == 201) {

        let newarray = ['不限'];
        var res_data = newarray.concat(res.data);      //在原有的数据上 凭借上 不限

        let newarray1 = ['以上'];
        var res_data1 = newarray1.concat(res.data);      //在原有的数据上 凭借上 以上


        let min = data_processing(res_data, heightSmall);      //已选中的数据处理  min
        let max = data_processing(res_data1, heightlarge);   //已选中的数据处理  max


        if (min == 0 && max == 0) {
          that.setData({
            heightindex: [min, max],
            heightZero: 1,
          })
        } else {
          that.setData({
            heightindex: [min, max],
            heightZero: 0,
          })
        }



        let dictObject = res.data;        //对象不能直接使用  以下转成数组
        // 对象转成数组
        var createArr = []
        for (let i in dictObject) {
          createArr.push(dictObject[i] + "cm");
        }


        let bx = ['不限'];
        var Unlimited = bx.concat(createArr);

        let ys = ['以上'];
        var Above = ys.concat(createArr);

        // console.log(createArr);
        that.setData({
          heightArray: [Unlimited, Above],
        })
      }

    })


    //  婚史 ajax
    let Marriage_url = 'public/paramsgather?type=marriage';
    let Marriage_data = {
      pid: '0'
    };
    var _marry = option.marry   //   婚史 
    // console.log(_marry)
    that.setData({
      marry: _marry
    })
    app.postRequest(Marriage_url, Marriage_data, function (res) {      //  婚史 ajax
      if (res.code == 201) {
        // console.log(res.data);
        for (let i = 0; i < res.data.length; i++) {    //已选中的数据处理
          if (i == _marry) {
            that.setData({
              Marriageindex: i,
            })
          }
        }

        that.setData({
          MarriageArray: res.data,
          idmarry: that.data.idmarry,
        })

      }

    })


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

        var obj = { id: 0, education_name: '不限' };
        var datares = res.data;
        datares.unshift(obj)


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


        that.setData({
          EducationArray: datares,
          educationid: that.data.educationid
        })
      }
    })




    //  收入 ajax
    let income_url = 'public/paramsgather?type=shouru1';
    let income_data = {
      // pid: '0'
    };
    var _income = option.income      //  收入  

    var incomenewArry = _income.split("-");  //转成数组
    var incomemin = incomenewArry[0];
    var incomemax = incomenewArry[1];

    that.setData({
      income: _income
    })
    app.postRequest(income_url, income_data, function (res) {      //  收入 ajax
      if (res.code == 201) {

        let newarray = ['不限'];
        var res_data = newarray.concat(res.data);      //在原有的数据上 凭借上 不限

        let newarray1 = ['以上'];
        var res_data1 = newarray1.concat(res.data);      //在原有的数据上 凭借上 以上

   
        var min = data_processing(res_data, incomemin);      //已选中的数据处理  min
        var max = data_processing(res_data1, incomemax);   //已选中的数据处理  max
     

        if (min == 0 && max == 0) {
 
          that.setData({
            incomeindex: [min, max],
            incomeZero: 1,
          })
  
        } else {

          that.setData({
            incomeindex: [min, max],
            incomeZero: 0,
          })

        }


        that.setData({
          incomeArray: [res_data, res_data1],
        })


      }

    })





  },


  // 年龄双选
  AgeEvent: function (e) {
    var that= this;
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    var _value = e.detail.value;  //发送选择改变,携带值为
    var Small = this.data.AgeArray[0][e.detail.value[0]].substring(0, 2);
    var large = this.data.AgeArray[1][e.detail.value[1]].substring(0, 2);


    // console.log(Agedata);

    if (Small > large && Small != '不限' && large != '以上') {     //判断  最小值  大于  最大值  数据互换
      //  console.log('调换')
      var Agedata = large + '-' + Small;
      that.Exchange(_value);    // 调用数组调换

    } else {

      var Agedata = Small + '-' + large;    //否则正常选中

    }


    if (Small == '不限' && large == '以上'){
      that.setData({
        Zero1: 1
      })
    }else{
     that.setData({
       Zero1:0
      })
    }



    this.setData({
      AgeIndex: _value,
      age: Agedata,
      Ageid: 1,
    })

  },

  // 数组数据调换
  Exchange: function (data) {
    for (var i = 0; i < data.length / 2; i++) {
      var data_i = data[i];
      data[i] = data[data.length - 1 - i];
      data[data.length - 1 - i] = data_i;
    }
    return;
  },




  heightEvent: function (e) {   //  身高    
    var that = this;
    // console.log(this);
    // console.log(e);
    // console.log(this.data.heightArray[e.detail.value]);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    var _value = e.detail.value;  //发送选择改变,携带值为
    var heightSmall = this.data.heightArray[0][e.detail.value[0]].substring(0, 3);
    var heightlarge = this.data.heightArray[1][e.detail.value[1]].substring(0, 3);

    // console.log(heightdata)
    if (heightSmall > heightlarge && heightSmall != '不限' && heightlarge != '以上') {     //判断  最小值  大于  最大值  数据互换
      //  console.log('调换')
      var heightdata = heightlarge + '-' + heightSmall;
      that.Exchange(_value);    // 调用数组调换

    } else {

      var heightdata = heightSmall + '-' + heightlarge;

    }

    if (heightSmall == '不限' && heightlarge == '以上') {
      that.setData({
        heightZero1: 1
      })
    } else {
      that.setData({
        heightZero1: 0
      })
    }





    that.setData({
      heightindex: _value,
      height: heightdata,
      heightid: 1,
    })
  },

  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,
      idmarry: 1,
    })
  },



  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,
      educationid: 1,
    })
  },




  incomeEvent: function (e) {   //  收入    单项选择器
    var that = this;
    // console.log(this);
    // console.log(e);
    // console.log(this.data.incomeArray[e.detail.value].id);
    // console.log('picker发送选择改变,携带值为', e.detail.value)
    var _value = e.detail.value;  //发送选择改变,携带值为
    var srSmall = this.data.incomeArray[0][e.detail.value[0]];
    var srlarge = this.data.incomeArray[1][e.detail.value[1]];



    // console.log(Agedata);
    if (srSmall > srlarge && srSmall != '不限' && srlarge != '以上') {     //判断  最小值  大于  最大值  数据互换
      //  console.log('调换')
      var srdata = srlarge + '-' + srSmall;
      that.Exchange(_value);    // 调用数组调换

    } else {

      var srdata = srSmall + '-' + srlarge;  //否则正常选中

    }

    if (srSmall == '不限' && srlarge == '以上') {
      that.setData({
      incomeZero1: 1
      })
    } else {
      that.setData({
       incomeZero1: 0
      })
    }



    that.setData({
      incomeindex: _value,
      income: srdata,
      incomeid: 1,

    })
  },




  // 会员编码 
  getPhone: function (e) {
    var _inputValue = e.detail.value;
    // console.log(_inputValue);
    this.setData({
      inputValue: _inputValue
    });
  },



  //下一步事件
  register: function (e) {
    var _this= this;


    //设置全局变量(app已经定义 var app=getApp())     赋值要传的参数

    app.globalData.inputValue = String(_this.data.inputValue);     //用户ID
    app.globalData.age = String(_this.data.age);    //  年龄  
    app.globalData.height = String(_this.data.height);    //  身高  
    app.globalData.marry = String(_this.data.marry)  //  婚史  
    app.globalData.idmarry = String(_this.data.idmarry)  //  婚史  
    app.globalData.education = String(_this.data.education)  //  学历  
    app.globalData.educationid = String(_this.data.educationid)  //  学历  
    app.globalData.income = String(_this.data.income);     //  收入 
    app.globalData.Selection = String(1);    //  是否选中   1 选中   0没选中

    app.globalData.city = String(_this.data.city);    //  市 

    app.globalData.provinceName = String(_this.data.provinceName);    //  省 
    app.globalData.cityName = String(_this.data.cityName);    //  市 
    app.globalData.areaName = String(_this.data.areaName);    //  区 


    app.globalData.trigger = true;    // 搜索或清空







    wx.switchTab({
      url: '../index/index',
    })


    _this.setData({
    inputValue:'',
    })

  },


  // 重置
  Reset: function (e) {
    var _this = this;

    _this.setData({
      AgeIndex: ['', ''],    //  年龄-下标
      age: 0,     //  年龄
      Ageid: 0,

      heightindex: ['', ''],  // 身高-下标
      height: 0, //身高
      heightid: 0,

      Marriageindex: '',  // 婚史-下标
      marry: 0,
      idmarry: 0,

      Educationindex: '',    //学历
      education: 0,   //学历
      educationid: 0, 

      incomeindex: ['', ''],  //收入
      income: 0,   //收入

      inputValue: '', //ID

      city: 0,  // 市
      provinceName: '',  // 省
      cityName: '', // 市
      areaName: '',  // 区

    })


    app.globalData.inputValue = String(_this.data.inputValue);     //用户ID
    app.globalData.age = String(_this.data.age);    //  年龄  
    app.globalData.height = String(_this.data.height);    //  身高  
    app.globalData.marry = String(_this.data.marry)  //  婚史  
    app.globalData.idmarry = String(_this.data.idmarry)  //  婚史  
    app.globalData.education = String(_this.data.education)  //  学历  
    app.globalData.educationid = String(_this.data.educationid)  //  学历  
    app.globalData.income = String(_this.data.income);     //  收入 
    app.globalData.Selection = String(1);    //  是否选中   1 选中   0没选中

    app.globalData.city = String(_this.data.city);    //  市 

    app.globalData.provinceName = String(_this.data.provinceName);    //  省 
    app.globalData.cityName = String(_this.data.cityName);    //  市 
    app.globalData.areaName = String(_this.data.areaName);    //  区 

    app.globalData.trigger = true;    // 搜索或清空




  },






  // 省市区三级联动 点击弹窗
  linkage: function (e) {
    var that = this;
    let citypid_url = 'public/citypid';
    let citypid_data = {};
    app.postRequest(citypid_url, citypid_data, function (res) {      //  省市区三级联动 ajax
      if (res.code == 201) {

        that.setData({
          provincedata: res.data,   // 省数据
        })

        // console.log(res.data)
        that.provinceOn();
      }
    })

  },


  // 省事件
  provinceOn: function (e) {
    var that = this;
    // console.log(e);
    if (e) {
      that.setData({
        provinceid: e.currentTarget.dataset.id,    //省ID
        provincetext: e.currentTarget.dataset.text,   //省名字
      })
    }
    // console.log(that.data.provinceid);
    // console.log(that.data.provincetext)

    let citypid_url = 'public/citypid';
    let citypid_data = {
      pid: that.data.provinceid,
    };
    app.postRequest(citypid_url, citypid_data, function (res) {      //  省市区三级联动 ajax
      if (res.code == 201) {

        that.setData({
          hiddenName: false,
          citydata: res.data,   // 市数据
          scrolltop: 0,    //市区滚动条
          areadata: '',    //区域数据清空
          cityidx: '',   // idx  选中清空
        })

        // console.log(res.data)

      }
    })

  },

  // 市事件
  cityOn: function (e) {
    var that = this;
    var _cityidx = that.data.cityidx
    var cityid = e.currentTarget.dataset.id;     //市ID
    var citytext = e.currentTarget.dataset.text;     //市名字
    var _index = e.currentTarget.dataset.index + 1;     //下标
    // console.log(_index);
    // console.log(_index*42,);
    // console.log(_cityidx);
    // console.log(cityid);
    that.setData({
      cityidx: cityid,  // 市  选中
      citytext: citytext, // 市 选中名字
      areadata: '',    //区域数据清空
    })

    let citypid_url = 'public/citypid';
    let citypid_data = {
      pid: cityid,
    };
    app.postRequest(citypid_url, citypid_data, function (res) {      //  省市区三级联动 ajax
      if (res.code == 201) {

        if (_cityidx == cityid) {
          that.setData({
            areadata: '',   // 区数据
            cityidx: '',   // idx  选中清空
          })
        } else {
          that.setData({
            areadata: res.data,   // 区数据
            scrolltop: _index * 42,
          })
          // console.log('执行')
          //   console.log(_index * 42);
        }
        // console.log(res.data)
      }

    })

  },


  //区选中事件
  areaOn: function (e) {
    var that = this;

    var _style = e.currentTarget.dataset.style;
    // console.log(_style);

    var regionid = e.currentTarget.dataset.id;     //传递地区ID
    // console.log(regionid);

    switch (_style) {

      case 'provinceLx':
        // console.log('省');
        that.setData({
          city: regionid,
          provinceName: that.data.provincetext, // 省名字
          cityName: '',   // 市名字清空
          areaName: "",  //  区名字
        })
        break;

      case 'cityLx':
        // console.log('市');
        that.setData({
          city: regionid,
          provinceName: that.data.provincetext, // 省名字
          cityName: that.data.citytext, // 省名字
          areaName: "",  //  区名字
        })
        break;

      case 'areaLx':
        // console.log('区');
        var areatext = e.currentTarget.dataset.text;     //区名字
        that.setData({
          city: regionid,
          provinceName: that.data.provincetext, // 省名字
          cityName: that.data.citytext, // 省名字
          areaName: areatext,   //  区名字

        })
        break;

    }

    // console.log(that.data.city)
    // console.log(that.data.provinceName)
    // console.log(that.data.cityName)
    // console.log(that.data.areaName)

  
  //选择完清空
    that.setData({
      hiddenName: true,
      provinceid: 1,
      provincetext: '北京', //省 名字
      cityidx: 0,
      citytext: '',

    })


  },

  //省市区不限  清空
  regionBx: function () {
    var that = this;
    that.setData({
      hiddenName: true,
      provinceid: 1,
      provincetext: '北京', //省 名字
      cityidx: 0,
      citytext: '',

      city: 0,   // 市数据清空
      provinceName: '', // 省名字
      cityName: '',   // 市名字清空
      areaName: "",  //  区名字
    })

  },

 
  //省市区关闭   清空
  Close: function () {
    var that = this;
    that.setData({
      hiddenName: true,
      provinceid: 1,
      provincetext: '北京', //省 名字
      cityidx: 0,
      citytext: '',

    })
  },






})


var data_processing = function (data, min_max,) {
  for (let i = 0; i < data.length; i++) {    //已选中的数据处理
    if (data[i] == min_max) { 
      return i;
     }
  }
}