<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;">管理出行准备指南各章节内容，对应官网「出行指南」页面</div>
      </div>
      <n-space>
        <a href="/guides" 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: 12px;">
        <n-input v-model:value="data.pageIntro" type="textarea" :rows="3" />
      </n-card>

      <n-card
        v-for="(s, i) in data.sections"
        :key="i"
        style="margin-bottom: 12px;"
      >
        <template #header>
          <div
            style="display: flex; justify-content: space-between; align-items: center; cursor: pointer; user-select: none;"
            @click="open[i] = !open[i]"
          >
            <span style="font-size: 15px; color: #333;">{{ s.icon || '' }} {{ s.title }}</span>
            <n-space align="center">
              <span style="font-size: 12px; color: #9ca3af;">{{ s.subtitle || '' }}</span>
              <span style="font-size: 12px; color: #999; transition: transform 0.2s; display: inline-block;" :style="open[i] ? 'transform: rotate(90deg)' : ''">&#9656;</span>
            </n-space>
          </div>
        </template>
        <div v-show="open[i]">
          <n-grid :cols="2" :x-gap="10" style="margin-bottom: 12px;">
            <n-gi>
              <n-form-item label="ID" :show-feedback="false">
                <n-input v-model:value="s.id" size="small" />
              </n-form-item>
            </n-gi>
            <n-gi>
              <n-form-item label="标题" :show-feedback="false">
                <n-input v-model:value="s.title" size="small" />
              </n-form-item>
            </n-gi>
            <n-gi>
              <n-form-item label="副标题" :show-feedback="false">
                <n-input v-model:value="s.subtitle" size="small" />
              </n-form-item>
            </n-gi>
            <n-gi>
              <n-form-item label="图标" :show-feedback="false">
                <n-input v-model:value="s.icon" size="small" />
              </n-form-item>
            </n-gi>
          </n-grid>
          <n-form-item label="正文内容" :show-feedback="false" style="margin-bottom: 10px;">
            <n-input v-model:value="s.content" type="textarea" :rows="4" />
          </n-form-item>
          <n-form-item v-if="s.tips" label="Tips（每行一个）" :show-feedback="false" style="margin-bottom: 10px;">
            <n-input
              type="textarea"
              :rows="3"
              :value="(s.tips||[]).join('\n')"
              @input="s.tips = $event.split('\n').filter(Boolean)"
            />
          </n-form-item>

          <div v-if="s.items && s.items.length" style="margin-top: 14px; border-top: 1px solid #f0f0f0; padding-top: 10px;">
            <div style="font-size: 14px; color: #555; margin-bottom: 10px; font-weight: 500;">子项列表</div>
            <n-space v-for="(item, ii) in s.items" :key="ii" align="center" style="margin-bottom: 6px;">
              <template v-if="typeof item === 'string'">
                <n-input
                  :value="item"
                  @input="s.items[ii] = $event"
                  size="small"
                  style="flex: 1; min-width: 200px;"
                />
              </template>
              <template v-else>
                <n-input v-model:value="item.text" placeholder="文字" size="small" style="flex: 1; min-width: 200px;" />
                <n-tag
                  :type="item.important ? 'warning' : 'default'"
                  checkable
                  :checked="item.important"
                  @update:checked="item.important = $event"
                  size="small"
                >
                  重要
                </n-tag>
              </template>
            </n-space>
          </div>

          <n-card v-if="s.months" title="每月穿搭" size="small" style="margin-top: 12px;">
            <n-collapse>
              <n-collapse-item v-for="(m, mi) in s.months" :key="mi" :name="mi">
                <template #header>{{ m.name }}</template>
                <n-form-item label="描述" :show-feedback="false" style="margin-bottom: 8px;">
                  <n-input v-model:value="m.desc" type="textarea" :rows="2" />
                </n-form-item>
                <n-grid :cols="2" :x-gap="12">
                  <n-gi>
                    <n-form-item label="日间温度" :show-feedback="false" style="margin-bottom: 8px;">
                      <n-input v-model:value="m.tempDay" />
                    </n-form-item>
                  </n-gi>
                  <n-gi>
                    <n-form-item label="夜间温度" :show-feedback="false" style="margin-bottom: 8px;">
                      <n-input v-model:value="m.tempNight" />
                    </n-form-item>
                  </n-gi>
                  <n-gi>
                    <n-form-item label="成人穿搭" :show-feedback="false" style="margin-bottom: 8px;">
                      <n-input v-model:value="m.adultWear" type="textarea" :rows="2" />
                    </n-form-item>
                  </n-gi>
                  <n-gi>
                    <n-form-item label="儿童穿搭" :show-feedback="false" style="margin-bottom: 8px;">
                      <n-input v-model:value="m.kidsWear" type="textarea" :rows="2" />
                    </n-form-item>
                  </n-gi>
                </n-grid>
                <n-form-item label="亮点" :show-feedback="false" style="margin-bottom: 8px;">
                  <n-input v-model:value="m.highlight" />
                </n-form-item>
                <n-form-item label="备注" :show-feedback="false" style="margin-bottom: 8px;">
                  <n-input v-model:value="m.note" />
                </n-form-item>
              </n-collapse-item>
            </n-collapse>
          </n-card>
        </div>
      </n-card>
    </template>
  </div>
</template>

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

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

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