<script setup lang="ts">
import { InfoFilled } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

import { fetchPostRentalNotice, type PostRentalNotice } from '@/features/orders/api/orders'
import { useSessionStore } from '@/stores/session'

const session = useSessionStore()
const router = useRouter()
const loading = ref(false)
const notice = ref<PostRentalNotice | null>(null)

onMounted(async () => {
  if (!session.isLoggedIn) {
    router.replace({ path: '/login', query: { redirect: '/post-rental-notice' } })
    return
  }
  await loadNotice()
})

async function loadNotice() {
  loading.value = true
  try {
    notice.value = await fetchPostRentalNotice()
  } catch (error) {
    console.error('Failed to load post rental notice:', error)
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="page-container">
    <div class="page-card">
      <header class="page-header">
        <div class="page-header-content">
          <el-icon class="page-icon" :size="32"><InfoFilled /></el-icon>
          <div>
            <p class="page-eyebrow">Post Rental Notice</p>
            <h1 class="page-title">{{ notice?.title || '租后须知' }}</h1>
            <p class="page-subtitle">租赁账号后的重要须知事项，请仔细阅读</p>
          </div>
        </div>
      </header>

      <div v-loading="loading" class="notice-body">
        <div v-if="notice" class="notice-content">
          {{ notice.content }}
        </div>
        <el-empty v-else-if="!loading" description="暂无租后须知内容" />
      </div>
    </div>
  </div>
</template>

<style scoped>
.page-container {
  max-width: 900px;
  margin: 0 auto;
}

.page-card {
  background: #fff;
  border-radius: 16px;
  border: 1px solid #eef1f5;
  box-shadow: 0 4px 16px rgba(23, 35, 61, 0.04);
  overflow: hidden;
}

.page-header {
  padding: 32px 32px 24px;
  background: linear-gradient(135deg, #fff9f5 0%, #fff 100%);
  border-bottom: 1px solid #f5f6f8;
}

.page-header-content {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.page-icon {
  color: #ff6a00;
  flex-shrink: 0;
}

.page-eyebrow {
  margin: 0 0 6px;
  color: #ff6a00;
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.page-title {
  margin: 0 0 8px;
  color: #17233d;
  font-size: 28px;
  font-weight: 900;
  line-height: 1.2;
}

.page-subtitle {
  margin: 0;
  color: #64748b;
  font-size: 14px;
  line-height: 1.5;
}

.notice-body {
  padding: 32px;
  min-height: 400px;
}

.notice-content {
  color: #334155;
  font-size: 15px;
  line-height: 1.8;
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style>
