<template>
  <el-card>
    <template #header>
      <div class='flex justify-between items-center'>
        <span class='font-700'>堡垒机账号管理</span>
        <el-button v-if='hasPermission("platform.accounts.manage")' type='primary' @click='openCreate'>新增账号</el-button>
      </div>
    </template>

    <el-table :data='rows' v-loading='loading'>
      <el-table-column prop='id' label='ID' width='70' sortable />
      <el-table-column prop='name' label='名称' min-width='140' sortable />
      <el-table-column prop='username' label='用户名' min-width='140' sortable />
      <el-table-column label='USM-AUTHENTICATION' min-width='260' prop='usm_authentication' sortable>
        <template #default='{ row }'><el-input :model-value='row.usm_authentication || ""' readonly><template #append><el-button @click='copyText(row.usm_authentication)'>复制</el-button></template></el-input></template>
      </el-table-column>
      <el-table-column label='USM' min-width='260' prop='usm' sortable>
        <template #default='{ row }'><el-input :model-value='row.usm || ""' readonly><template #append><el-button @click='copyText(row.usm)'>复制</el-button></template></el-input></template>
      </el-table-column>
      <el-table-column prop='last_token_refreshed_at' label='刷新时间' min-width='170' sortable>
        <template #default='{ row }'>{{ formatDateTime(row.last_token_refreshed_at) }}</template>
      </el-table-column>
      <el-table-column label='操作' width='230'>
        <template #default='{ row }'>
          <div class='btn-gap-8 btn-gap-8--nowrap'>
            <el-button size='small' @click='openEdit(row)'>编辑</el-button>
            <el-button size='small' type='warning' :loading='!!refreshingMap[row.id]' :disabled='!!refreshingMap[row.id]' @click='refreshToken(row)'>刷新Token</el-button>
            <el-button size='small' type='danger' @click='removeRow(row)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model='dialogVisible' :title='editingId ? "编辑账号" : "新增账号"' width='560px'>
      <el-form :model='form' label-width='100px'>
        <el-form-item label='名称'><el-input v-model='form.name' /></el-form-item>
        <el-form-item label='用户名'><el-input v-model='form.username' /></el-form-item>
        <el-form-item label='密码'><el-input v-model='form.password' type='password' show-password /></el-form-item>
        <el-form-item label='启用'><el-switch v-model='form.is_active' /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click='dialogVisible = false'>取消</el-button>
        <el-button type='primary' :loading='saving' @click='submit'>提交</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang='ts'>
import { ElMessage, ElMessageBox } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { accountsApi } from '@/api/accounts'
import { formatDateTime } from '@/composables/datetime'
import { useAuthStore } from '@/stores/auth'

const authStore = useAuthStore()
const { hasPermission } = authStore
const loading = ref(false)
const saving = ref(false)
const dialogVisible = ref(false)
const editingId = ref<number | null>(null)
const rows = ref<any[]>([])
const refreshingMap = ref<Record<number, boolean>>({})
const form = reactive<any>({ name: '', username: '', password: '', is_active: true })
const refreshPollAttempts = 90
const refreshPollIntervalMs = 1000

async function fetchList(): Promise<void> {
  loading.value = true
  try {
    const response: any = await accountsApi.list({ page: 1, per_page: 200 })
    rows.value = response.data.data || []
  } finally {
    loading.value = false
  }
}

function openCreate(): void {
  editingId.value = null
  Object.assign(form, { name: '', username: '', password: '', is_active: true })
  dialogVisible.value = true
}

function openEdit(row: any): void {
  editingId.value = row.id
  Object.assign(form, { name: row.name, username: row.username, password: '', is_active: row.is_active })
  dialogVisible.value = true
}

async function submit(): Promise<void> {
  saving.value = true
  try {
    const payload: any = { ...form }
    if (!payload.password) {
      delete payload.password
    }
    if (editingId.value) {
      await accountsApi.update(editingId.value, payload)
      ElMessage.success('更新成功')
    } else {
      await accountsApi.create(payload)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    await fetchList()
  } finally {
    saving.value = false
  }
}

async function refreshToken(row: any): Promise<void> {
  if (refreshingMap.value[row.id]) {
    return
  }

  refreshingMap.value[row.id] = true
  ElMessage.info('Token 刷新任务已提交，正在同步状态...')

  try {
    const submitResponse: any = await accountsApi.refreshToken(row.id)
    const taskId = submitResponse?.data?.task_id

    if (!taskId) {
      throw new Error('任务提交成功但未返回任务ID')
    }

    await waitTokenRefreshFinished(row.id, taskId)
    ElMessage.success('Token 刷新成功')
    await fetchList()
  } catch (error: any) {
    if (!error?.message) {
      ElMessage.error(resolveErrorMessage(error))
    }
  } finally {
    refreshingMap.value[row.id] = false
  }
}

async function removeRow(row: any): Promise<void> {
  await ElMessageBox.confirm(`确认删除账号 ${row.name} 吗？`, '提示', { type: 'warning' })
  await accountsApi.remove(row.id)
  ElMessage.success('删除成功')
  await fetchList()
}

async function copyText(text?: string): Promise<void> {
  if (!text) {
    ElMessage.warning('暂无可复制内容')
    return
  }
  await navigator.clipboard.writeText(text)
  ElMessage.success('已复制')
}

function resolveErrorMessage(error: any): string {
  const message = error?.message

  if (typeof message === 'string' && message.trim() !== '') {
    return message
  }

  return 'Token 刷新失败，请稍后重试'
}

async function waitTokenRefreshFinished(accountId: number, taskId: string): Promise<void> {
  for (let attempt = 1; attempt <= refreshPollAttempts; attempt += 1) {
    const response: any = await accountsApi.refreshTokenStatus(accountId, taskId)
    const status = response?.data?.status

    if (status === 'success') {
      return
    }

    await delay(refreshPollIntervalMs)
  }

  throw new Error('等待 Token 刷新结果超时，请稍后重试')
}

function delay(ms: number): Promise<void> {
  return new Promise((resolve) => {
    setTimeout(resolve, ms)
  })
}

onMounted(fetchList)
</script>
