<template>
  <div class="user-manage-container">
    <el-card shadow="never" class="main-card">
      <template #header>
        <div class="header-row">
          <div class="left-panel">
            <h2 class="sys-title">👥 用户与权限管理</h2>
          </div>
          <div class="header-actions">
            <el-button @click="router.push('/dashboard')" icon="Back">返回监控</el-button>
            <el-button type="primary" icon="Plus" @click="openCreateModal">新建用户</el-button>
          </div>
        </div>
      </template>

      <el-table :data="users" border style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" align="center" />
        <el-table-column prop="username" label="用户名" min-width="150">
          <template #default="{ row }">
            <span style="font-weight: bold;">{{ row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="role" label="角色身份" width="150" align="center">
          <template #default="{ row }">
            <el-tag v-if="row.role === 'admin'" type="danger" effect="dark">超级管理员</el-tag>
            <el-tag v-else-if="row.role === 'engineer'" type="warning" effect="dark">设备工程师</el-tag>
            <el-tag v-else type="info" effect="plain">普通客户</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" min-width="180">
          <template #default="{ row }">
            {{ new Date(row.created_at).toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="关联设备数" min-width="120" align="center">
          <template #default="{ row }">
            <el-tag v-if="row.role === 'admin'" type="danger" effect="plain">全部权限</el-tag>
            <el-tag v-else effect="plain" type="success">{{ row.allowed_device_ids?.length || 0 }} 台</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" align="center" fixed="right">
          <template #default="{ row }">
            <el-button
                v-if="row.role !== 'admin'"
                type="primary"
                link
                icon="Setting"
                @click="openPermissionModal(row)"
            >
              分配设备
            </el-button>
            <el-popconfirm
                title="确定删除该用户吗?"
                confirm-button-text="删除"
                cancel-button-text="取消"
                icon="Warning"
                icon-color="red"
                @confirm="deleteUser(row.id)"
            >
              <template #reference>
                <el-button type="danger" link icon="Delete">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="showCreateModal" title="新建账号" width="400px">
      <el-form :model="newUser" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="newUser.username" placeholder="请输入登录名" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="newUser.password" type="password" placeholder="设置初始密码" show-password />
        </el-form-item>
        <el-form-item label="角色权限">
          <el-select v-model="newUser.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="普通客户 (只读)" value="client" />
            <el-option label="设备工程师 (可维护)" value="engineer" />
            <el-option label="超级管理员 (Root权限)" value="admin" />
          </el-select>
          <div style="font-size: 12px; color: #999; margin-top: 5px; line-height: 1.2;">
            <span v-if="newUser.role === 'admin'" style="color: #f56c6c;">* 拥有删除用户、爬虫控制等最高权限</span>
            <span v-else-if="newUser.role === 'engineer'" style="color: #e6a23c;">* 拥有修改设备地点、写日志权限</span>
            <span v-else>* 仅可查看被分配的设备数据</span>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showCreateModal = false">取消</el-button>
          <el-button type="primary" @click="createUser" :loading="creating">确认创建</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
        v-model="showPermissionModal"
        :title="`分配设备 - [${currentUser?.username}]`"
        width="720px"
        top="5vh"
        destroy-on-close
    >
      <div class="permission-wrapper">
        <div class="selection-toolbar">
          <el-input
              v-model="deviceFilterKeyword"
              placeholder="搜索设备名称或地点..."
              prefix-icon="Search"
              clearable
              style="width: 240px"
          />

          <div class="toolbar-stats">
            <span>已选: <span class="highlight-count">{{ selectedDeviceIds.length }}</span> / {{ allDevices.length }}</span>
            <el-divider direction="vertical" />
            <el-checkbox v-model="showSelectedOnly" label="只看已选" size="small" />
          </div>

          <div class="toolbar-actions">
            <el-button link type="primary" @click="selectAllDevices">全选</el-button>
            <el-button link type="info" @click="clearAllDevices">清空</el-button>
          </div>
        </div>

        <div class="device-grid-container">
          <el-scrollbar max-height="450px">
            <div class="device-grid">
              <div
                  v-for="device in displayDevices"
                  :key="device.id"
                  class="device-card"
                  :class="{ 'is-active': selectedDeviceIds.includes(device.id) }"
                  @click="toggleDeviceSelection(device.id)"
              >
                <div class="card-content">
                  <div class="d-name">{{ device.name }}</div>
                  <div class="d-site">
                    <el-icon><Location /></el-icon> {{ device.install_site || '未分配地点' }}
                  </div>
                </div>
                <div class="check-mark" v-if="selectedDeviceIds.includes(device.id)">
                  <el-icon><Check /></el-icon>
                </div>
              </div>

              <div v-if="displayDevices.length === 0" class="empty-tip">
                <el-empty description="没有找到匹配的设备" :image-size="80" />
              </div>
            </div>
          </el-scrollbar>
        </div>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showPermissionModal = false">取消</el-button>
          <el-button type="primary" @click="savePermissions">保存授权 ({{ selectedDeviceIds.length }})</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import request from '../utils/request'
import { ElMessage } from 'element-plus'
import { Back, Plus, Setting, Delete, Warning, Search, Location, Check } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const creating = ref(false)

const users = ref([])
const rawDevices = ref([]) // 原始设备数据
const showCreateModal = ref(false)
const showPermissionModal = ref(false)

const newUser = ref({ username: '', password: '', role: 'client' })
const currentUser = ref(null)

// 🟢 新增/修改的状态变量
const selectedDeviceIds = ref([]) // 存储当前选中的ID数组
const deviceFilterKeyword = ref('') // 搜索关键词
const showSelectedOnly = ref(false) // 是否只看已选

// 统一设备列表数据源
const allDevices = computed(() => rawDevices.value)

// 🟢 核心计算逻辑：过滤设备列表
const displayDevices = computed(() => {
  let list = allDevices.value

  // 1. 关键词过滤
  if (deviceFilterKeyword.value) {
    const k = deviceFilterKeyword.value.toLowerCase()
    list = list.filter(d =>
        (d.name && d.name.toLowerCase().includes(k)) ||
        (d.install_site && d.install_site.toLowerCase().includes(k))
    )
  }

  // 2. "只看已选"过滤
  if (showSelectedOnly.value) {
    list = list.filter(d => selectedDeviceIds.value.includes(d.id))
  }

  return list
})

onMounted(async () => {
  await fetchUsers()
  await fetchAllDevices()
})

const fetchUsers = async () => {
  loading.value = true
  try {
    const res = await request.get('/api/admin/users')
    users.value = res.data.data || res.data
  } catch (e) { console.error(e) } finally { loading.value = false }
}

const fetchAllDevices = async () => {
  try {
    const res = await request.get('/api/devices_overview')
    rawDevices.value = res.data.data || res.data
  } catch (e) { console.error(e) }
}

const openCreateModal = () => {
  newUser.value = {username: '', password: '', role: 'client'}
  showCreateModal.value = true
}

const createUser = async () => {
  if (!newUser.value.username || !newUser.value.password) return ElMessage.warning('请填写完整')
  creating.value = true
  try {
    await request.post('/api/admin/create_user', newUser.value)
    ElMessage.success('用户创建成功')
    showCreateModal.value = false
    fetchUsers()
  } catch (e) {
    ElMessage.error(e.response?.data?.msg || '创建失败')
  } finally {
    creating.value = false
  }
}

// 🟢 打开分配弹窗
const openPermissionModal = (user) => {
  currentUser.value = user
  // 确保是新数组，避免引用污染
  selectedDeviceIds.value = user.allowed_device_ids ? [...user.allowed_device_ids] : []
  // 重置筛选状态
  deviceFilterKeyword.value = ''
  showSelectedOnly.value = false
  showPermissionModal.value = true
}

// 🟢 切换单个选中状态
const toggleDeviceSelection = (id) => {
  const index = selectedDeviceIds.value.indexOf(id)
  if (index > -1) {
    selectedDeviceIds.value.splice(index, 1)
  } else {
    selectedDeviceIds.value.push(id)
  }
}

// 🟢 全选（基于当前过滤后的列表）
const selectAllDevices = () => {
  const currentIds = displayDevices.value.map(d => d.id)
  // 将未选中的添加进去（Set去重）
  const newSet = new Set([...selectedDeviceIds.value, ...currentIds])
  selectedDeviceIds.value = Array.from(newSet)
}

// 🟢 清空（全部清空）
const clearAllDevices = () => {
  selectedDeviceIds.value = []
}

const savePermissions = async () => {
  try {
    await request.post('/api/admin/assign_devices', {
      user_id: currentUser.value.id,
      device_ids: selectedDeviceIds.value
    })
    ElMessage.success('权限已更新')
    showPermissionModal.value = false
    fetchUsers()
  } catch (e) {
    ElMessage.error('保存失败')
  }
}

const deleteUser = async (id) => {
  try {
    await request.post('/api/admin/delete_user', {user_id: id})
    ElMessage.success('用户已删除')
    fetchUsers()
  } catch (e) {
    ElMessage.error(e.response?.data?.msg || '删除失败')
  }
}
</script>

<style scoped>
.user-manage-container {
  padding: 10px;
  background: #f5f7fa;
  min-height: 100vh;
  box-sizing: border-box;
}

.main-card {
  border-radius: 8px;
  min-height: 80vh;
}

.header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.sys-title {
  margin: 0;
  font-size: 20px;
  color: #303133;
  font-weight: 700;
}

.header-actions {
  display: flex;
  gap: 10px;
}

/* 🟢 新增：权限选择器样式 */
.permission-wrapper {
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  overflow: hidden;
}

.selection-toolbar {
  padding: 10px 15px;
  background: #f5f7fa;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  align-items: center;
  gap: 15px;
  flex-wrap: wrap;
}

.toolbar-stats {
  font-size: 13px;
  color: #606266;
  display: flex;
  align-items: center;
  gap: 10px;
}

.highlight-count {
  color: #409EFF;
  font-weight: bold;
  font-size: 15px;
}

.toolbar-actions {
  margin-left: auto;
}

.device-grid-container {
  padding: 15px;
  background: #fff;
}

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.device-card {
  position: relative;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  padding: 12px;
  cursor: pointer;
  transition: all 0.2s;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.device-card:hover {
  border-color: #c6e2ff;
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.device-card.is-active {
  border-color: #409EFF;
  background-color: #ecf5ff;
}

.card-content {
  pointer-events: none; /* 让点击穿透到底层div */
}

.d-name {
  font-weight: bold;
  color: #303133;
  font-size: 14px;
  margin-bottom: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.d-site {
  font-size: 12px;
  color: #909399;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 右下角打钩图标 */
.check-mark {
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 28px 28px 0;
  border-color: transparent #409EFF transparent transparent;
}

.check-mark .el-icon {
  position: absolute;
  top: 2px;
  right: -26px;
  color: #fff;
  font-size: 12px;
  font-weight: bold;
}

.empty-tip {
  grid-column: 1 / -1;
  text-align: center;
  padding: 20px;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .selection-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .toolbar-actions {
    margin-left: 0;
    display: flex;
    justify-content: flex-end;
  }
  .device-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
