// pages/shop/shop.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinceCityArr: [],//省份城市为二维数组a[[],[]]
    provinceArr: [],//省
    cityArr: [],//市
    index:0,
    provinceIndex:0,
    cityIndex:0,
    inputVal:"",
    scrollHig:0,
    pageNum:1,//页码
    count:10,//每页条数
    showDataArr:[],//展示的数据列表
    dataArr: [],//搜索的数据列表
    getDataArr:[],//每次下拉获取的数据
    topNum:0,//滚动条位置
    latitude: 0,
    longitude: 0,
  },
  bindchange:function(e){
    this.data.pageNum = 1;
    this.data.dataArr = [];
    this.setData({
      inputVal:"",
      topNum:0
    })
    this.searchShop();
  },
  bindcolumnchange: function (e) {
    console.log(e)
    if (this.data.provinceCityArr[0][0] == "") {
      this.data.provinceCityArr[0][0] = "全部"
    }

    if (this.data.provinceCityArr[1][0] == "") {
      this.data.provinceCityArr[1][0] = "全部";
    }
    var clo = e.detail.column;//指移动的哪一列
    var value = e.detail.value;//指列中的哪一个
    var activeArr = [this.data.provinceArr, this.data.cityArr[value]]
    if (clo==0){
      console.log(0)
      this.setData({
        provinceCityArr: activeArr,
        index:0,
        provinceIndex: value,
        cityIndex:0,
      })
    }else{
      this.setData({
        index: value,
        cityIndex:value
      })
      // console.log(this.data.provinceCityArr)
    }
    // console.log(activeArr)
    // console.log(clo, value)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)

    this.data.latitude = wx.getStorageSync('latitude');
    this.data.longitude = wx.getStorageSync('longitude');
    if (app.globalData.openId) {
      this.getShopList();
    } else {
      app.globalData.openidSuccessFuc = this.getShopList;
      // app.globalData.openidSuccessFuc = this.searchShop;
    }
    if (!wx.getStorageSync('latitude')) {
      this.getNowAddress();
    }
    var hig = wx.getSystemInfoSync().windowHeight;
    this.setData({
      scrollHig: hig-70
    })
  },

  getShopList:function(){//获取所有门店省市
    app.wxRequest(app.globalData.httpUrl + 'shop/shop_province', {}, e => {
      // console.log(e)
      var shopProvince = ["全部"];//省份
      var shopCity = [];//城市
      var shopCityAll = ["全部"];//全部城市
      var provinceCity = [];//省份城市为二维数组
      if (e.code == 200) {
        for (var i = 0; i < e.data.length;i++){
          shopProvince.push(e.data[i].shop_province)
          var arr = e.data[i].shop_city.split(",")
          shopCityAll.push(arr)
          shopCity.push(arr)
        }
        shopCityAll = shopCityAll.join(",")
        shopCityAll = shopCityAll.split(",")
        shopCity.unshift(shopCityAll);
        provinceCity.push(shopProvince);
        provinceCity.push(shopCity[0]);
        this.setData({
          provinceArr: shopProvince,
          cityArr: shopCity,
          provinceCityArr: provinceCity,
        })

        this.searchShop();
        // console.log(this.data.provinceArr)
        // console.log(this.data.cityArr)
        // console.log(this.data.provinceCityArr)
      }
    }, this)
  },

  searchShop: function (){//搜索
      var province = "";
      var city = "";
      if (this.data.provinceCityArr[0][this.data.provinceIndex] == "全部") {
        this.data.provinceCityArr[0][this.data.provinceIndex] = "";
      } 
      if (this.data.provinceCityArr[1][this.data.cityIndex] == "全部") {
        this.data.provinceCityArr[1][this.data.cityIndex] = "";
      }
      province = this.data.provinceCityArr[0][this.data.provinceIndex];
      city = this.data.provinceCityArr[1][this.data.cityIndex];
    var locationIndex;
      if (this.data.longitude){
        locationIndex = 2
      }else{
        locationIndex = 1
      }
    var params = {
      "shop_province": province,
      "shop_city": city,
      "key": this.data.inputVal,
      "cur_page": this.data.pageNum,
      "show_num":this.data.count,
      "location": locationIndex,
      "lng": this.data.longitude,
      "lat": this.data.latitude,
    }
    app.wxRequest(app.globalData.httpUrl + 'shop/shopsearch', params, e => {
      console.log(e)
      if (e.code == 200) {
        for (var i = 0; i < e.data.length;i++){
          this.data.dataArr.push(e.data[i]);
        }
        this.setData({
          showDataArr: this.data.dataArr,
          getDataArr: e.data,
        })
        console.log(this.data.showDataArr)
      } else if (e.code == -200){
        wx.showModal({
          title: '没有搜索到匹配的门店',
        })
        // this.setData({
        //   inputVal: ""
        // })
      }
    }, this)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("到顶了")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  lower:function(){
    console.log("到底了")
    if (this.data.getDataArr != "") {
      this.data.pageNum++;
      this.searchShop();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: "none",
        duration: 1000
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "老门框爆肚会员",
      success: res => {
        console.log(res, "转发成功")
      },
    }
  },
  callPhone:function(e){//拨打电话
    var index = e.target.dataset.index;
    console.log(index)
    wx.makePhoneCall({
      phoneNumber: this.data.showDataArr[index].shop_phone
    })
  },
  getInputVal:function(e){//获取input框中value
    console.log(e.detail.value);
    this.setData({
      inputVal:e.detail.value
    })
    if (this.data.inputVal == "") {
      this.data.pageNum = 1;
      this.data.dataArr = [];
      this.searchShop();
    }
  },
  searchFn:function(){//点击搜索按钮
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    console.log(this.data.provinceIndex)
    console.log(this.data.cityIndex)
    console.log(this.data.index)
    this.data.pageNum = 1;
    this.data.dataArr = [];
    this.searchShop();
  },
  deleteVal:function(){//清空搜索框
    this.setData({
      inputVal:""
    })
    // this.data.provinceCityArr[0][this.data.provinceIndex] = "";
    // this.data.provinceCityArr[1][this.data.cityIndex] = "";
    this.data.pageNum = 1;
    this.data.dataArr = [];
    this.searchShop();
  },
  openAddress:function(e){//打开地图
    var index = e.target.dataset.index;
    console.log(index)
    var latitude = this.data.showDataArr[index].shop_latitude;
    var longitude = this.data.showDataArr[index].shop_longtitude;
    latitude = parseFloat(latitude);
    longitude = parseFloat(longitude);
    var addressName = this.data.showDataArr[index].shop_name
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18,
      name: addressName
    })
  },
  getNowAddress: function () {//获取当前城市经纬度
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        console.log(res)
        wx.setStorageSync('latitude', res.latitude);
        wx.setStorageSync('longitude', res.longitude);
        this.data.latitude = wx.getStorageSync('latitude');
        this.data.longitude = wx.getStorageSync('longitude');
        this.data.dataArr=[];
        if (this.data.provinceCityArr[0] && this.data.provinceCityArr[1]){

          this.searchShop();
        }
      }
    })
  },
})