<script setup lang="ts">
import type {
  AdminCloudtentaclesPersistedSession,
  AdminCloudtentaclesSessionsMap,
  AdminCloudtentaclesSourceItem,
} from '@/types/admin'
import { formatAdminDateTime } from '@/utils/admin-time'

type Props = {
  cloudtentaclesFilePath: string
  cloudtentaclesSessionFilePath: string
  cloudtentaclesPersistedSession: AdminCloudtentaclesPersistedSession | null
  sources: AdminCloudtentaclesSourceItem[]
  sessions: AdminCloudtentaclesSessionsMap
  selectedSourceKey: string
}

defineProps<Props>()
</script>

<template>
  <el-card class="section-card">
    <template #header>
      <span class="card-title">配置信息</span>
    </template>
    <el-descriptions :column="1" border size="small">
      <el-descriptions-item label="配置文件">
        <code>{{ cloudtentaclesFilePath || '-' }}</code>
      </el-descriptions-item>
      <el-descriptions-item label="会话文件">
        <code>{{ cloudtentaclesSessionFilePath || '-' }}</code>
      </el-descriptions-item>
      <el-descriptions-item label="账号数量">
        {{ sources.length }} 个
      </el-descriptions-item>
      <el-descriptions-item label="当前选中账号">
        {{ selectedSourceKey }}
      </el-descriptions-item>
      <el-descriptions-item label="职责">
        管理 cloudtentacles 外部履约平台的账号、短信验证码登录与 token
        探活。该区域不处理订单来源同步。
      </el-descriptions-item>
      <el-descriptions-item label="当前账号已保存 token">
        {{
          cloudtentaclesPersistedSession?.hasToken
            ? `${cloudtentaclesPersistedSession.tokenMasked} / ${formatAdminDateTime(cloudtentaclesPersistedSession.loggedInAt)}`
            : '未保存'
        }}
      </el-descriptions-item>
      <el-descriptions-item v-if="Object.keys(sessions).length > 0" label="各账号会话状态">
        <div class="session-list">
          <div v-for="(session, key) in sessions" :key="key" class="session-item">
            <el-tag :type="session.hasToken ? 'success' : 'info'" size="small">{{ key }}</el-tag>
            <span>{{ session.hasToken ? session.tokenMasked : '未登录' }}</span>
          </div>
        </div>
      </el-descriptions-item>
    </el-descriptions>
  </el-card>
</template>

<style scoped>
.section-card {
  border-radius: var(--radius-lg);
}

.card-title {
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--text-primary);
}

.session-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.session-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
</style>
