<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <view
    :key="renovationId"
    class="Mall4j shop-feature"
    style="padding-bottom: 80rpx;"
  >
    <feature
      ref="featureIndexRef"
      :page-load="pageLoad"
      :page-id="renovationId"
      :shop-id="shopId"
      :page-scorll-top="pageScorllTop"
      @page-loaded="pageLoaded"
    />
    <shop-tabbar
      style="position:fixed;bottom:0;width:100%;left:0;right:0;"
      :renovation-id="renovationId"
      :shop-id="shopId"
    />
    <popup-ad
      v-model="popupadShow"
      :popup-info="popupInfo"
      :shop-id="shopId"
    />
  </view>
</template>

<script setup>
const pageLoad = ref(false)
const renovationId = ref('') // 页面id
const shopId = ref('')
const pageScorllTop = ref(0) // 页面滚动距离

onMounted(() => {
  getShopInfo()
})

onLoad((options) => {
  if (options.renovationId) {
    renovationId.value = options.renovationId
  }
  if (options.shopId) {
    shopId.value = options.shopId
    onGetPopupInfo()
  }
})

// 获取弹窗广告信息
const popupadShow = ref(false)
const popupInfo = ref({})
const onGetPopupInfo = async () => {
  const info = await util.getPopupInfo({ pageType: 4, shopId: shopId.value })
  if (info) {
    popupInfo.value = info
    popupadShow.value = true
  }
}

onPageScroll((e) => {
  pageScorllTop.value = e.scrollTop
})

const featureIndexRef = ref(null)
onPullDownRefresh(() => {
  setTimeout(() => {
    nextTick(() => {
      featureIndexRef.value?.getPageInfoById()
    })
    uni.stopPullDownRefresh() // 停止下拉刷新
  }, 100)
})

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom(() => {
  featureIndexRef.value?.getNextPage()
})

/**
 * 获取店铺信息
 */
const getShopInfo = () => {
  const params = {
    url: '/tmerclub_admin/ua/shop_detail/head_info',
    method: 'GET',
    data: {
      shopId: shopId.value,
      imgType: 0 // 图片类型：0.app(h5) 1.pc
    }
  }
  http.request(params).then((res) => {
    if (!res.renovationId) {
      uni.redirectTo({
        url: '/package-shop/shop-page/shop-index/shop-index?shopId=' + shopId.value
      })
    }
    uni.setStorageSync('cloudShopInfo', res)
    if (res.renovationId !== renovationId.value && res.renovationId) {
      renovationId.value = res.renovationId
    }
    // 进入页面时判断店铺状态
    handleShopStatus(res)
  })
}

/**
 * 店铺状态处理
 */
const handleShopStatus = (res) => {
  const shopStatus = res.shopStatus
  // shopStatus店铺状态(-1:未开通 0: 停业中 1:营业中 2:平台下线 3:平台下线待审核)，可修改
  if (shopStatus === -1) {
    handleTipsModal($t('storeStatusTips2'))
    return
  }
  if (shopStatus === 0) {
    handleTipsModal($t('storeReviewTips6'))
    return
  }
  if (shopStatus === 2 || shopStatus === 3) {
    handleTipsModal($t('storeStatusTips3'))
  }
}

const handleTipsModal = (tips) => {
  uni.showModal({
    title: $t('tips'),
    content: tips,
    showCancel: false, // 是否显示取消按钮
    cancelText: $t('cancel'),
    confirmText: $t('confirm'),
    success: (res) => {
      if (res.confirm) {
        uni.navigateBack({
          delta: 1
        })
      }
    }
  })
}

// 页面加载回调
const pageLoaded = (e) => {
  uni.setNavigationBarTitle({
    title: e.detail.title
  })
}

</script>
