<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">图片配置</h1>
        <p style="margin: 0; font-size: 13px; color: #9ca3af">管理网站各处使用的图片资源</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="(group, groupKey) in data" :key="groupKey" :title="groupLabels[groupKey] || groupKey">
          <n-grid :cols="3" :x-gap="16" :y-gap="16">
            <n-gi v-for="(val, imgKey) in group" :key="imgKey">
              <div>
                <div style="font-size: 12px; color: #6b7280; margin-bottom: 6px; font-weight: 500">{{ imgKey }}</div>
                <AdminImageUpload v-model="data[groupKey][imgKey]" />
              </div>
            </n-gi>
          </n-grid>
        </n-card>
      </n-space>
    </n-spin>
  </div>
</template>

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

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

const groupLabels = {
  logo: 'Logo', hero: '首页横幅', brandStory: '品牌故事',
  mascot: '吉祥物', team: '团队', xiaohongshu: '小红书', summerCamp: '夏令营',
}

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

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

onMounted(load)
</script>
