// pages/homapage/homepage.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    shai: false,
    letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    cityListId: '',
    //下面是城市列表信息,这里只是模拟数据
    citylist: [{
      "letter": "A",
      "data": [{
        "id": "v7",
        "cityName": "安徽"
      }]
    }, {
      "letter": "B",
      "data": [{
        "id": "v10",
        "cityName": "巴中"
      }, {
        "id": "v4",
        "cityName": "包头"
      }, {
        "id": "v1",
        "cityName": "北京"
      }]
    }, {
      "letter": "C",
      "data": [{
        "id": "v15",
        "cityName": "成都"
      }]
    }, {
      "letter": "D",
      "data": [{
        "id": "v21",
        "cityName": "稻城"
      }]
    }, {
      "letter": "G",
      "data": [{
        "id": "v17",
        "cityName": "广州"
      }, {
        "id": "v29",
        "cityName": "桂林"
      }]
    }, {
      "letter": "H",
      "data": [{
        "id": "v9",
        "cityName": "海南"
      }, {
        "id": "v3",
        "cityName": "呼和浩特"
      }]
    }, {
      "letter": "L",
      "data": [{
        "id": "v24",
        "cityName": "洛阳"
      }, {
        "id": "v20",
        "cityName": "拉萨"
      }, {
        "id": "v14",
        "cityName": "丽江"
      }]
    }, {
      "letter": "M",
      "data": [{
        "id": "v13",
        "cityName": "眉山"
      }]
    }, {
      "letter": "N",
      "data": [{
        "id": "v27",
        "cityName": "南京"
      }]
    }, {
      "letter": "S",
      "data": [{
        "id": "v18",
        "cityName": "三亚"
      }, {
        "id": "v2",
        "cityName": "上海"
      }]
    }, {
      "letter": "T",
      "data": [{
        "id": "v5",
        "cityName": "天津"
      }]
    }, {
      "letter": "W",
      "data": [{
        "id": "v12",
        "cityName": "乌鲁木齐"
      }, {
        "id": "v25",
        "cityName": "武汉"
      }]
    }, {
      "letter": "X",
      "data": [{
        "id": "v23",
        "cityName": "西安"
      }, {
        "id": "v28",
        "cityName": "香港"
      }, {
        "id": "v19",
        "cityName": "厦门"
      }]
    }, {
      "letter": "Z",
      "data": [{
        "id": "v8",
        "cityName": "张家口"
      }]
    }],
    //下面是热门城市数据,模拟数据
    newcity: ['北京', '上海', '广州', '深圳', '成都', '杭州'],
    // citySel: '全国',
    locateCity: '',
    type: "",
    record: '',
    showcountry: false,
    showcity: false,
    showregin:false,
    countrylist: [],
    citylist: [],
    reginlist:[],
    sort_id: '',
    country: '',
    city: "",
    region: '',
    odor_id: "",
    kindlist: [{
      type: 1,
      name: '品类',
      sle: false

    }, {
      type: 2,
      name: '国别',
      sle: false

    }, {
      type: 3,
      name: '产区',
      sle: false

    }, {
      type: 4,
      name: '香型',
      sle: false

    }],
    currentCity:''
    


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      url: app.globalData.url
    })
    console.log(this.data.url)
    this.getshailist();
    this.gethomeindex();
    this.getcity()
  },

  //进入搜索页
  entersearch(){
    wx.navigateTo({
      url: '/pages/homeblock/search/search',
    })
  },

  //进入品牌页
  jumpbrand(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/homeblock/brandlist/brandlist?id='+id,
    })
  },


  //获取用户当前位置
  getcity() {
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        // 经纬度
        var latitude = res.latitude
        var longitude = res.longitude

        // that.setData({
        //   wd: latitude,
        //   jd: longitude
        // })

        // that.gethostipallist()
        var aK = that.data.aK
        wx.request({
          url: 'https://api.map.baidu.com/geocoder/v2/?ak=y8jPDTMIuAnaVscUztce1RKfNx8v5sok&location=' + latitude + ',' + longitude + '&output=json',
          data: {},
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log('1234567890', res)
            var province = res.data.result.addressComponent.province;
            let district = res.data.result.addressComponent.district
            that.setData({
              currentCity: province
            })
            wx.setStorageSync('city', province)
            wx.request({
              // url: 'xxx' + city,
              // data: {},
              // header: {
              //   'content-type': 'application/json'
              // },
              // success: function (res) {
              //   that.setData({
              //     county: res.data,
              //   })
              // },
            })
          }
        })

      },
      fail: function () {
        wx.showToast({
          title: '授权失败,请打开GPS重新进入页面授权',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },


  letterTap(e) {
    console.log(e)
    const Item = e.currentTarget.dataset.item;
    console.log(Item)
    this.setData({
      cityListId: Item
    });
    console.log("..............." + this.data.cityListId);
  },
  //获取筛选内容
  getshailist() {
    let that = this;
    var url = "/index/Index/filter"
    var params = {

    }

    app.post(url, params).then((res) => {

      that.setData({
        record: res.wine_count,
       
      })


      if (that.data.type == 1) {
        that.setData({
          shaikind: res.sort,
         
        })
      } else if (that.data.type == 2) {
        that.setData({
          countrylist: res.country
        })

        let newcountrylist = that.data.countrylist;
        for (var obj of newcountrylist) {
          obj.sel = false
        }

        that.setData({
          countrylist:newcountrylist
        })
      } else if (that.data.type == 3) {
        if(that.data.kindlist[1].name=='中国'){
          that.setData({
            citylist: res.region
          })
        }else{
          that.setData({
            citylist: []
          })
        }
         

        let newcitylist = that.data.citylist;
        for (var obj of newcitylist) {
          obj.sel = false
        }

        that.setData({
          citylist:newcitylist
        })
        
      } else if (that.data.type == 4) {
        that.setData({
          shaikind: res.odor
        })
      }

      let newshaikind = that.data.shaikind;
      for (var obj of newshaikind) {
        obj.sel = false
      }

      that.setData({
        shaikind: newshaikind
      })

      let newnewshaikind = that.data.shaikind;

      for (var obj of newnewshaikind) {


        if (that.data.type == 1) {
          if (obj.id == that.data.sort_id) {
            obj.sel = true
          }
        } else if (that.data.type == 4) {
          if (obj.id == that.data.odor_id) {
            obj.sel = true
          }
        }

      }

      that.setData({
        shaikind: newnewshaikind
      })


    }).catch((err) => {


    })
  },

  // 点击筛选

  shaikind(e) {
    let that = this;
    let type = e.currentTarget.dataset.type
    that.setData({
      type: e.currentTarget.dataset.type,
      // shai: true
    })

    if (type == 2) {
      that.setData({
        shai: false,
        showcountry: true

      })
    }else if(type==3){
      that.setData({
        shai: false,
        showcity: true

      })
    }else{
      that.setData({
        shai: true,
        showcity: false,
        showcountry: false

      })
    }

    let newkindlist = that.data.kindlist;
    for (var obj of newkindlist) {
      if (obj.type == type) {
        obj.sel = !obj.sel;
        if (obj.sel == true) {
          that.setData({
            shai: true,
          })
        } else {
          that.setData({
            shai: false,
          })
        }
      } else {
        obj.sel = false
      }
    }

    that.setData({
      kindlist: newkindlist
    })

    that.getshailist();

  },
  //选择类别
  selectkind(e) {


    let id = e.currentTarget.dataset.id;
    console.log(id)

    let newshaikind = this.data.shaikind;

    for (var obj of newshaikind) {
      if (obj.id == id) {
        obj.sel = !obj.sel
      } else {
        obj.sel = false
      }

      if (this.data.type == 1) {
        if (obj.sel == true) {
          this.setData({
            sort_id: obj.id,
            
          })

          let newkindlist=this.data.kindlist;
          newkindlist[0].name = obj.sort_name;
          this.setData({
            kindlist:newkindlist
          })
        }
      } else if (this.data.type == 4) {
        if (obj.sel == true) {
          this.setData({
            odor_id: obj.id
          })

          let newkindlist = this.data.kindlist;
          newkindlist[3].name = obj.odor_name;
          this.setData({
            kindlist: newkindlist
          })
        }
      }


    }


    this.setData({
      shaikind: newshaikind
    })

    // this.setData({
    //   shai:false
    // })
  },

  hideregister() {
    this.setData({
      shai: false
    })
    let newkindlist = this.data.kindlist;
    for (var obj of newkindlist) {
      obj.sel = false;
    }

    this.setData({
      kindlist: newkindlist
    })

    this.gethomeindex()
  },

  //筛选首页内容
  gethomeindex() {
    let that = this;
    var url = "/index/Index/index"
    var params = {
      sort_id: that.data.sort_id,
      country: that.data.country,
      city: that.data.city,
      region: that.data.region,
      odor_id: that.data.odor_id

    }

    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        newcity: res.top,
        citylist: res.brand_list
      })
      let newbrand_list = that.data.citylist;
    }).catch((err) => {
      console.log(err)

    })
  },
  // 选择国家
  selectcountry(e) {
    let countryid = e.currentTarget.dataset.id;

    let newcountrylist = this.data.countrylist;

    for (var obj of newcountrylist) {
      obj.sel = !obj.sel;
      if (obj.sel == true) {
        if (obj.id == countryid) {
          let newkindlist = this.data.kindlist;

          newkindlist[1].name = obj.country_name
          this.setData({
            kindlist: newkindlist,
            country: obj.country_name
          })
        } else {
          obj.sel = false
        }
      }


    }

    this.setData({
      countrylist: newcountrylist
    })

  },
  //选择城市
  selectcity(e){
    let cityindex = e.currentTarget.dataset.index;
    console.log(cityindex)
   

    let newcitylist = this.data.citylist;
   

    for (var i=0;i<newcitylist.length;i++) {
     
     
      if (i == cityindex) {
       
        this.setData({
          reginlist: newcitylist[i].region_list, 
          showregin:true,
          showcity:false,
          city: newcitylist[i].city
        })

        } 
    }
    let newreginlist=this.data.reginlist;
    for (var obj of newreginlist){
      obj.sel=false
    }
    this.setData({
      reginlist:newreginlist
    })
  },

  //选择区
  selectregin(e){
    let reginindex=e.currentTarget.dataset.index;
    let newreginlist=this.data.reginlist;
    for(var i=0;i<newreginlist.length;i++){
    //  newreginlist[i].sel = !newreginlist[i].sel
      if(i==reginindex){
        console.log(243782347)
        newreginlist[i].sel=true;
        let newkindlist = this.data.kindlist;
        newkindlist[2].name = newreginlist[i].name;
        this.setData({
          kindlist: newkindlist,
          region: newreginlist[i].name
        })

        console.log(this.data.kindlist)
        
      }else{
        newreginlist[i].sel=false
      }
    }

    this.setData({
      reginlist:newreginlist
    })

    console.log(this.data.reginlist)

  },


//确定选择国家
  countrysure() {
    this.setData({
      showcountry: false,
      shai:false
    })

    this.gethomeindex()
  },
  //确定选择省
  citysure(){
    this.setData({
      showregin: false,
      shai: false,
      showcity: false

    
    })

    this.gethomeindex()
  },

  //选择区

  sureregin(){
    this.setData({
      showregin:false,
      shai:false,
      showcity:false
    })

    this.gethomeindex()
  },

  //筛选
  shaixuan(){
    wx.navigateTo({
      url: '/pages/homeblock/zhinengselect/zhinengselect',
    })
    // this.gethomeindex()
  },

  choseaddress(){
    wx.navigateTo({
      url: '/pages/homeblock/selectcity/selectcity',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let city = wx.getStorageSync("cityname");
    console.log(city)
    if(city!=''){
      this.setData({
        currentCity: city
      })
    }
    // this.getshailist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})