<template>
  <div class="user-management">
    <h3 class="page-title">👥 用户管理</h3>

    <!-- Translation Suggestions Review -->
    <div v-if="translations.length > 0" class="review-section">
      <h4 class="section-title">🌐 翻译建议</h4>
      <div class="review-list">
        <div v-for="t in translations" :key="t._id || t.id" class="review-item">
          <div class="review-info">
            <span class="review-original">{{ t.original }}</span>
            <span class="review-arrow">&rarr;</span>
            <span class="review-suggested">{{ t.suggested }}</span>
            <span class="review-user">{{ t.user_name || '匿名' }}</span>
          </div>
          <div class="review-actions">
            <button class="btn-sm btn-approve" @click="approveTranslation(t)">采纳</button>
            <button class="btn-sm btn-reject" @click="rejectTranslation(t)">拒绝</button>
          </div>
        </div>
      </div>
    </div>

    <!-- Business Application Approval -->
    <div v-if="groupedBizApps.length > 0" class="review-section">
      <h4 class="section-title">💼 商业认证申请</h4>
      <div class="review-list">
        <div v-for="group in groupedBizApps" :key="group.user_id" class="biz-app-group">
          <div class="review-item">
            <div class="review-info">
              <span class="review-name">{{ group.latest.display_name || group.latest.username }}</span>
              <span class="review-reason">商户名：{{ group.latest.business_name }}</span>
              <span class="biz-status-tag" :class="'biz-' + group.effectiveStatus">{{ { pending: '待审核', approved: '已通过', rejected: '已拒绝' }[group.effectiveStatus] }}</span>
              <img v-if="group.latest.document && group.latest.document.startsWith('data:image')" :src="group.latest.document" class="biz-doc-preview" @click="showDocFull = group.latest.document" />
            </div>
            <div class="review-actions">
              <template v-if="group.effectiveStatus === 'pending'">
                <button class="btn-sm btn-approve" @click="approveBusiness(group.latest)">通过</button>
                <button class="btn-sm btn-reject" @click="rejectBusiness(group.latest)">拒绝</button>
              </template>
              <button v-if="group.history.length > 1" class="btn-sm btn-outline" @click="group.expanded = !group.expanded">
                {{ group.expanded ? '收起' : `历史 (${group.history.length})` }}
              </button>
            </div>
          </div>
          <div v-if="group.expanded" class="biz-history">
            <div v-for="app in group.history" :key="app.id" class="biz-history-item">
              <span class="biz-status-tag small" :class="'biz-' + app.status">{{ { pending: '待审核', approved: '已通过', rejected: '已拒绝' }[app.status] }}</span>
              <span>{{ app.business_name }}</span>
              <span v-if="app.reject_reason" class="biz-reject-reason">拒绝原因：{{ app.reject_reason }}</span>
              <span class="biz-time">{{ formatDate(app.created_at) }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- User self-registers, admin assigns roles below -->

    <!-- Search & Filter -->
    <div class="filter-toolbar">
      <div class="search-box">
        <input
          class="search-input"
          v-model="searchQuery"
          placeholder="搜索用户..."
        />
        <button v-if="searchQuery" class="search-clear-btn" @click="searchQuery = ''">✕</button>
      </div>
      <div class="role-filters">
        <button
          v-for="r in roles"
          :key="r.value"
          class="filter-btn"
          :class="{ active: filterRole === r.value }"
          @click="filterRole = filterRole === r.value ? '' : r.value"
        >{{ r.label }}</button>
      </div>
    </div>

    <!-- User List -->
    <div class="user-list">
      <div v-for="u in filteredUsers" :key="u._id || u.id" class="user-card">
        <div class="user-info">
          <div class="user-name">
            {{ u.display_name || u.username }}
            <span class="user-username" v-if="u.display_name">@{{ u.username }}</span>
          </div>
          <div class="user-meta">
            <span class="user-role-badge" :class="'role-' + u.role">{{ roleLabel(u.role) }}</span>
            <span v-if="u.business_verified" class="biz-badge">💼 商业认证</span>
            <span class="user-date">注册: {{ formatDate(u.created_at) }}</span>
          </div>
        </div>
        <div class="user-actions">
          <select
            :value="u.role"
            class="role-select"
            @change="changeRole(u, $event.target.value)"
            :disabled="u.role === 'admin'"
          >
            <option value="viewer">查看者</option>
            <option value="editor">编辑</option>
            <option value="senior_editor">高级编辑</option>
          </select>
          <button v-if="!u.business_verified" class="btn-sm btn-outline" @click="grantBusiness(u)" title="开通商业认证">💼</button>
          <button v-else class="btn-sm btn-outline" @click="revokeBusiness(u)" title="撤销商业认证" style="opacity:0.5">💼✕</button>
          <button class="btn-sm btn-delete" @click="removeUser(u)" title="删除用户">🗑️</button>
        </div>
      </div>
      <div v-if="filteredUsers.length === 0" class="empty-hint">未找到用户</div>
    </div>

    <div class="user-count">共 {{ users.length }} 个用户</div>

    <!-- Full-size document preview -->
    <div v-if="showDocFull" class="doc-overlay" @click="showDocFull = null">
      <img :src="showDocFull" class="doc-full-img" />
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useAuthStore } from '../stores/auth'
import { useUiStore } from '../stores/ui'
import { api } from '../composables/useApi'
import { showConfirm, showPrompt } from '../composables/useDialog'

