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

  },

  /**
   * 组件的初始数据
   */
  data: {
    imgUrls: [
      'https://file-cloud.fmode.cn/sHNeVwSaAg/20230530/v61567044111362.png',
      'https://file-cloud.fmode.cn/sHNeVwSaAg/20230530/aijk11044111180.png',
      'https://file-cloud.fmode.cn/sHNeVwSaAg/20230530/6eqp18044111072.web',
      'https://file-cloud.fmode.cn/sHNeVwSaAg/20230530/5rv371044110921.png'
    ],
    latitude: 0,
    longitude: 0,
    active: 0,
    list: [],
    showTab: false,
    stickytop: navigationBarHeight,
    tabs: [{
        title: '推荐',
        icon: 'https://s1.ax1x.com/2023/04/10/ppqEqdH.png'
      },
      {
        title: '评分',
        icon: 'https://s1.ax1x.com/2023/04/10/ppqkofx.png'
      },
      {
        title: '距离',
        icon: 'https://s1.ax1x.com/2023/04/10/ppqkIt1.png'
      },
      {
        title: '价格',
        icon: 'https://s1.ax1x.com/2023/04/10/ppqkh79.png'
      },
    ],
    tabIndex: 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.getShopStore()
      await this.getCategory()
      // this.data.textItem
      this.getShopRank(this.data.textItem)
    },
    async getBanner() {
      let Banner = new Parse.Query('Banner')
      Banner.notEqualTo('isDeleted', "true")
      Banner.equalTo('company', company)
      Banner.equalTo('isEnabled', "true")
      Banner.equalTo('type','homestay')
      let banner = await Banner.find()
      if (banner && banner.length > 0) {
        let listJSON = []
        banner.forEach(c => {
          listJSON.push(c.toJSON())
        })
        this.setData({
          banner: listJSON
        })
      }
    },
    async getCategory(){
      let cate = new Parse.Query("Category")
      cate.notEqualTo('isDeleted', "true")
      cate.equalTo("company",company)
      cate.equalTo("type",'foodTag')
      let cateDate = await cate.find()
      let cateList = []
      cateDate.forEach(res=>{
        let item = res.toJSON()
        cateList.push(item)
      })
      if(cateList.length){
        this.setData({
          textItem:cateList[0].objectId,
        })
      }
      this.setData({
        cateList
      })
    },
    goUrl(e) {
      let {
        url
      } = e.currentTarget.dataset
      wx.navigateTo({
        url: url,
      })
    },
    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: () => {}
        });
      })

    },
    onTabClick(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({
        tabIndex: index,
        active: index
      })
      this.getShopStore()
    },
    async getShopStore() {
      let ShopStore = new Parse.Query('ShopStore')
      ShopStore.notEqualTo('isDeleted', "true")
      ShopStore.equalTo('company', company)
      ShopStore.equalTo('type', "catering")
      ShopStore.include("category")
      ShopStore.limit(10)
      if (this.data.active == 0) {
        ShopStore.descending('isShow')
      }
      if (this.data.active == 1) {
        ShopStore.descending('score')
      }
      if (this.data.active == 3) {
        ShopStore.ascending('perCapita')
      }
      // if (this.data.value) {
      //   ShopStore.contains('storeName', this.data.value)
      // }
      let shopStores = await ShopStore.find()
      if (shopStores) {
        let listJSON = []
        for (let i = 0; i < shopStores.length; i++) {
          let json = shopStores[i].toJSON()
          let distances = compute.computeDistance(
            this.data.latitude,
            this.data.longitude,
            json.location.latitude,
            json.location.longitude
          )
          json.distances = distances
          listJSON.push(json)
        }
        //按距离排序
        if (this.data.active == 2) {
          listJSON = listJSON.sort((a, b) => {
            return a.distance - b.distance
          })
        }
        this.setData({
          list: listJSON
        })
      }
    },
    async getShopRank(id) {
      let ShopStore = new Parse.Query('ShopStore')
      ShopStore.notEqualTo('isDeleted', "true")
      ShopStore.equalTo('company', company)
      ShopStore.equalTo('type', "catering")
      ShopStore.equalTo('category', id)
      ShopStore.include("category")
      let shopStores = await ShopStore.find()
      if (shopStores) {
        let rankList = []
        for (let i = 0; i < shopStores.length; i++) {
          let json = shopStores[i].toJSON()
          rankList.push(json)
        }
        this.setData({
          rankList
        })
      }
    },
    changeText(e) {
      let {
        id
      } = e.currentTarget.dataset
      this.setData({
        textItem: id
      })
      this.getShopRank(id)
    }
  }
})