<template>
  <scroll-view scroll-y class="page-scroll">
    <view :class="['settings-page', elderClass]">
      <text class="page-title">设置</text>

      <view class="settings-group">
        <view class="settings-item" @tap="handleClearCache">
          <view class="settings-icon">
            <text class="settings-icon-text">缓</text>
          </view>
          <text class="settings-label">清除缓存</text>
          <text class="settings-arrow">></text>
        </view>
        <view class="settings-item" @tap="handleAbout">
          <view class="settings-icon">
            <text class="settings-icon-text">关</text>
          </view>
          <text class="settings-label">关于我们</text>
          <text class="settings-arrow">></text>
        </view>
        <view class="settings-item" @tap="handlePrivacy">
          <view class="settings-icon">
            <text class="settings-icon-text">隐</text>
          </view>
          <text class="settings-label">隐私政策</text>
          <text class="settings-arrow">></text>
        </view>
      </view>

      <view class="settings-group">
        <view class="settings-item logout-item" @tap="handleLogout">
          <text class="settings-label logout-label">退出登录</text>
        </view>
      </view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import { useAuthStore } from '@/stores/auth'
import { clearRequestCache } from '@/services/request'
import { useElderClass } from '@/composables/useElderClass'

const authStore = useAuthStore()
const { elderClass } = useElderClass()

function handleClearCache() {
  uni.showModal({
    title: '清除缓存',
    content: '确定要清除本地缓存数据吗？不会影响账号信息。',
    success: (res: any) => {
      if (res.confirm) {
        const preservedKeys = [
          'access_token', 'refresh_token', 'user_data', 'user_roles',
          'tenant_id', 'wechat_openid', 'current_patient', 'current_patient_id',
        ]
        const preservedData: Record<string, unknown> = {}
        for (const key of preservedKeys) {
          const val = uni.getStorageSync(key)
          if (val) preservedData[key] = val
        }

        try { uni.clearStorageSync() } catch { /* ignore */ }

        for (const [key, val] of Object.entries(preservedData)) {
          uni.setStorageSync(key, val)
        }

        clearRequestCache()
        uni.showToast({ title: '缓存已清除', icon: 'success' })
      }
    },
  })
}

function handleAbout() {
  uni.showModal({
    title: '关于我们',
    content: 'HMS 健康管理平台 v1.0.0\n为您的健康保驾护航',
    showCancel: false,
  })
}

function handlePrivacy() {
  uni.navigateTo({ url: '/pages/legal/privacy-policy' })
}

function handleLogout() {
  uni.showModal({
    title: '退出登录',
    content: '确定要退出登录吗？',
    success: (res: any) => {
      if (res.confirm) {
        authStore.logout()
      }
    },
  })
}
</script>

<style lang="scss" scoped>
.page-scroll {
  height: 100vh;
  background: $bg;
}

.settings-page {
  padding: 32px 24px;
}

.page-title {
  @include section-title;
  padding-left: 4px;
}

.settings-group {
  background: $card;
  border-radius: $r;
  overflow: hidden;
  margin-bottom: 24px;
  box-shadow: $shadow-sm;
}

.settings-item {
  display: flex;
  align-items: center;
  padding: 28px 24px;
  border-bottom: 1px solid $bd-l;

  &:last-child {
    border-bottom: none;
  }

  &.logout-item {
    justify-content: center;
  }
}

.settings-icon {
  @include flex-center;
  width: 48px;
  height: 48px;
  border-radius: $r-sm;
  background: $pri-l;
  margin-right: 16px;
  flex-shrink: 0;
}

.settings-icon-text {
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: var(--tk-font-h2);
  font-weight: bold;
  color: $pri-d;
}

.settings-label {
  flex: 1;
  font-size: var(--tk-font-num);
  color: $tx;
}

.logout-label {
  color: $dan;
  font-weight: bold;
}

.settings-arrow {
  font-size: var(--tk-font-h2);
  color: $tx3;
  font-family: 'Georgia', 'Times New Roman', serif;
  flex-shrink: 0;
}
</style>
