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: [],
    value: null,
    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,
    banners: []
  },
  ready: function () {
    // 在组件布局完成后执行，确保options参数中有data信息
    this.getBanner()
    this.getData()
    // this.selectComponent('#tabs').resize();
  },
  navigateComments(e) {
    let id = e.currentTarget.dataset.item.objectId
    wx.navigateTo({
      url: `/nova-tourism/pages/gourmet/store-package/comment/index?id=` + id
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async getData() {
      if (this.data.latitude == 0 && this.data.longitude == 0) {
        let {
          latitude,
          longitude
        } = await this.getLocation()
        this.setData({
          latitude,
          longitude
        })
      }
      this.getShopStore()
    },
    storepackage(e) {
      let id = e.currentTarget.dataset.item.objectId
      wx.navigateTo({
        url: '/nova-tourism/pages/gourmet/store-package/index?id=' + id
      });

    },
    getLocation() {
      return new Promise((resolve, reject) => {
        wx.getLocation({
          type: 'gcj02',
          success: (res) => {
            resolve({
              latitude: res.latitude,
              longitude: res.longitude
            })
          },
          fail: () => {},
          complete: () => {}
        });
      })

    },



    onSearch() {
      wx.showToast({
        title: `搜索 ${this.data.value}`,
        icon: 'none',
      });
    },
    onTabClick(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({
        tabIndex: index,
        active: index
      })
      this.getShopStore()
    },
    onClick() {
      this.setData({
        value: null,
      });
    },
    onChan(e) {
      this.setData({
        value: e.detail,
      });
      this.getShopStore()
    },
    onChange(event) {
      let active = event.detail.name
      this.setData({
        active: active
      })
      this.getShopStore()

    },
    async getShopStore() {
      let ShopStore = new Parse.Query('ShopStore')
      ShopStore.notEqualTo('isDeleted', "true")
      ShopStore.equalTo('company', company)
      ShopStore.equalTo('type', "catering")
      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++) {
          listJSON.push(shopStores[i].toJSON())
        }

        for (let i = 0; i < listJSON.length; i++) {
          let item = listJSON[i]
          if (item.location) {
            let distance = compute.computeDistance(
              this.data.latitude,
              this.data.longitude,
              item.location.latitude,
              item.location.longitude
            )
            item.distance = distance.toFixed(2)

            listJSON[i] = item
          }
        }
        //按距离排序
        if (this.data.active == 2) {
          listJSON = listJSON.sort((a, b) => {
            return a.distance - b.distance
          })
        }

        this.setData({
          list: listJSON
        })
      }
    },
    async getBanner() {
      let Banner = new Parse.Query('Banner')
      Banner.notEqualTo('isDeleted', "true")
      Banner.equalTo('company', company)
      Banner.equalTo('isEnabled', "true")
      Banner.equalTo('type', 'gourmet')
      let banner = await Banner.find()
      if (banner && banner.length > 0) {
        let listJSON = []
        banner.forEach(c => {
          listJSON.push(c.toJSON())
        })
        this.setData({
          banners: listJSON
        })
      }
    },
  }
})