<script setup>
import { inject, onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import { fetchPendingUsers, approveUser, rejectUser } from '../api/users'
import { approvePasswordReset, fetchPasswordResets, rejectPasswordReset } from '../api/passwordResets'
import { parseSqliteDateTime } from '../utils/datetime'

const refreshStats = inject('refreshStats', null)
const refreshNavBadges = inject('refreshNavBadges', null)

const pendingUsers = ref([])
const passwordResets = ref([])

const loadingPending = ref(false)
const loadingResets = ref(false)

function isVip(user) {
  const expire = user?.vip_expire_time
  if (!expire) return false
  if (String(expire).startsWith('2099-12-31')) return true
  const dt = parseSqliteDateTime(expire)
  return dt ? dt.getTime() > Date.now() : false
}

async function loadPending() {
  loadingPending.value = true
  try {
    pendingUsers.value = await fetchPendingUsers()
  } catch {
    pendingUsers.value = []
  } finally {
    loadingPending.value = false
  }
}

async function loadResets() {
  loadingResets.value = true
  try {
    passwordResets.value = await fetchPasswordResets()
  } catch {
    passwordResets.value = []
  } finally {
    loadingResets.value = false
  }
}

async function refreshAll() {
  await Promise.all([loadPending(), loadResets()])
  await refreshNavBadges?.({ pendingResets: passwordResets.value.length })
}

async function onApproveUser(row) {
  try {
    await ElMessageBox.confirm(`确定通过用户「${row.username}」的注册申请吗？`, '审核通过', {
      confirmButtonText: '通过',
      cancelButtonText: '取消',
      type: 'success',
    })
  } catch {
    return
  }

  try {
    await approveUser(row.id)
    ElMessage.success('用户审核通过')
    await refreshAll()
    await refreshStats?.()
  } catch {
    // handled by interceptor
  }
}

async function onRejectUser(row) {
  try {
    await ElMessageBox.confirm(`确定拒绝用户「${row.username}」的注册申请吗？`, '拒绝申请', {
      confirmButtonText: '拒绝',
      cancelButtonText: '取消',
      type: 'warning',
    })
  } catch {
    return
  }

  try {
    await rejectUser(row.id)
    ElMessage.success('已拒绝用户')
    await refreshAll()
    await refreshStats?.()
  } catch {
    // handled by interceptor
  }
}

async function onApproveReset(row) {
  try {
    await ElMessageBox.confirm(`确定批准「${row.username}」的密码重置申请吗？`, '批准重置', {
      confirmButtonText: '批准',
      cancelButtonText: '取消',
      type: 'success',
    })
  } catch {
    return
  }

  try {
    const res = await approvePasswordReset(row.id)
    ElMessage.success(res?.message || '密码重置申请已批准')
    await loadResets()
    await refreshNavBadges?.({ pendingResets: passwordResets.value.length })
  } catch {
    // handled by interceptor
  }
}

async function onRejectReset(row) {
  try {
    await ElMessageBox.confirm(`确定拒绝「${row.username}」的密码重置申请吗？`, '拒绝重置', {
      confirmButtonText: '拒绝',
      cancelButtonText: '取消',
      type: 'warning',
    })
  } catch {
    return
  }

  try {
    const res = await rejectPasswordReset(row.id)
    ElMessage.success(res?.message || '密码重置申请已拒绝')
    await loadResets()
    await refreshNavBadges?.({ pendingResets: passwordResets.value.length })
  } catch {
    // handled by interceptor
  }
}

onMounted(refreshAll)
</script>

<template>
  <div class="page-stack">
    <div class="app-page-title">
      <h2>待审核</h2>
      <div>
        <el-button @click="refreshAll">刷新</el-button>
      </div>
    </div>

    <el-card shadow="never" :body-style="{ padding: '16px' }" class="card">
      <h3 class="section-title">用户注册审核</h3>

      <div class="table-wrap">
        <el-table :data="pendingUsers" v-loading="loadingPending" style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column label="用户名" min-width="200">
            <template #default="{ row }">
              <div class="user-cell">
                <strong>{{ row.username }}</strong>
                <el-tag v-if="isVip(row)" type="warning" effect="light" size="small">VIP</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="email" label="邮箱" min-width="220">
            <template #default="{ row }">{{ row.email || '-' }}</template>
          </el-table-column>
          <el-table-column prop="created_at" label="注册时间" min-width="180" />
          <el-table-column label="操作" width="180" fixed="right">
            <template #default="{ row }">
              <el-button type="success" size="small" @click="onApproveUser(row)">通过</el-button>
              <el-button type="danger" size="small" @click="onRejectUser(row)">拒绝</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <el-card shadow="never" :body-style="{ padding: '16px' }" class="card">
      <h3 class="section-title">密码重置审核</h3>

      <div class="table-wrap">
        <el-table :data="passwordResets" v-loading="loadingResets" style="width: 100%">
          <el-table-column prop="id" label="申请ID" width="90" />
          <el-table-column prop="username" label="用户名" min-width="200" />
          <el-table-column prop="email" label="邮箱" min-width="220">
            <template #default="{ row }">{{ row.email || '-' }}</template>
          </el-table-column>
          <el-table-column prop="created_at" label="申请时间" min-width="180" />
          <el-table-column label="操作" width="180" fixed="right">
            <template #default="{ row }">
              <el-button type="success" size="small" @click="onApproveReset(row)">批准</el-button>
              <el-button type="danger" size="small" @click="onRejectReset(row)">拒绝</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.page-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  border-radius: var(--app-radius);
  border: 1px solid var(--app-border);
}

.section-title {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 800;
}

.table-wrap {
  overflow-x: auto;
}

.user-cell {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
</style>
