<template>
  <div>
    <n-space justify="space-between" align="flex-start" style="margin-bottom: 20px;">
      <div>
        <div style="font-size: 22px; font-weight: 600; color: #111827; margin-bottom: 4px;">旅行日历</div>
        <div style="font-size: 13px; color: #9ca3af;">管理12个月份的出行建议，对应官网「旅行日历」页面</div>
      </div>
      <n-space>
        <a href="/calendar" target="_blank" style="text-decoration: none;">
          <n-button>查看前台 ↗</n-button>
        </a>
        <n-button type="primary" :loading="saving" :disabled="saving" @click="save">
          {{ saving ? '保存中...' : '保存' }}
        </n-button>
      </n-space>
    </n-space>

    <n-spin v-if="!loaded" style="display:block; padding: 60px; text-align: center;" />
    <template v-else>
      <n-card title="页面概述" style="margin-bottom: 20px;">
        <n-form-item label="页面标题">
          <n-input v-model:value="data.meta.pageTitle" />
        </n-form-item>
        <n-form-item label="最佳时间">
          <n-input v-model:value="data.meta.bestTime" />
        </n-form-item>
        <n-form-item label="页面介绍">
          <n-input v-model:value="data.meta.pageIntro" type="textarea" :rows="3" />
        </n-form-item>
      </n-card>

      <n-grid :cols="3" :x-gap="16" :y-gap="16" responsive="screen" :collapsed-rows="999">
        <n-gi v-for="m in data.months" :key="m.month">
          <n-card
            :style="m.isPeakSeason ? 'border: 2px solid #3a7d44;' : ''"
          >
            <template #header>
              <n-space justify="space-between" align="center">
                <span style="font-size: 15px; font-weight: 600;">{{ m.month }}月 · {{ m.name }}</span>
                <n-tag size="small">{{ m.season }}</n-tag>
              </n-space>
            </template>
            <n-form-item label="温度范围" :show-feedback="false" style="margin-bottom: 8px;">
              <n-input v-model:value="m.tempRange" size="small" />
            </n-form-item>
            <n-form-item label="天气特征" :show-feedback="false" style="margin-bottom: 8px;">
              <n-input v-model:value="m.weatherFeature" size="small" />
            </n-form-item>
            <n-form-item label="推荐指数 (1-5)" :show-feedback="false" style="margin-bottom: 8px;">
              <n-input-number v-model:value="m.ratingStars" :min="1" :max="5" size="small" style="width: 100%;" />
            </n-form-item>
            <n-form-item label="Tagline" :show-feedback="false" style="margin-bottom: 8px;">
              <n-input v-model:value="m.tagline" size="small" />
            </n-form-item>
            <n-form-item label="穿衣建议" :show-feedback="false" style="margin-bottom: 8px;">
              <n-input v-model:value="m.clothingTips" type="textarea" :rows="2" size="small" />
            </n-form-item>
            <n-form-item label="亮点（每行一个）" :show-feedback="false" style="margin-bottom: 8px;">
              <n-input
                type="textarea"
                :rows="3"
                size="small"
                :value="(m.highlights||[]).join('\n')"
                @input="m.highlights = $event.split('\n').filter(Boolean)"
              />
            </n-form-item>
            <n-space align="center" style="margin-top: 4px;">
              <n-tag
                :type="m.isPeakSeason ? 'success' : 'default'"
                checkable
                :checked="m.isPeakSeason"
                @update:checked="m.isPeakSeason = $event"
                size="small"
              >
                旺季
              </n-tag>
            </n-space>
          </n-card>
        </n-gi>
      </n-grid>
    </template>
  </div>
</template>

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

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

async function load() { const res = await adminFetch('/api/admin/site-content?key=calendar'); data.value = res.data; loaded.value = true }
async function save() {
  saving.value = true
  try {
    await adminFetch('/api/admin/site-content', { method: 'PUT', body: { key: 'calendar', data: data.value } })
    message.success('已保存')
  } catch { message.error('保存失败') } finally { saving.value = false }
}
onMounted(load)
</script>
