<template>
  <div class="container">
    <Breadcrumb :items="['menu.ops.systemSettings', 'menu.ops.systemSettings.licenseCenter']" />

    <a-card class="license-page-card" :bordered="false">
      <template #title>
        <div class="card-head-title">
          <div class="page-title">许可证信息</div>
        </div>
      </template>
      <template #extra>
        <a-button type="outline" :loading="loading" :disabled="loading" @click="loadLicense">
          <template #icon><icon-refresh /></template>
          刷新
        </a-button>
      </template>

      <a-spin :loading="loading" class="page-spin">
        <a-empty v-if="!loading && loadError && !license" description="加载失败，请稍后重试" />

        <template v-else-if="license">
          <a-card class="section-card" :bordered="false">
            <template #title>
              <span class="section-title">基本信息</span>
            </template>
            <div class="kv-list">
              <div v-for="row in basicRows" :key="row.key" class="basic-row">
                <span class="label">{{ row.label }}</span>
                <span class="value" :class="{ 'value-code': row.key === 'id' }">{{ row.display }}</span>
              </div>
            </div>
          </a-card>

          <a-card class="section-card section-card--quota" :bordered="false">
            <template #title>
              <span class="section-title">资源限制</span>
            </template>
            <p class="quota-hint">配额为 0 表示该资源未授权，不能新增。</p>
            <div class="kv-list">
              <div v-for="row in quotaRows" :key="row.key" class="quota-row">
                <span class="label">{{ row.label }}</span>
                <span class="value">{{ row.display }}</span>
              </div>
            </div>
          </a-card>
        </template>
      </a-spin>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import { Message } from '@arco-design/web-vue'
import { IconRefresh } from '@arco-design/web-vue/es/icon'
import { fetchLicenseInfo, type LicenceInfo } from '@/api/ops/dcControl'

const loading = ref(false)
const loadError = ref(false)
const license = ref<LicenceInfo | null>(null)

const formatQuota = (value: number) => (value === 0 ? '未授权' : String(value))

const basicRows = computed(() => {
  const current = license.value
  if (!current) return []
  return [
    { key: 'id', label: '许可证 ID', display: current.id },
    { key: 'platform_name', label: '平台名称', display: current.platform_name },
    { key: 'workspace', label: 'Workspace', display: current.workspace },
    { key: 'issued_on', label: '签发日期', display: current.issued_on },
    { key: 'valid_from', label: '生效日期', display: current.valid_from },
    { key: 'expires_on', label: '到期日期', display: current.expires_on },
  ]
})

const quotaRows = computed(() => {
  const quotas = license.value?.quotas
  if (!quotas) return []
  return [
    { key: 'max_database', label: '数据库', display: formatQuota(quotas.max_database) },
    { key: 'max_middleware', label: '中间件', display: formatQuota(quotas.max_middleware) },
    { key: 'max_network_device', label: '网络设备', display: formatQuota(quotas.max_network_device) },
    { key: 'max_security', label: '安全设备', display: formatQuota(quotas.max_security) },
    { key: 'max_storage', label: '存储设备', display: formatQuota(quotas.max_storage) },
    { key: 'max_pc', label: 'PC', display: formatQuota(quotas.max_pc) },
    { key: 'max_server', label: '服务器', display: formatQuota(quotas.max_server) },
    { key: 'max_user', label: '用户', display: formatQuota(quotas.max_user) },
    { key: 'max_role', label: '角色', display: formatQuota(quotas.max_role) },
    { key: 'max_permission', label: '权限', display: formatQuota(quotas.max_permission) },
    { key: 'max_menu', label: '菜单', display: formatQuota(quotas.max_menu) },
  ]
})

async function loadLicense() {
  loading.value = true
  loadError.value = false
  try {
    const response = await fetchLicenseInfo()
    if (response.code !== 0) {
      throw new Error(response.message || '获取许可证失败')
    }
    license.value = response.details
  } catch (error) {
    loadError.value = true
    license.value = null
    console.error('[license-center] fetchLicenseInfo', error)
    Message.error(error instanceof Error ? error.message : '获取许可证失败')
  } finally {
    loading.value = false
  }
}

onMounted(loadLicense)
</script>

<script lang="ts">
export default {
  name: 'LicenseCenter',
}
</script>

<style scoped lang="less">
/* 与 system-settings / 报表等运维页一致 */
.container {
  padding: 0 20px 20px;
}

.license-page-card {
  :deep(.arco-card-body) {
    padding-top: 8px;
  }
}

.card-head-title {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  min-width: 0;
}

.page-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text-1);
  line-height: 1.4;
}

.page-subtitle {
  margin: 0;
  font-size: 13px;
  font-weight: normal;
  color: var(--color-text-2);
  line-height: 1.5;
  word-break: break-word;
}

.page-spin {
  display: block;
  width: 100%;
  min-height: 120px;
}

.section-card {
  margin-bottom: 16px;
  background: var(--color-fill-1);
  border-radius: 4px;
  overflow: hidden;

  &:last-child {
    margin-bottom: 0;
  }

  :deep(.arco-card-header) {
    border-bottom: 1px solid var(--color-border-2);
    padding-top: 12px;
    padding-bottom: 12px;
  }

  :deep(.arco-card-body) {
    padding: 0;
    background: var(--color-bg-2);
  }
}

.section-card--quota {
  :deep(.arco-card-body) {
    padding-top: 0;
  }
}

.section-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text-1);
}

.quota-hint {
  margin: 0;
  padding: 12px 20px 4px;
  font-size: 12px;
  color: var(--color-text-3);
  line-height: 1.5;
  background: var(--color-bg-2);
}

.kv-list {
  padding: 0;
  background: var(--color-bg-2);
}

.basic-row,
.quota-row {
  display: flex;
  align-items: flex-start;
  padding: 12px 20px;
  border-bottom: 1px solid var(--color-border-1);

  &:last-child {
    border-bottom: none;
  }

  .label {
    flex-shrink: 0;
    width: 100px;
    font-size: 14px;
    color: var(--color-text-2);
  }

  .value {
    flex: 1;
    min-width: 0;
    font-size: 14px;
    color: var(--color-text-1);
    text-align: left;
  }
}

.quota-row {
  align-items: center;

  .value {
    text-align: right;
    font-variant-numeric: tabular-nums;
  }
}

.value-code {
  word-break: break-all;
}

@media (max-width: 576px) {
  .container {
    padding: 0 12px 16px;
  }

  :deep(.arco-card-header) {
    flex-wrap: wrap;
    gap: 8px;
  }

  .basic-row {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;

    .label {
      width: auto;
    }
  }

  .quota-row .label {
    width: auto;
    min-width: 72px;
  }
}
</style>
