<script setup lang="ts">
import { StaticUrl } from '@/config'
import router from '@/router'

definePage({
  name: 'smqjh-home',
  layout: 'tabbar',
  islogin: false,
  style: {
    navigationBarTitleText: '首页',
    navigationStyle: 'custom',
    backgroundColor: '#9ED605',
    backgroundColorTop: '#9ED605',
  },
})
const { show } = useGlobalToast()

const addressStore = useAddressStore()
const { statusBarHeight, MenuButtonHeight, opcity, isOnlineAudit } = storeToRefs(useSysStore())
const { name } = storeToRefs(addressStore)
const { userInfo } = storeToRefs(useUserStore())
const xsbStore = ref<typeof import('@/subPack-xsb/store-xsb/sys')>()
const { data: goodsList, isLastPage, page, reload, error, refresh } = usePagination((pageNum, pageSize) =>
  Apis.xsb.getSearchProductList({ data: { pageNum, pageSize, salesNum: 'DESC', shopId: xsbStore.value?.useSysXsbStore().SelectShopInfo.shopId || 2, channelId: userInfo.value.channelId || 1 } }), {
  data: resp => resp.data?.list,
  initialData: [],
  initialPage: 1,
  initialPageSize: 10,
  append: true,
  immediate: false,
})
const state = computed(() => {
  return error.value ? 'error' : !isLastPage.value ? 'loading' : 'finished'
})
const loading = ref(true)

onShow(async () => {
  useSysStore().getAudit()
  useSmqjhCartStore().getCartList('XSB')

  xsbStore.value = await AsyncImport('@/subPack-xsb/store-xsb/sys')
  refresh()
})
onMounted(() => {
  setTimeout(() => {
    loading.value = false
  }, 1000)
})
onReachBottom(() => {
  if (!isLastPage.value) {
    page.value++
  }
})

const navList = computed(() => {
  const list = [
    { icon: `${StaticUrl}/xsb.png`, title: '星闪豹', name: 'xsb-homeTabbar', show: true },
    { icon: `${StaticUrl}/smqjh-index-cd.png`, title: '充电', name: '', show: !isOnlineAudit.value },
    { icon: `${StaticUrl}/smqjh-sp.png`, title: '电影演出', name: '', show: !isOnlineAudit.value },
    { icon: `${StaticUrl}/smqjh-vip.png`, title: '视频权益', name: '', show: !isOnlineAudit.value },
    { icon: `${StaticUrl}/smqjh-diancan.png`, title: '大牌点餐', name: '', show: !isOnlineAudit.value },
    { icon: `${StaticUrl}/smqjh-jiayou.png`, title: '加油', name: '', show: !isOnlineAudit.value },
    { icon: `${StaticUrl}/smqjh-jiudian.png`, title: '酒店民宿', name: '', show: !isOnlineAudit.value },
    { icon: `${StaticUrl}/smqjh-daijia.png`, title: '代驾', name: '', show: !isOnlineAudit.value },
  ]
  return list
})
onMounted(() => {
  addressStore.getLocation()
  opcity.value = 0
})

onPageScroll((e) => {
  const calculatedOpacity = e.scrollTop / 100
  opcity.value = Math.min(1, Math.max(0.1, calculatedOpacity))
})
function handleClick(name: string) {
  if (!name) {
    show({ msg: '敬请期待 !' })
    return
  }
  router.push({ name })
}
onShareAppMessage(() => {
  return {
    title: '市民请集合',
  }
})
function handleGo() {
  show({ msg: '敬请期待 !' })
}
</script>

