<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'

import { fetchAdminMgrUsers, deleteAdminMgrUser, changeAdminPassword, type AdminMgrUser } from '@/features/admin/api/adminMgr'
import { useAdminPaginatedTable } from '@/features/admin/composables/useAdminPaginatedTable'
import { formatDateTime } from '@/utils/time'

import AdminUserDialog from '../components/AdminUserDialog.vue'
import AssignRolesDialog from '../components/AssignRolesDialog.vue'

const showDialog = ref(false)
const editingAdmin = ref<AdminMgrUser | null>(null)
const showRolesDialog = ref(false)
const rolesAdmin = ref<AdminMgrUser | null>(null)
const showPasswordDialog = ref(false)
const passwordAdmin = ref<AdminMgrUser | null>(null)
const passwordForm = ref({ old_password: '', new_password: '' })
const passwordSubmitting = ref(false)

const { loading, data: admins, total, currentPage, currentPageSize, load: loadAdmins, handleSizeChange } = useAdminPaginatedTable<AdminMgrUser>({
  fetchFn: fetchAdminMgrUsers,
})

function openCreate() {
  editingAdmin.value = null
  showDialog.value = true
}

function openEdit(row: AdminMgrUser) {
  editingAdmin.value = row
  showDialog.value = true
}

function openRoles(row: AdminMgrUser) {
  rolesAdmin.value = row
  showRolesDialog.value = true
}

function openPassword(row: AdminMgrUser) {
  passwordAdmin.value = row
  passwordForm.value = { old_password: '', new_password: '' }
  showPasswordDialog.value = true
}

async function handleDelete(row: AdminMgrUser) {
  try {
    await ElMessageBox.confirm(`确定要删除管理员「${row.username}」吗？此操作不可撤销。`, '删除确认', {
      confirmButtonText: '确认删除',
      cancelButtonText: '取消',
      type: 'warning',
    })
    await deleteAdminMgrUser(row.id)
    ElMessage.success('管理员已删除')
    await loadAdmins()
  } catch {
    // 用户取消
  }
}

async function handleChangePassword() {
  if (!passwordAdmin.value) return
  if (!passwordForm.value.old_password || !passwordForm.value.new_password) {
    ElMessage.warning('请填写完整')
    return
  }
  passwordSubmitting.value = true
  try {
    await changeAdminPassword(passwordAdmin.value.id, passwordForm.value)
    ElMessage.success('密码已修改')
    showPasswordDialog.value = false
  } catch (error) {
    ElMessage.error(readError(error, '修改失败'))
  } finally {
    passwordSubmitting.value = false
  }
}

function readError(error: unknown, fallback: string) {
  if (typeof error === 'object' && error && 'response' in error) {
    const response = (error as { response?: { data?: { message?: string } } }).response
    return response?.data?.message || fallback
  }
  return fallback
}

const statusLabel: Record<string, string> = {
  active: '启用',
  disabled: '禁用',
}
</script>

<template>
  <section class="page">
    <div class="page-header-row">
      <div class="page-header">
        <p class="eyebrow">Admin Users</p>
        <h1>管理员管理</h1>
        <p>管理后台管理员账号，分配角色和权限。</p>
      </div>
      <div class="toolbar-actions">
        <el-button @click="loadAdmins">刷新</el-button>
        <el-button type="primary" @click="openCreate">新建管理员</el-button>
      </div>
    </div>

    <el-table v-loading="loading" class="table-panel" :data="admins">
      <el-table-column prop="id" label="ID" width="70" />
      <el-table-column prop="username" label="用户名" min-width="120" />
      <el-table-column prop="nickname" label="昵称" min-width="120" />
      <el-table-column label="角色" min-width="200">
        <template #default="{ row }">
          <el-tag
            v-for="role in row.roles"
            :key="role.id"
            size="small"
            style="margin-right: 4px; margin-bottom: 2px"
          >
            {{ role.name }}
          </el-tag>
          <span v-if="!row.roles?.length" style="color: #8f9bba">未分配</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="90">
        <template #default="{ row }">
          <el-tag :type="row.status === 'active' ? 'success' : 'danger'" size="small">
            {{ statusLabel[row.status] || row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最后登录" min-width="170">
        <template #default="{ row }">{{ row.last_login_at ? formatDateTime(row.last_login_at) : '-' }}</template>
      </el-table-column>
      <el-table-column label="操作" width="300" fixed="right">
        <template #default="{ row }">
          <el-button size="small" @click="openEdit(row)">编辑</el-button>
          <el-button size="small" type="primary" @click="openRoles(row)">角色</el-button>
          <el-button size="small" @click="openPassword(row)">密码</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-wrap" v-if="total > 0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="currentPageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @current-change="loadAdmins"
        @size-change="handleSizeChange"
      />
    </div>

    <!-- 新建/编辑对话框 -->
    <AdminUserDialog
      v-model="showDialog"
      :admin="editingAdmin"
      @saved="loadAdmins"
    />

    <!-- 角色分配对话框 -->
    <AssignRolesDialog
      v-model="showRolesDialog"
      :admin="rolesAdmin"
      @saved="loadAdmins"
    />

    <!-- 修改密码对话框 -->
    <el-dialog
      :model-value="showPasswordDialog"
      :title="`修改密码 - ${passwordAdmin?.username || ''}`"
      width="460px"
      @update:model-value="showPasswordDialog = $event"
    >
      <div class="dialog-body">
        <el-form-item label="原密码" class="full-control">
          <el-input v-model="passwordForm.old_password" type="password" show-password placeholder="请输入原密码" />
        </el-form-item>
        <el-form-item label="新密码" class="full-control">
          <el-input v-model="passwordForm.new_password" type="password" show-password placeholder="请输入新密码（至少6位）" />
        </el-form-item>
      </div>
      <template #footer>
        <el-button @click="showPasswordDialog = false">取消</el-button>
        <el-button type="primary" :loading="passwordSubmitting" @click="handleChangePassword">确认修改</el-button>
      </template>
    </el-dialog>
  </section>
</template>

<style scoped>
.toolbar-actions {
  display: flex;
  gap: 8px;
}
.dialog-body {
  display: grid;
  gap: 16px;
}
.full-control {
  width: 100%;
}
</style>