const auth = useAuthStore()
const ui = useUiStore()

const users = ref([])
const searchQuery = ref('')
const filterRole = ref('')
const translations = ref([])
const showDocFull = ref(null)
const businessApps = ref([])
import { reactive } from 'vue'

const groupedBizApps = computed(() => {
  const map = {}
  for (const app of businessApps.value) {
    const uid = app.user_id
    if (!map[uid]) map[uid] = { user_id: uid, history: [], latest: null, expanded: false }
    map[uid].history.push(app)
  }
  return Object.values(map).map(g => {
    g.history.sort((a, b) => b.id - a.id)
    g.latest = g.history[0]
    // Check if user is already verified (from users list)
    const user = users.value.find(u => (u._id || u.id) === g.user_id)
    if (user && user.business_verified) {
      g.effectiveStatus = 'approved'
    } else {
      g.effectiveStatus = g.latest.status
    }
    return reactive(g)
  }).filter(g => g.latest)
})

function formatDate(d) {
  if (!d) return ''
  return new Date(d + 'Z').toLocaleString('zh-CN', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' })
}

const roles = [
  { value: 'admin', label: '管理员' },
  { value: 'senior_editor', label: '高级编辑' },
  { value: 'editor', label: '编辑' },
  { value: 'viewer', label: '查看者' },
]

const filteredUsers = computed(() => {
  let list = users.value
  if (searchQuery.value.trim()) {
    const q = searchQuery.value.trim().toLowerCase()
    list = list.filter(u =>
      (u.username || '').toLowerCase().includes(q) ||
      (u.display_name || '').toLowerCase().includes(q)
    )
  }
  if (filterRole.value) {
    list = list.filter(u => u.role === filterRole.value)
  }
  return list
})

function roleLabel(role) {
  const map = { admin: '管理员', senior_editor: '高级编辑', editor: '编辑', viewer: '查看者' }
  return map[role] || role
}


async function loadUsers() {
  try {
    const res = await api('/api/users')
    if (res.ok) {
      users.value = await res.json()
    }
  } catch {
    users.value = []
  }
}

async function loadTranslations() {
  try {
    const res = await api('/api/translation-suggestions')
    if (res.ok) {
      translations.value = await res.json()
    }
  } catch {
    translations.value = []
  }
}

async function loadBusinessApps() {
  try {
    const res = await api('/api/business-applications')
    if (res.ok) {
      businessApps.value = await res.json()
    }
  } catch {
    businessApps.value = []
  }
}

async function changeRole(user, newRole) {
  const id = user._id || user.id
  try {
    const res = await api(`/api/users/${id}`, {
      method: 'PUT',
      body: JSON.stringify({ role: newRole }),
    })
    if (res.ok) {
      user.role = newRole
      ui.showToast(`已更新 ${user.display_name || user.username} 的角色`)
    }
  } catch {
    ui.showToast('更新失败')
  }
}

async function removeUser(user) {
  const ok = await showConfirm(`确定删除用户 "${user.display_name || user.username}"？此操作不可撤销。`)
  if (!ok) return
  const id = user._id || user.id
  try {
    const res = await api(`/api/users/${id}`, { method: 'DELETE' })
    if (res.ok) {
      users.value = users.value.filter(u => (u._id || u.id) !== id)
      ui.showToast('已删除')
    }
  } catch {
    ui.showToast('删除失败')
  }
}

async function grantBusiness(user) {
  const ok = await showConfirm(`直接为「${user.display_name || user.username}」开通商业认证？`)
  if (!ok) return
  const id = user._id || user.id
  try {
    const res = await api(`/api/business-grant/${id}`, { method: 'POST' })
    if (res.ok) {
      user.business_verified = 1
      ui.showToast('已开通商业认证')
    }
  } catch {
    ui.showToast('操作失败')
  }
}

async function revokeBusiness(user) {
  const ok = await showConfirm(`撤销「${user.display_name || user.username}」的商业认证？`)
  if (!ok) return
  const id = user._id || user.id
  try {
    const res = await api(`/api/business-revoke/${id}`, { method: 'POST' })
    if (res.ok) {
      user.business_verified = 0
      ui.showToast('已撤销商业认证')
    }
  } catch {
    ui.showToast('操作失败')
  }
}

async function approveTranslation(t) {
  const id = t._id || t.id
  try {
    const res = await api(`/api/translation-suggestions/${id}/approve`, { method: 'POST' })
    if (res.ok) {
      translations.value = translations.value.filter(item => (item._id || item.id) !== id)
      ui.showToast('已采纳')
    }
  } catch {
    ui.showToast('操作失败')
  }
}

async function rejectTranslation(t) {
  const id = t._id || t.id
  try {
    const res = await api(`/api/translation-suggestions/${id}/reject`, { method: 'POST' })
    if (res.ok) {
      translations.value = translations.value.filter(item => (item._id || item.id) !== id)
      ui.showToast('已拒绝')
    }
  } catch {
    ui.showToast('操作失败')
  }
}

async function approveBusiness(app) {
  const id = app._id || app.id
  try {
    const res = await api(`/api/business-applications/${id}/approve`, { method: 'POST' })
    if (res.ok) {
      businessApps.value = businessApps.value.filter(item => (item._id || item.id) !== id)
      ui.showToast('已通过')
    }
  } catch {
    ui.showToast('操作失败')
  }
}

async function rejectBusiness(app) {
  const id = app._id || app.id
  const reason = await showPrompt('请输入拒绝原因（选填）：')
  if (reason === null) return
  try {
    const res = await api(`/api/business-applications/${id}/reject`, {
      method: 'POST',
      body: JSON.stringify({ reason: reason || '' }),
    })
    if (res.ok) {
      businessApps.value = businessApps.value.filter(item => (item._id || item.id) !== id)
      ui.showToast('已拒绝')
    }
  } catch {
    ui.showToast('操作失败')
  }
}

onMounted(() => {
  loadUsers()
  loadTranslations()
  loadBusinessApps()
})
</script>

<style scoped>
.user-management {
  padding: 0 12px 24px;
}

.page-title {
  margin: 0 0 16px;
  font-size: 16px;
  color: #3e3a44;
}

.section-title {
  font-size: 14px;
  font-weight: 600;
  color: #3e3a44;
  margin: 0 0 10px;
}

/* Review sections */
.review-section {
  margin-bottom: 18px;
  padding: 14px;
  background: #fff8e1;
  border-radius: 12px;
  border: 1.5px solid #ffe082;
}

.review-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.review-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: #fff;
  border-radius: 8px;
  gap: 8px;
  flex-wrap: wrap;
}

