// component/Search/search.js
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    array: ['美国', '中国', '巴西', '日本'],
    InputFous: false,
    CitySite: false,
    cityList: [],
    site:{
      companyId: app.state.companyId,
      cityId: app.state.cityId,
      siteName: app.state.siteName,
    },
    getCityName: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    InputFous(){
      wx.navigateTo({
        url: '/pages/SearchPage/SearchPage',
      })
    },
    getCityList() {
      let city = "成都",
      _this = this;
      console.log(app.state.admin)
      app.$api('b2b_get_site', {}).then(res => {
        _this.setData({
          cityList: res
        })
        console.log(_this.data.cityList)
      }).catch(err => { })
      if (app.state.locaSite) {
        
        return
      }
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          wx.request({
            url: 'http://apis.map.qq.com/ws/geocoder/v1/',
            data: {
              location: `${res.latitude},${res.longitude}`,
              key: '66YBZ-TC3K2-NNMUD-CC637-2W6WK-AWBM2'
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              console.log("res")
              city = res.data.result.address_component.city.replace('市', '');
              app.state.locaSite = true 
              app.$api('b2b_get_site', {}).then(res => { 
                for (let i = 0; i < res.length; i++) {
                  if (city.indexOf(res[i].siteName.replace("站", "")) != -1) {
                    _this.setData({
                      'site.cityId': res[i].cityId,
                      'site.siteName': res[i].siteName
                    })
                  }
                }
                _this.setData({
                  cityList: res
                })
              }).catch(err => { })
            }
          })
        },
        fail(err) {
          app.$api('b2b_get_site', {}).then(res => {
            _this.setData({
              cityList: res
            })
            console.log(_this.data.cityList)
          }).catch(err => { })
        }
      }) 
    },
    SetCity(e) {
      let that=this;
      let site = {
        cityId: e.target.dataset.msg.cityId,
        companyId: e.target.dataset.msg.companyId,
        siteName: e.target.dataset.msg.siteName,
      }
      that.setData({
        site: site,
        CitySite:false,
      })
      this.triggerEvent('ChangeSite', that.data.site)
      wx.setStorage({
        key: "site",
        data: site,
        success: function() {
          
        },
        fail: function() {
          
        }
      })
    },
    CloseCity(e) {
      if (e.target.dataset.target == "self") {
        this.setData({
          CitySite: false,
        })
      }
    },
    ChangeSite() {
      this.setData({
        CitySite: true,
      })
    },
    bindPickerChange: function(e) {
      this.setData({
        index: e.detail.value
      })
    },
    ListenInput() {
      this.setData({
        InputFous: true,
      })
    },
  }
})