let Parse = getApp().Parse
const company = getApp().globalData.company
let navigationBarHeight = getApp().globalData.statusBarHeight + 44;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        stickytop: navigationBarHeight,
        objMap : {
          '0':{
            type:'village',
            title:"村落推荐"
          },
          '1':{
            type:'catering',
            title:"美食推荐"
          },
          '2':{
            type:'stay',
            title:"民宿推荐"
          },
          '3':{
            type:'shop',
            title:"商超推荐"
          },
        },
        activeColor:"#229293"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        activeColor:getApp().globalData.activeColor || '#229293'
    })
      let {index} = options
      if(index){
        let idx = Number(index)
        this.setData({
          active:idx
        })
      }
      if(this.data.active==0){
        this.getVillage()
      }else{
        let {objMap} =this.data
        this.getShopStore(objMap[this.data.active].type)
      }
    },
    goUrl(e){
      let {id} = e.currentTarget.dataset
      let {active} = this.data
      let url = ''
      if(active==0){
        url=`/nova-tourism/pages/home/village/village-details/index?id=${id}`
      }else if(active==1){
        url=`/nova-tourism/pages/gourmet/store-package/index?id=${id}`
      }else if(active==2){
        url=`/nova-tourism/pages/homestay/hotel-details/index?id=${id}`
      }else if(active==3){
        wx.setStorageSync('storeID',id);
        url=`/nova-tourism/pages/index/index?active=3`
      }
      wx.navigateTo({
        url: url,
      })
    },
    onChange(e){
      console.log(e);
      let {detail} = e
      this.setData({
        value:detail
      })
      this.tabChange()
    },
    async tabChange(e) {
      console.log(e);
      if(e?.detail){
        this.setData({
          active:e.detail.index,
        })
      }
      let {objMap} =this.data
      if(this.data.active==0){
        console.log('sdks');
        this.getVillage()
      }else{
        this.getShopStore(objMap[this.data.active].type)
      } 
      console.log(this.data.listDate);
    },
    async getVillage(){
      let listJSON = []
      let Department = new Parse.Query('Department')
      Department.notEqualTo('isDeleted', "true")
      Department.equalTo('company', company)
      this.data.value && Department.contains('shortname', this.data.value)
      let department = await Department.find()
      if (department && department.length > 0) {
        department.forEach(c => {
          let dJSON = c.toJSON()
          listJSON.push(dJSON)
        })
      }
      this.setData({
        listDate:listJSON
      })
      console.log(this.data.listDate);
    },
    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")
      this.data.value && ShopStore.contains('storeName', this.data.value)
      let shopStores = await ShopStore.find()
      if (shopStores && shopStores.length > 0) {
        shopStores.forEach(c => {
          listDate.push(c.toJSON())
        })
      }
      this.setData({
        listDate
      })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})