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

    },

    /**
     * 组件的初始数据
     */
    data: {
        value: '',
        active: 1,
        activeKey: 0,
        food: [],
        homestay: [],
        department: [],
        banner: null,
        active1:0,
        currentTab:0,
        tabs: [
          {
            title: '村落',
            icon: 'https://file-cloud.fmode.cn/sHNeVwSaAg/20230812/gu1jnl043245587.png'
          },
          {
            title: '美食',
            icon: 'https://file-cloud.fmode.cn/sHNeVwSaAg/20230812/53a71a043348365.png'
          },
          {
            title: '民宿',
            icon: 'https://file-cloud.fmode.cn/sHNeVwSaAg/20230812/ljkjrg043355069.png'
          },
          {
            title: '商超',
            icon: 'https://file-cloud.fmode.cn/sHNeVwSaAg/20230812/teofq2043403030.png'
          },
        ],
        tabIndex: 0,
        sidebarIndex: 0,
        type:'catering',
        rankList: [{
            title: '村落'
          }, {
            title: '美食'
          }, {
            title: '民宿'
          }, {
            title: '商超'
          }],
          latitude: 0,
          longitude: 0,
    },
    ready: async function() {
        // 在组件布局完成后执行，确保options参数中有data信息
        this.getData()
    },
    /**
     * 组件的方法列表
     */
    methods: {
        async getData() {
            // this.getShopStore()
            // this.gethomestay()
            if (this.data.latitude == 0 && this.data.longitude == 0) {
              let {
                latitude,
                longitude
              } = await this.getLocation()
              this.setData({
                latitude,
                longitude
              })
            }
            this.getBanner()
            this.getRank()
            this.getHomeShow()
            // this.getDepartment()
        },
        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,
            })
          },
        villagedetails(e) {
            let id = e.currentTarget.dataset.item.objectId
            wx.navigateTo({
                url: '/nova-tourism/pages/home/village/village-details/index?id=' + id
            });

        },
        onPullDownRefresh: function() {
            this.setData({
                pageNum: 1,
                theEnd: false
            }, () => {
                // this.getList().then(res => wx.stopPullDownRefresh())
            })
        },
        village() {
            wx.navigateTo({
                url: '/nova-tourism/pages/home/village/index'
            });

        },
        gourmet() {
            wx.navigateTo({
                url: '/nova-tourism/pages/gourmet/index'
            });

        },
        foodlist() {
            wx.navigateTo({
                url: '/nova-tourism/pages/food-list/index'
            });

        },

        homestay() {
            wx.navigateTo({
                url: '/nova-tourism/pages/homestay/hotel-list/index'
            });

        },
        hoteldetails(e) {
            let id = e.currentTarget.dataset.item.objectId
            wx.navigateTo({
                url: '/nova-tourism/pages/homestay/hotel-details/index?id=' + id
            });

        },
        storepackage(e) {
            let id = e.currentTarget.dataset.item.objectId
            wx.navigateTo({
                url: '/nova-tourism/pages/gourmet/store-package/index?id=' + id
            });

        },
        // homestay(e) {
        //     let id = e.currentTarget.dataset.item.objectId
        //     console.log(id);
        //     wx.navigateTo({
        //         url: '/nova-tourism/pages/homestay/hotel-details/index?id=' + id
        //     });

        // },
        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(3)
          let shopStores = await ShopStore.find()
          if (shopStores && shopStores.length > 0) {
            shopStores.forEach(c => {
              listDate.push(c.toJSON())
            })
          }
          return listDate
        },
        async getHomeShow(){
          let discount = await this.descShopStore("perCapita")
          let hot = await this.descShopStore("score")
          this.setData({
            discount,
            hot
          })
        },
        async descShopStore(column) {
          let ShopStore = new Parse.Query('ShopStore')
          ShopStore.notEqualTo('isDeleted', "true")
          ShopStore.equalTo('company', company)
          ShopStore.equalTo('isShow', "true")
          ShopStore.descending(column)
          let shopStore = await ShopStore.first()
          let shop = {}
          if (shopStore?.id) {
            shop = shopStore.toJSON()
          }
          return shop
        },
        // async getShopStore() {
        //     let { type } = this.data
        //     this.setData({
        //       listDate: []
        //     })
        //     let ShopStore = new Parse.Query('ShopStore')
        //     ShopStore.equalTo('company', company)
        //     ShopStore.equalTo('type', type)
        //     ShopStore.equalTo('isShow', "true")
        //     this.data.value && ShopStore.contains('storeName',this.data.value)
        //     let shopStores = await ShopStore.find()
        //     if (shopStores && shopStores.length > 0) {
        //         let listJSON = []
        //         shopStores.forEach(c => {
        //             listJSON.push(c.toJSON())
        //         })
        //         this.setData({
        //             listDate: listJSON
        //         })
        //     }
        //     console.log(this.data.listDate);
        // },
        player() {
            console.log('播放视频')
        },
        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 gethomestay() {
            let ShopStore = new Parse.Query('ShopStore')
            ShopStore.notEqualTo('isDeleted', "true")
            ShopStore.equalTo('company', company)
            ShopStore.equalTo('type', "stay")
            ShopStore.equalTo('isShow', "true")
            let shopStores = await ShopStore.find()
            if (shopStores && shopStores.length > 0) {
                let listJSON = []
                shopStores.forEach(c => {
                    listJSON.push(c.toJSON())
                })
                this.setData({
                    homestay: 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
          }
        },
        onSearch() {
            wx.showToast({
                title: `搜索 ${this.data.value}`,
                icon: 'none',
            });
        },
        onClick() {
            wx.showToast({
                title: `搜索 ${this.data.value}`,
                icon: 'none',
            });
        },
        onChange(e) {
            this.setData({
                value: e.detail,
            });
            this.getShopStore()
        },
        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,
            })
          },
        // 处理点击tab
        onTabClick(e) {
            let id = e.currentTarget.id;
            console.log(id);
            let objMap = {
              '1':{
                type:'catering'
              },
              '2':{
                type:'stay'
              },
              '3':{
                type:'shop'
              },
            }
            this.setData({
              tabIndex: id,
            })
            if(id > 0){
              this.setData({
                type:objMap[id].type
              })
            }
            id > 0 && this.getShopStore(objMap[id].equl)
        },
        onSidebarClick(e) {
          let id = e.currentTarget.id;
          this.setData({
            sidebarIndex: id,
          })
      },
      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,
        })
      },
      
        // switchNav: function (e) {
        //   let page = this;
        //   let id = e.target.id;
        //   if (this.data.currentTab == id) {
        //     return false;
        //   } else {
        //     page.setData({
        //       currentTab: id
        //     });
        //   }
        //   page.setData({
        //     active1: id
        //   });
        // },
    },

})