<template>
  <div class="page-content">
    <div class="mb-5 flex items-center justify-between gap-4">
      <div>
        <h2 class="text-xl font-semibold">License 授权</h2>
        <p class="text-g-500 mt-1 text-sm">平台版本、账号数上限与授权有效期管控</p>
      </div>
      <ElButton type="primary" :loading="uploading" @click="openUpload">上传 License</ElButton>
    </div>

    <ElAlert
      v-if="!info.licensed"
      class="mb-4"
      type="warning"
      :closable="false"
      title="当前为社区版 Free(账号上限 3 个)。上传有效 License 后可启用专业版/旗舰版能力与更多账号。"
    />

    <ElDescriptions v-loading="loading" :column="2" border class="mb-4">
      <ElDescriptionsItem label="当前版本">{{ info.edition_name }}</ElDescriptionsItem>
      <ElDescriptionsItem label="版本标识">{{ info.edition }}</ElDescriptionsItem>
      <ElDescriptionsItem label="授权对象">{{ info.issued_to || '—' }}</ElDescriptionsItem>
      <ElDescriptionsItem label="账号数上限">
        {{ info.max_accounts === 0 ? '不限' : info.max_accounts + ' 个' }}
      </ElDescriptionsItem>
      <ElDescriptionsItem label="有效期至">{{ info.expires }}</ElDescriptionsItem>
      <ElDescriptionsItem label="License 文件">{{ info.file || '未配置' }}</ElDescriptionsItem>
      <ElDescriptionsItem label="授权特性" :span="2">
        {{ info.features?.length ? info.features.join('、') : '—' }}
      </ElDescriptionsItem>
    </ElDescriptions>

    <ElDialog v-model="dialogVisible" title="上传 License" width="560px">
      <ElInput
        v-model="content"
        type="textarea"
        :rows="10"
        placeholder="粘贴 License 文件完整内容(JSON,含 signature 字段)"
      />
      <template #footer>
        <ElButton @click="dialogVisible = false">取消</ElButton>
        <ElButton type="primary" :loading="uploading" @click="submit">保存并生效</ElButton>
      </template>
    </ElDialog>
  </div>
</template>

<script setup lang="ts">
  import { ElMessage, ElMessageBox } from 'element-plus'
  import request from '@/utils/http'

  interface LicenseInfo {
    edition: string
    edition_name: string
    issued_to?: string
    max_accounts: number
    features: string[]
    expires: string
    file: string
    licensed: boolean
  }

  const loading = ref(false)
  const uploading = ref(false)
  const dialogVisible = ref(false)
  const content = ref('')
  const info = ref<LicenseInfo>({
    edition: 'free',
    edition_name: '社区版 Free',
    max_accounts: 3,
    features: [],
    expires: '永久',
    file: '',
    licensed: false
  })

  async function load() {
    loading.value = true
    try {
      info.value = await request.get<LicenseInfo>({ url: '/api/v1/admin/license' })
    } finally {
      loading.value = false
    }
  }

  function openUpload() {
    content.value = ''
    dialogVisible.value = true
  }

  async function submit() {
    if (!content.value.trim()) {
      ElMessage.warning('请粘贴 License 内容')
      return
    }
    uploading.value = true
    try {
      const result = await request.post<LicenseInfo>({ url: '/api/v1/admin/license', params: { content: content.value } })
      info.value = result
      dialogVisible.value = false
      ElMessage.success('License 已生效')
      await load()
    } finally {
      uploading.value = false
    }
  }

  onMounted(load)
</script>
