<template>
  <div style="max-width: 1100px">
    <n-space justify="space-between" align="center" style="margin-bottom: 20px">
      <div>
        <h1 style="margin: 0 0 4px; font-size: 20px; font-weight: 600; color: #111827">SEO 配置</h1>
        <p style="margin: 0; font-size: 13px; color: #9ca3af">管理各页面的 SEO 标题、描述、关键词</p>
      </div>
      <n-button type="primary" :loading="saving" @click="save">保存</n-button>
    </n-space>

    <n-spin :show="!loaded">
      <n-space vertical :size="16">
        <n-card v-for="(seo, pageKey) in data.pages" :key="pageKey">
          <template #header>
            <n-space align="center" :size="8">
              <span>{{ pageLabels[pageKey] || pageKey }}</span>
              <n-tag size="small" :bordered="false" style="font-size: 11px; color: #aaa; background: #f0f0f0">{{ pageKey }}</n-tag>
            </n-space>
          </template>
          <n-form label-placement="top">
            <n-form-item label="Title">
              <n-input v-model:value="seo.title" />
            </n-form-item>
            <n-form-item label="H1">
              <n-input v-model:value="seo.h1" />
            </n-form-item>
            <n-form-item label="Description">
              <n-input v-model:value="seo.description" type="textarea" :rows="2" />
            </n-form-item>
            <n-form-item label="Keywords">
              <n-input v-model:value="seo.keywords" placeholder="逗号分隔" />
            </n-form-item>
            <n-form-item label="OG Image">
              <AdminImageUpload v-model="seo.ogImage" />
            </n-form-item>
          </n-form>
        </n-card>
      </n-space>
    </n-spin>
  </div>
</template>

<script setup>
import { NButton, NCard, NSpace, NInput, NForm, NFormItem, NSpin, NTag } from 'naive-ui'
import { useMessage } from 'naive-ui'

definePageMeta({ layout: 'admin', middleware: 'admin' })
const { adminFetch } = useAdmin()
const message = useMessage()
const data = ref({ pages: {} })
const loaded = ref(false)
const saving = ref(false)

const pageLabels = {
  home: '首页', products: '产品页', 'summer-camp': '夏令营', 'winter-camp': '冬令营',
  destinations: '目的地', guides: '出行指南', calendar: '旅行日历', faq: '常见问答',
  reviews: '客户评价', gallery: '旅拍', about: '关于我们', contact: '联系我们',
  news: '最新动态', qualifications: '资质荣誉', blog: '博客', pricing: '价格',
  customize: '定制', selector: '产品选择器', stories: '客户故事', xiaohongshu: '小红书',
}

async function load() {
  const res = await adminFetch('/api/admin/site-content?key=seo')
  data.value = res.data
  loaded.value = true
}

async function save() {
  saving.value = true
  try {
    await adminFetch('/api/admin/site-content', { method: 'PUT', body: { key: 'seo', data: data.value } })
    message.success('已保存')
  } catch (e) {
    message.error('保存失败')
  } finally {
    saving.value = false
  }
}

onMounted(load)
</script>