.review-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  flex-wrap: wrap;
  font-size: 13px;
}

.review-original {
  font-weight: 500;
  color: #3e3a44;
}

.review-arrow {
  color: #b0aab5;
}

.review-suggested {
  font-weight: 600;
  color: #4a9d7e;
}

.review-user,
.review-reason {
  font-size: 12px;
  color: #999;
}

.review-name {
  font-weight: 600;
  color: #3e3a44;
}

.review-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.biz-app-group { margin-bottom: 6px; }
.biz-status-tag {
  font-size: 11px; padding: 2px 8px; border-radius: 8px; font-weight: 500; white-space: nowrap;
}
.biz-status-tag.small { font-size: 10px; padding: 1px 6px; }
.biz-pending { background: #fff3e0; color: #e65100; }
.biz-approved { background: #e8f5e9; color: #2e7d32; }
.biz-rejected { background: #fce4ec; color: #c62828; }
.biz-history {
  margin: 4px 0 8px 16px; padding: 8px 12px; background: #fafaf8; border-radius: 8px; border-left: 3px solid #e5e4e7;
}
.biz-history-item {
  display: flex; align-items: center; gap: 8px; font-size: 12px; padding: 4px 0; flex-wrap: wrap;
}
.biz-reject-reason { color: #c62828; font-size: 11px; }
.biz-time { color: #bbb; font-size: 11px; margin-left: auto; }
.biz-doc-preview { width: 60px; height: 60px; object-fit: cover; border-radius: 6px; cursor: pointer; border: 1px solid #e5e4e7; margin-top: 6px; }
.biz-doc-preview:hover { border-color: #7ec6a4; }
.doc-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.7); z-index: 1000; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.doc-full-img { max-width: 90vw; max-height: 90vh; border-radius: 10px; }

.btn-approve {
  background: #4a9d7e;
  color: #fff;
  border: none;
  cursor: pointer;
  font-family: inherit;
  border-radius: 8px;
}

.btn-reject {
  background: #ef5350;
  color: #fff;
  border: none;
  cursor: pointer;
  font-family: inherit;
  border-radius: 8px;
}

/* Create user */
.create-section {
  margin-bottom: 18px;
  padding: 14px;
  background: #f8f7f5;
  border-radius: 12px;
  border: 1.5px solid #e5e4e7;
}

.create-form {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}

.form-input {
  flex: 1;
  min-width: 120px;
  padding: 8px 12px;
  border: 1.5px solid #d4cfc7;
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
}

.form-input:focus {
  border-color: #7ec6a4;
}

.form-select {
  padding: 8px 10px;
  border: 1.5px solid #d4cfc7;
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  background: #fff;
}

.created-link {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  padding: 10px 12px;
  background: #e8f5e9;
  border-radius: 8px;
  font-size: 12px;
  flex-wrap: wrap;
}

.created-link code {
  flex: 1;
  word-break: break-all;
  font-size: 11px;
  background: #fff;
  padding: 4px 8px;
  border-radius: 4px;
}

/* Filter toolbar */
.filter-toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
  align-items: center;
}

.search-box {
  display: flex;
  align-items: center;
  background: #f8f7f5;
  border-radius: 10px;
  padding: 2px 8px;
  border: 1.5px solid #e5e4e7;
  flex: 1;
  min-width: 150px;
}

.search-input {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px 6px;
  font-size: 14px;
  outline: none;
  font-family: inherit;
}

.search-clear-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  color: #999;
  padding: 4px;
}

.role-filters {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 5px 14px;
  border-radius: 16px;
  border: 1.5px solid #e5e4e7;
  background: #fff;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  color: #6b6375;
  transition: all 0.15s;
}

.filter-btn.active {
  background: #e8f5e9;
  border-color: #7ec6a4;
  color: #2e7d5a;
  font-weight: 600;
}

/* User list */
.user-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.user-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: #fff;
  border: 1.5px solid #e5e4e7;
  border-radius: 10px;
  gap: 10px;
  flex-wrap: wrap;
}

.user-card:hover {
  border-color: #d4cfc7;
}

.user-info {
  flex: 1;
  min-width: 0;
}

.user-name {
  font-weight: 600;
  font-size: 14px;
  color: #3e3a44;
  margin-bottom: 4px;
}

.user-username {
  font-weight: 400;
  font-size: 12px;
  color: #b0aab5;
  margin-left: 4px;
}

.user-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.user-role-badge {
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

.role-admin { background: #f3e5f5; color: #7b1fa2; }
.role-senior_editor { background: #e3f2fd; color: #1565c0; }
.role-editor { background: #e8f5e9; color: #2e7d5a; }
.role-viewer { background: #f5f5f5; color: #757575; }

.biz-badge {
  font-size: 11px;
  color: #e65100;
}

.user-date {
  font-size: 11px;
  color: #b0aab5;
}

.user-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.role-select {
  padding: 5px 8px;
  border: 1.5px solid #d4cfc7;
  border-radius: 8px;
  font-size: 12px;
  font-family: inherit;
  background: #fff;
}

.btn-sm {
  padding: 5px 12px;
  font-size: 12px;
  border-radius: 8px;
}

.btn-outline {
  background: #fff;
  color: #6b6375;
  border: 1.5px solid #d4cfc7;
  cursor: pointer;
  font-family: inherit;
}

.btn-outline:hover {
  background: #f8f7f5;
}

.btn-delete {
  background: #fff;
  color: #ef5350;
  border: 1.5px solid #ffcdd2;
  cursor: pointer;
  font-family: inherit;
}

.btn-primary {
  background: linear-gradient(135deg, #7ec6a4 0%, #4a9d7e 100%);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 9px 20px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: default;
}

.user-count {
  text-align: center;
  font-size: 12px;
  color: #b0aab5;
  margin-top: 16px;
}

.empty-hint {
  text-align: center;
  color: #b0aab5;
  font-size: 13px;
  padding: 24px 0;
}

@media (max-width: 600px) {
  .create-form {
    flex-direction: column;
  }
  .create-form .form-input,
  .create-form .form-select {
    width: 100%;
  }
  .user-card {
    flex-direction: column;
    align-items: flex-start;
  }
  .user-actions {
    width: 100%;
    justify-content: flex-end;
  }
}
</style>