<template>
  <view class="page-class box-border">
    <wd-navbar
      title="" :custom-style="`background-color: rgba(158,214,5,${opcity});`" :bordered="false" :z-index="99"
      safe-area-inset-top fixed
    >
      <template #left>
        <view class="relative z-10 h48rpx w-full w202rpx opacity-100">
          <image class="absolute left-0 top-0 h48rpx w202rpx" :src="`${StaticUrl}/logo.png`" />
        </view>
      </template>
    </wd-navbar>

    <view
      class="header-linear h320rpx px24rpx"
      :style="{ paddingTop: `${(Number(statusBarHeight) || 44) + MenuButtonHeight + 12}px` }"
    >
      <view class="box-border flex items-center">
        <view class="flex items-center" @click="useAddressStore().getMapAddress()">
          <image :src="`${StaticUrl}/location-black.png`" class="h33.8rpx min-w28.97rpx w28.97rpx" />
          <view class="ml18rpx max-w-180rpx truncate text-32rpx">
            {{ name }}
          </view>
        </view>
        <view class="ml16rpx h60rpx w502rpx flex items-center justify-between rounded-40rpx bg-white pr6rpx">
          <view class="flex items-center pb14rpx pl24rpx pt16rpx">
            <wd-icon name="search" size="14" color="#ccc" />
            <view class="ml12rpx text-24rpx text-gray">
              霸王茶姬
            </view>
          </view>
          <view
            class="h50rpx w96rpx flex items-center justify-center rounded-26rpx bg-[var(--them-color)] text-24rpx text-white font-semibold"
          >
            搜索
          </view>
        </view>
      </view>
    </view>
    <view class="px24rpx -mt260rpx">
      <view class="mt24rpx rounded-16rpx bg-white py24rpx" :class="[loading ? 'px24rpx' : '']">
        <wd-skeleton
          :loading="loading"
          theme="image" :row-col="[
            [
              { width: '120rpx', height: '120rpx' },
              { width: '120rpx', height: '120rpx' },
              { width: '120rpx', height: '120rpx' },
              { width: '120rpx', height: '120rpx' },
            ],
            [
              { width: '120rpx', height: '120rpx' },
              { width: '120rpx', height: '120rpx' },
              { width: '120rpx', height: '120rpx' },
              { width: '120rpx', height: '120rpx' },
            ],

          ]"
        >
          <view class="grid grid-cols-4 gap12rpx">
            <template v-for="item in navList" :key="item.icon">
              <view
                v-if="item.show" class="flex flex-col items-center justify-center"
                @click="handleClick(String(item.name))"
              >
                <view class="relative h120rpx w120rpx">
                  <image :src="item.icon" class="h120rpx w120rpx" />
                  <view
                    v-if="item.title != '星闪豹'"
                    class="linebg absolute left-0 top-0 h-full w-full flex items-center justify-center rounded-32rpx text-24rpx text-white font-semibold"
                  >
                    敬请期待
                  </view>
                </view>
                <view class="text-24rpx">
                  {{ item.title }}
                </view>
              </view>
            </template>
          </view>
        </wd-skeleton>
      </view>

      <view class="mt20rpx w-full flex items-center justify-between" @click="handleGo">
        <image :src="`${StaticUrl}/smqjh-fl.png`" class="h346rpx w344rpx flex-shrink-0" />
        <view class="flex flex-1 flex-col items-end justify-center">
          <image :src="`${StaticUrl}/smqjh-hot-vip.png`" class="h180rpx w344rpx" />
          <image :src="`${StaticUrl}/smqjh-cd.png`" class="mt14rpx h152rpx w344rpx" />
        </view>
      </view>
      <view class="mt20rpx">
        <wd-skeleton
          theme="image" animation="gradient" :loading="loading"
          :row-col="[[{ height: '568rpx', width: '344rpx' }, { height: '568rpx', width: '344rpx' }], [{ height: '568rpx', width: '344rpx' }, { height: '568rpx', width: '344rpx' }]]"
        >
          <view class="flex items-center">
            <scroll-view scroll-y type="custom">
              <grid-view type="masonry" cross-axis-count="2" main-axis-gap="10" cross-axis-gap="10">
                <view
                  v-for="item in goodsList" :key="item.id"
                  class="relative overflow-hidden rounded-16rpx bg-white pb16rpx"
                >
                  <view @click="router.push({ name: 'xsb-goods', params: { id: item.prodId } })">
                    <view class="relative h344rpx">
                      <image :src="item.pic" class="h344rpx w344rpx" />
                      <view class="absolute bottom-0 left-0 flex items-center text-22rpx">
                        <view class="rounded-tl-8rpx bg-#02ECFD pl4rpx">
                          星闪豹
                        </view>
                        <!-- <view class="rounded-tr-8rpx bg-[rgba(0,0,0,0.5)] pl8rpx pr4rpx text-white">
                      峰10:00-13:00
                    </view> -->
                      </view>
                    </view>
                    <view class="mt16rpx pl20rpx pr6rpx">
                      <view class="flex items-center">
                        <!-- <view class="rounded-8rpx bg-#FF4D3A px12rpx text-24rpx text-white">
                      补贴
                    </view> -->
                        <view class="line-clamp-2 text-28rpx font-semibold">
                          {{ item.prodName }}
                        </view>
                      </view>
                      <view class="mt20rpx flex items-end text-#FF4D3A">
                        <view class="text-24rpx">
                          ￥
                        </view>
                        <view class="text-36rpx line-height-[36rpx]">
                          {{ item.channelProdPrice }}
                        </view>
                        <view class="text-24rpx">
                          元
                        </view>
                      </view>
                    </view>
                  </view>
                  <view
                    v-if="!item.spuStock"
                    class="absolute left-0 top-0 z-1 h-full w-full flex items-center justify-center bg-[rgba(255,255,255,0.6)]"
                  >
                    <view
                      class="h156rpx w156rpx flex items-center justify-center rounded-full bg-[rgba(0,0,0,.6)] text-28rpx text-white"
                    >
                      商品已售罄
                    </view>
                  </view>
                </view>
              </grid-view>
            </scroll-view>
          </view>
        </wd-skeleton>
        <wd-loadmore :state="state" :loading-props="{ color: '#9ED605', size: 20 }" @reload="reload" />
      </view>
    </view>
  </view>
</template>

<style scoped lang="scss">
.page-class {
  :deep() {
    .wd-divider {
      margin: 0 !important;
    }
  }

  .flex-box-item {
    background: linear-gradient(180deg, #EBFFB4 0%, #FFFFFF 46%, #FFFFFF 100%);
  }

  .baoyou {
    background: url('https://zswl-shop.oss-cn-chengdu.aliyuncs.com/2025/11/771b8a09633448d8b62c0004a8928054.png') no-repeat;
    background-size: contain;
  }

  .linebg {
    background: rgba(0, 0, 0, .3);
  }

}
</style>
