<template>
  <s-layout
    title="我的"
    tabbar="/pages/index/user"
    navbar="custom"
    :bgStyle="template.style?.background"
    :navbarStyle="template.style?.navbar"
    onShareAppMessage
    :showFloatButton="true"
  >
    <s-block v-for="(item, index) in template.data" :key="index" :styles="item.style">
      <s-block-item :type="item.type" :data="item.data" :styles="item.style" />
    </s-block>
  </s-layout>
</template>

<script setup>
  import { computed, onMounted } from 'vue';
  import { onShow, onPageScroll, onPullDownRefresh } from '@dcloudio/uni-app';
  import sheep from '@/sheep';

  // 隐藏原生tabBar
  uni.hideTabBar({
    fail: () => {},
  });

  const template = computed(() => sheep.$store('app').template.user);
  const isLogin = computed(() => sheep.$store('user').isLogin);

  // ⭐ 页面首次加载时也更新用户数据
  onMounted(() => {
    console.log('[用户页面] onMounted - 检查登录状态:', isLogin.value);
    if (isLogin.value) {
      const userStore = sheep.$store('user');
      
      // 检查用户信息是否为空
      const isUserInfoEmpty = !userStore.userInfo || !userStore.userInfo.nickname;
      
      // 检查订单数据是否为默认值（待付款数量为0或'--'）
      const isNumDataEmpty = !userStore.numData || 
                             userStore.numData.coupons_num === '--' ||
                             !userStore.numData.order_num;
      
      console.log('[用户页面] 用户信息状态:', {
        nickname: userStore.userInfo?.nickname,
        coupons_num: userStore.numData?.coupons_num,
        unpaid: userStore.numData?.order_num?.unpaid
      });
      
      // 如果已登录但用户信息或订单数据为空，强制更新（绕过限流）
      if (isUserInfoEmpty || isNumDataEmpty) {
        console.log('[用户页面] 数据不完整，强制更新');
        console.log('[用户页面] 用户信息为空:', isUserInfoEmpty);
        console.log('[用户页面] 订单数据为空:', isNumDataEmpty);
        userStore.lastUpdateTime = 0; // 重置限流时间
        userStore.updateUserData();
      }
    }
  });

  onShow(() => {
    console.log('[用户页面] onShow - 更新用户数据');
    sheep.$store('user').updateUserData();
  });

  onPullDownRefresh(() => {
    console.log('[用户页面] onPullDownRefresh - 刷新用户数据');
    sheep.$store('user').updateUserData();
    setTimeout(function () {
      uni.stopPullDownRefresh();
    }, 800);
  });

  onPageScroll(() => {});
</script>

<style></style>
