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

import { fetchAdminUsers, freezeAdminUser, unfreezeAdminUser, type AdminUserItem } from '@/api/adminUsers'

const loading = ref(false)
const submitting = ref(false)
const users = ref<AdminUserItem[]>([])
const activeUser = ref<AdminUserItem | null>(null)
const freezeReason = ref('')

onMounted(loadUsers)

async function loadUsers() {
  loading.value = true
  try {
    users.value = await fetchAdminUsers()
  } finally {
    loading.value = false
  }
}

function openFreeze(row: AdminUserItem) {
  activeUser.value = row
  freezeReason.value = ''
}

async function handleFreeze() {
  if (!activeUser.value) return
  submitting.value = true
  try {
    await freezeAdminUser(activeUser.value.id, freezeReason.value)
    ElMessage.success('用户已冻结')
    activeUser.value = null
    await loadUsers()
  } catch (error) {
    ElMessage.error(readError(error, '冻结失败'))
  } finally {
    submitting.value = false
  }
}

async function handleUnfreeze(row: AdminUserItem) {
  submitting.value = true
  try {
    await unfreezeAdminUser(row.id)
    ElMessage.success('用户已解冻')
    await loadUsers()
  } catch (error) {
    ElMessage.error(readError(error, '解冻失败'))
  } finally {
    submitting.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
}
</script>

<template>
  <section class="page">
    <div class="page-header-row">
      <div class="page-header">
        <p class="eyebrow">Users</p>
        <h1>用户管理</h1>
        <p>查看用户实名、风险、信用和业务数量，处理冻结与解冻。</p>
      </div>
      <el-button @click="loadUsers">刷新</el-button>
    </div>

    <el-table v-loading="loading" class="table-panel" :data="users">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="phone" label="手机号" min-width="130" />
      <el-table-column prop="nickname" label="昵称" min-width="130" />
      <el-table-column prop="realname_status" label="实名" width="110" />
      <el-table-column prop="risk_status" label="风险" width="110" />
      <el-table-column prop="credit_score" label="信用分" width="100" />
      <el-table-column prop="status" label="状态" width="110" />
      <el-table-column prop="order_count" label="订单" width="90" />
      <el-table-column prop="listing_count" label="发布" width="90" />
      <el-table-column prop="dispute_count" label="申诉" width="90" />
      <el-table-column prop="last_login_at" label="最近登录" min-width="180" />
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button v-if="row.status === 'active'" size="small" type="danger" :loading="submitting" @click="openFreeze(row)">
            冻结
          </el-button>
          <el-button v-else size="small" type="primary" :loading="submitting" @click="handleUnfreeze(row)">解冻</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :model-value="!!activeUser" title="冻结用户" width="560px" @update:model-value="activeUser = null">
      <div v-if="activeUser" class="dialog-body">
        <p><strong>{{ activeUser.phone }}</strong> · {{ activeUser.nickname }}</p>
        <el-input v-model="freezeReason" type="textarea" :rows="4" placeholder="填写冻结原因，便于审计追踪" />
      </div>
      <template #footer>
        <el-button @click="activeUser = null">取消</el-button>
        <el-button type="danger" :loading="submitting" @click="handleFreeze">确认冻结</el-button>
      </template>
    </el-dialog>
  </section>
</template>
