<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">管理品牌名称、slogan、信任数据等基础信息</p>
      </div>
      <n-space>
        <n-button tag="a" href="/" target="_blank" secondary>查看前台 ↗</n-button>
        <n-button type="primary" :loading="saving" @click="save">保存</n-button>
      </n-space>
    </n-space>

    <n-spin :show="!loaded">
      <n-space vertical :size="16">
        <n-card title="基础信息">
          <n-form label-placement="top">
            <n-grid :cols="2" :x-gap="12" :y-gap="4">
              <n-gi><n-form-item label="品牌名"><n-input v-model:value="data.name" /></n-form-item></n-gi>
              <n-gi><n-form-item label="全称"><n-input v-model:value="data.fullName" /></n-form-item></n-gi>
              <n-gi><n-form-item label="域名"><n-input v-model:value="data.domain" /></n-form-item></n-gi>
              <n-gi><n-form-item label="网址"><n-input v-model:value="data.url" /></n-form-item></n-gi>
              <n-gi><n-form-item label="情感 Slogan"><n-input v-model:value="data.slogan.emotional" /></n-form-item></n-gi>
              <n-gi><n-form-item label="功能 Slogan"><n-input v-model:value="data.slogan.functional" /></n-form-item></n-gi>
              <n-gi><n-form-item label="ICP 主体"><n-input v-model:value="data.icpEntity" /></n-form-item></n-gi>
              <n-gi><n-form-item label="ICP 备案号"><n-input v-model:value="data.icp" /></n-form-item></n-gi>
            </n-grid>
          </n-form>
        </n-card>

        <n-card title="信任数据">
          <template #header-extra>
            <n-button size="small" type="primary" @click="data.trustStats.push({ value: '', unit: '', label: '' })">添加</n-button>
          </template>
          <n-space vertical :size="8">
            <n-space v-for="(s, i) in data.trustStats" :key="i" align="center">
              <n-input v-model:value="s.value" placeholder="数值" style="width: 80px" />
              <n-input v-model:value="s.unit" placeholder="单位" style="width: 60px" />
              <n-input v-model:value="s.label" placeholder="标签" style="width: 160px" />
              <n-button size="tiny" type="error" quaternary @click="data.trustStats.splice(i, 1)">删</n-button>
            </n-space>
          </n-space>
        </n-card>

        <n-card title="差异化优势">
          <template #header-extra>
            <n-button size="small" type="primary" @click="data.differentiators.push({ title: '', description: '' })">添加</n-button>
          </template>
          <n-space vertical :size="8">
            <n-space v-for="(d, i) in data.differentiators" :key="i" align="center">
              <n-input v-model:value="d.title" placeholder="标题" style="width: 150px" />
              <n-input v-model:value="d.description" placeholder="描述" style="width: 400px" />
              <n-button size="tiny" type="error" quaternary @click="data.differentiators.splice(i, 1)">删</n-button>
            </n-space>
          </n-space>
        </n-card>
      </n-space>
    </n-spin>
  </div>
</template>

<script setup>
import { NButton, NCard, NSpace, NInput, NForm, NFormItem, 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({ slogan: {}, trustStats: [], differentiators: [] })
const loaded = ref(false)
const saving = ref(false)

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

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

onMounted(load)
</script>
