let Parse = getApp().Parse;
const company = getApp().globalData.company
const compute = require("../../../../utils/compute.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    department: [],
    banner: null,
    tabs: [{
        title: '村落推荐',
        icon: 'https://file.ruixiuauto.com/cDBRXuM6Xh/20230807/fq8811044112324.png'
      },
      {
        title: '美食推荐',
        icon: 'https://file.ruixiuauto.com/cDBRXuM6Xh/20230807/mp1e1k044119032.png'
      },
      {
        title: '民宿推荐',
        icon: 'https://file.ruixiuauto.com/cDBRXuM6Xh/20230807/qrnn1v044125861.png'
      },
      {
        title: '商超推荐',
        icon: 'https://file.ruixiuauto.com/cDBRXuM6Xh/20230807/g6bkbs044122180.png'
      },
    ],
    rankList: [{
      title: '村落'
    }, {
      title: '美食'
    }, {
      title: '民宿'
    }, {
      title: '商超'
    }],
    latitude: 0,
    longitude: 0,
  },
  lifetimes: {
    attached() {
      this.getData()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async getData() {
      if (this.data.latitude == 0 && this.data.longitude == 0) {
        let {
          latitude,
          longitude
        } = await this.getLocation()
        this.setData({
          latitude,
          longitude
        })
      }
      this.getBanner()
      this.getRank()
    },
    getLocation() {
      return new Promise((resolve, reject) => {
        wx.getLocation({
          type: 'gcj02',
          success: (res) => {
            resolve({
              latitude: res.latitude,
              longitude: res.longitude
            })
          },
          fail: () => {
            resolve({
              latitude: 0,
              longitude: 0
            })
          },
          complete: () => {}
        });
      })

    },
    async getRank(){
      let {rankList} = this.data
      let village = await this.getDepartment()
      let catering = await this.getShopStore('catering')
      let stay = await this.getShopStore('stay')
      let shop = await this.getShopStore('shop')
      rankList[0]['contents']=village
      rankList[1]['contents']=catering
      rankList[2]['contents']=stay
      rankList[3]['contents']=shop
      this.setData({
        rankList,
        village
      })
    },
    goUrl(e){
      let {url} = e.currentTarget.dataset
      wx.navigateTo({
        url: url,
      })
    },
    tabsGoUrl(e){
      let {index} = e.currentTarget.dataset
      let url = ''
      switch (index) {
        case 0:
          url+=`/nova-tourism/pages/template-2/searching/index?index=${index}`
          break;
        case 1:
          url+=`/nova-tourism/pages/food-list/index`
          break;
        case 2:
          url+=`/nova-tourism/pages/homestay/hotel-list/index`
          break;
        case 3:
          url+=`/nova-tourism/pages/template-2/searching/index?index=${index}`
          break;
        default:
          break;
      }
      wx.navigateTo({
        url: url,
      })
    },
    rankToDetail(e){
      let {key,id} = e.currentTarget.dataset
      let url = ''
      if(key=='村落'){
        url = `/nova-tourism/pages/home/village/village-details/index?id=${id}`
      }else if(key=='美食'){
        url = `/nova-tourism/pages/gourmet/store-package/index?id=${id}`
      }else if(key=='民宿'){
        url = `/nova-tourism/pages/homestay/hotel-details/index?id=${id}`
      }else if(key=='商超'){
        wx.setStorageSync('storeID', id)
        url = `/nova-tourism/pages/index/index?active=3`
      }
      wx.navigateTo({
        url: url,
      })
    },
    async getShopStore(type) {
      let listDate= []
      let ShopStore = new Parse.Query('ShopStore')
      ShopStore.notEqualTo('isDeleted', "true")
      ShopStore.equalTo('company', company)
      ShopStore.equalTo('type', type)
      ShopStore.equalTo('isShow', "true")
      ShopStore.limit(4)
      let shopStores = await ShopStore.find()
      if (shopStores && shopStores.length > 0) {
        shopStores.forEach(c => {
          listDate.push(c.toJSON())
        })
      }
      return listDate
    },
    async getBanner() {
      let Banner = new Parse.Query('Banner')
      Banner.notEqualTo('isDeleted', "true")
      Banner.equalTo('company', company)
      Banner.equalTo('isEnabled', "true")
      Banner.equalTo('type','home')
      let banner = await Banner.find()
      if (banner && banner.length > 0) {
        let listJSON = []
        banner.forEach(c => {
          listJSON.push(c.toJSON())
        })
        this.setData({
          banner: listJSON
        })
      }
    },
    async getDepartment() {
      let Department = new Parse.Query('Department')
      Department.notEqualTo('isDeleted', "true")
      Department.equalTo('company', company)
      let department = await Department.find()
      if (department && department.length > 0) {
        let listJSON = []
        department.forEach(c => {
          let dJSON = c.toJSON()
          let distances = compute.computeDistance(
            this.data.latitude,
            this.data.longitude,
            dJSON.location.latitude,
            dJSON.location.longitude
        )
          dJSON.distances = distances
          listJSON.push(dJSON)
        })
        return listJSON
      }
    },
  },

})