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

    },

    /**
     * 组件的初始数据
     */
    data: {
        /**默认餐厅照片 */
        defaultCover:'https://file-cloud.fmode.cn/sHNeVwSaAg/20230812/j1aeh1052327045.jpg?imageView2/1/w/200/h/200',
      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,
    },
    lifetimes: {
      attached() {
        this.getData()
      }
    },
    // ready: function() {
    //     // 在组件布局完成后执行，确保options参数中有data信息

    //     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.getBanner()
            this.getShopStore()
            await this.getCategory()
            this.getShopRank(this.data.textItem)
        },
        async getBanner() {
          let Banner = new Parse.Query('Banner')
          Banner.equalTo('company', company)
          Banner.equalTo('isEnabled', "true")
          Banner.notEqualTo('isDeleted', "true")
          Banner.equalTo('type','homestay')
          let banner = await Banner.find()
          console.log(banner);
          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.equalTo("company",company)
            cate.notEqualTo('isDeleted', "true")
            cate.equalTo("type",'foodTag')
            let cateDate = await cate.find()
            console.log(cateDate)
            let cateList = []
            cateDate.forEach(res=>{
              let item = res.toJSON()
              cateList.push(item)
            })
            console.log(cateList)
            if(cateList.length){
              this.setData({
                textItem:cateList[0]?.objectId,
              })
            }
            this.setData({
              cateList
            })
          },
          /** 选择美食分类*/
          changeText(e) {
            let {
              id
            } = e.currentTarget.dataset
            this.setData({
              textItem: id
            })
            this.getShopRank(id)
          },
          /**获取美食排行 */
          async getShopRank(id) {
            let ShopStore = new Parse.Query('ShopStore')
            ShopStore.equalTo('company', company)
            ShopStore.equalTo('type', "catering")
            ShopStore.notEqualTo('isDeleted', "true")
            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
              })
            }
          },
        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: () => {}
                });
            })

        },
        onTabClick(e) {
          console.log(e);
          let index = e.currentTarget.dataset.index;
          this.setData({
            // tabIndex: index,
            active: index
          })
          this.getShopStore()
          // this.getCategory()
        },
        onClick() {
            this.setData({
                value: null,
            });
        },
        // onChan(e) {
        //     this.setData({
        //         value: e.detail,
        //     });
        //   this.getCategory()
        //   this.getShopStore()
        // },
        // onChange(event) {
        //     let active = event.detail.name
        //     this.setData({
        //         active: active
        //     })
        //   this.getCategory()
        //   this.getShopStore()

        // },
        async getShopStore() {
            let ShopStore = new Parse.Query('ShopStore')
            ShopStore.equalTo('company', company)
            ShopStore.notEqualTo('isDeleted', "true")
            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
                })
            }
        },
    }
})