<template>
  <CustomNav title="首页" leftType="scan" @scan="handleScan" />
  <view class="content">
    <!-- 原始页面已移至此文件，保留以备参考 -->
    <swiper class="banner-swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :circular="true">
      <swiper-item v-for="(img, idx) in bannerImages" :key="idx">
        <image :src="img" class="banner-img" mode="aspectFill" />
      </swiper-item>
    </swiper>
    <view class="card-list">
      <view class="card" v-for="(card, idx) in cards" :key="idx">
        <view class="card-title">{{ card.title }}</view>
        <view class="card-desc">{{ card.desc }}</view>
      </view>
    </view>
  </view>
  <TabBar />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import CustomNav from '@/components/custom-nav.vue'
import TabBar from '@/components/tab-bar.vue'
import { isLoggedIn as checkLogin, getRole } from '@/composables/useAuth'

const title = ref('Hello')

const bannerImages = [
  '/static/carousel/BHFIIABBCDJII-5kCEkD6zh9.png',
  '/static/carousel/BHFIIABBDGHEA-wtWLrLS75o.png',
  '/static/carousel/BHFIIABBHJBAH-yDeckRiiQP.png'
]

const cards = [
  { title: '健康档案', desc: '管理您的健康信息' },
  { title: '慢病管理', desc: '查看慢病相关数据' },
  { title: '健康咨询', desc: '在线咨询医生' },
  { title: '用药提醒', desc: '设置用药提醒' }
]

onShow(() => {
  try {
    const logged = checkLogin()
    if (!logged) {
      return
    }

    const r = getRole()
    if (r === 3) {
      console.log('[index] redirecting to patient index, role=', r)
      uni.reLaunch({ url: '/pages/patient/index/index' })
      return
    }
  } catch (err) {
    console.error('检查登录态时出错:', err)
  }
})

function handleScan(res: any) {
  console.log('[index] scan result', res)
  const resultText = res?.result || ''
  if (resultText) {
    uni.showToast({ title: String(resultText), icon: 'none', duration: 2000 })
  } else {
    uni.showToast({ title: '未识别到有效内容', icon: 'none' })
  }
}
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: calc(var(--status-bar-height) + 44px);
  height: calc(100vh - var(--status-bar-height) - 44px);
  background: #f7f8fa;
}

.banner-swiper {
  width: 670rpx;
  max-width: 100vw;
  height: 400rpx;
  margin: 20rpx auto 30rpx auto;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.08);
  background: #fff;
}

.banner-img {
  width: 100%;
  height: 100%;
  display: block;
}

.card-list {
  width: 90%;
  display: flex;
  flex-direction: column;
  gap: 32rpx;
  padding: 0 40rpx;
  margin: 0 auto;
}

.card {
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  padding: 40rpx 32rpx;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.card-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 16rpx;
}

.card-desc {
  font-size: 28rpx;
  color: #888;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}
</style>
