<template>
  <div class="user-management-container">
    <vab-card title="用户权限管理">
      <vab-query-form>
        <vab-query-form-left-panel>
          <el-form :inline="true" :model="queryForm" @submit.prevent>
            <el-form-item label="用户名">
              <el-input v-model="queryForm.username" placeholder="请输入用户名" clearable />
            </el-form-item>
            <el-form-item label="姓名">
              <el-input v-model="queryForm.realName" placeholder="请输入姓名" clearable />
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="queryForm.status" placeholder="全部" clearable style="width: 120px">
                <el-option label="启用" :value="1" />
                <el-option label="禁用" :value="0" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <vab-icon icon="search-line" /> 查询
              </el-button>
            </el-form-item>
          </el-form>
        </vab-query-form-left-panel>
        <vab-query-form-right-panel>
          <el-button type="primary" @click="handleAdd">
            <vab-icon icon="add-line" /> 新增用户
          </el-button>
        </vab-query-form-right-panel>
      </vab-query-form>

      <el-table v-loading="loading" :data="tableData" border stripe>
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="realName" label="姓名" width="120" />
        <el-table-column label="角色" min-width="150">
          <template #default="{ row }">
            <el-tag v-for="role in row.roles" :key="role" class="mr-5" size="small">
              {{ getRoleName(role) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="department" label="所属部门" width="150">
          <template #default="{ row }">
            {{ getDeptName(row.department) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              :active-value="1"
              :inactive-value="0"
              @change="handleStatusChange(row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)"> <vab-icon icon="edit-line" /> 编辑</el-button>
            <el-button type="warning" size="small" @click="handleResetPwd(row)"> <vab-icon icon="password-line" /> 重置密码</el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)"> <vab-icon icon="delete-line" :is-custom-svg="false" /> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <vab-pagination
        :current-page="queryForm.pageNo"
        :page-size="queryForm.pageSize"
        :total="total"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
      />
    </vab-card>

    <user-edit-dialog ref="editDialogRef" @success="handleSuccess" />
  </div>
</template>

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

defineOptions({
  name: 'UserManagement',
})

const queryForm = reactive({
  username: '',
  realName: '',
  status: '',
  pageNo: 1,
  pageSize: 10,
})

const loading = ref(false)
const total = ref(0)
const tableData = ref<any[]>([])
const editDialogRef = ref()

const getRoleName = (role: string) => {
  const map: Record<string, string> = {
    admin: '系统管理员',
    manager: '宿管主管',
    staff: '宿管员',
    repair: '维修人员',
    student: '学生'
  }
  return map[role] || role
}

const getDeptName = (dept: string) => {
  const map: Record<string, string> = {
    center: '公寓管理中心',
    logistics: '后勤保障部',
    student_affairs: '学工处'
  }
  return map[dept] || dept
}

const generateMockData = () => {
  return [
    {
      id: 1,
      username: 'admin',
      realName: '系统管理员',
      roles: ['admin'],
      department: 'center',
      status: 1,
      createTime: '2025-01-01 10:00:00',
    },
    {
      id: 2,
      username: 'zhangsan',
      realName: '张三',
      roles: ['manager'],
      department: 'center',
      status: 1,
      createTime: '2025-01-10 14:30:00',
    },
    {
      id: 3,
      username: 'lisi',
      realName: '李四',
      roles: ['staff'],
      department: 'logistics',
      status: 1,
      createTime: '2025-01-15 09:20:00',
    },
  ]
}

const handleQuery = () => {
  loading.value = true
  // Mock query
  setTimeout(() => {
    tableData.value = generateMockData()
    total.value = 20
    loading.value = false
  }, 500)
}

const handleAdd = () => {
  editDialogRef.value.open()
}

const handleEdit = (row: any) => {
  editDialogRef.value.open(row)
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm('确认删除该用户吗？', '提示', {
    type: 'warning',
  }).then(() => {
    ElMessage.success('删除成功')
    handleQuery()
  })
}

const handleResetPwd = (row: any) => {
  ElMessageBox.confirm('确认重置该用户的密码吗？', '提示', {
    type: 'warning',
  }).then(() => {
    ElMessage.success('密码已重置为 123456')
  })
}

const handleStatusChange = (row: any) => {
  ElMessage.success(`用户 ${row.realName} 状态已${row.status === 1 ? '启用' : '禁用'}`)
}

const handleSuccess = () => {
  handleQuery()
}

const handlePageChange = (val: number) => {
  queryForm.pageNo = val
  handleQuery()
}

const handleSizeChange = (val: number) => {
  queryForm.pageSize = val
  handleQuery()
}

handleQuery()
</script>

<style scoped>
.mr-5 {
  margin-right: 5px;
}
</style>
