<script setup lang="ts">
import { getUserController } from '@/api/user-controller'
import type { QueryByPageParams, UserDto, UserVo } from '@/api/models'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules, ElIcon } from 'element-plus'
import { Plus, RefreshRight, Search, Delete, Edit } from '@element-plus/icons-vue'
import { ref, reactive, computed } from 'vue'

const api = getUserController()

// 搜索表单
const searchFormRef = ref<FormInstance>()
const searchForm = reactive({
  username: '',
  account: '',
  role: '',
  enable: 1
})

// 用户表单(新增/编辑)
const dialogVisible = ref(false)
const dialogTitle = ref('新增用户')
const formRef = ref<FormInstance>()
const userForm = reactive<Partial<UserDto>>({
  id: undefined,
  username: '',
  account: '',
  password: '',
  role: ''
})
const formRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 32, message: '用户名长度需在 2 到 32 个字符之间', trigger: 'blur' }
  ],
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 4, max: 20, message: '账号长度需在 4 到 20 个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 4, max: 20, message: '密码长度需在 4 到 20 个字符之间', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' },
    { min: 4, max: 32, message: '角色长度需在 4 到 32 个字符之间', trigger: 'change' }
  ]
}

// 表格数据
const tableData = ref<UserVo[]>([])
const loading = ref(false)
const selectedRows = ref<UserVo[]>([])

// 分页
const pagination = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
})

// 角色选项
const roleOptions = [
  { label: '管理员', value: 'admin' },
  { label: '普通用户', value: 'user' },
  // { label: '访客', value: 'guest' }
]

// 是否编辑模式
const isEdit = computed(() => !!userForm.id)

// 搜索
const handleSearch = () => {
  pagination.pageNum = 1
  fetchData()
}

// 重置搜索
const handleReset = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

// 获取数据
const fetchData = async () => {
  loading.value = true
  try {
    const condition: QueryByPageParams = {
      username: searchForm.username || '',
      account: searchForm.account || '',
      role: searchForm.role || '',
      enable: searchForm.enable,
      pageNum: pagination.pageNum,
      pageSize: pagination.pageSize
    }
    const res = await api.queryByPage(condition)
    const pageData = res?.data
    tableData.value = pageData?.data || []
    pagination.total = pageData?.total || 0
  } catch (err) {
    ElMessage.error('获取数据失败')
    tableData.value = []
  } finally {
    loading.value = false
  }
}

// 翻页
const handlePageChange = (page: number) => {
  pagination.pageNum = page
  fetchData()
}

// 每页条数变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.pageNum = 1
  fetchData()
}

// 打开新增
const handleAdd = () => {
  dialogTitle.value = '新增用户'
  Object.assign(userForm, { id: undefined, username: '', account: '', password: '', role: '' })
  dialogVisible.value = true
}

// 打开编辑
const handleEdit = async (row: UserVo) => {
  dialogTitle.value = '编辑用户'
  try {
    const id = row.id || ''
    const res = await api.queryById(id)
    if (res?.code === 0 || res?.code === 200) {
      Object.assign(userForm, {
        id: res.data?.id,
        username: res.data?.username,
        account: res.data?.account,
        password: '',
        role: res.data?.role
      })
      dialogVisible.value = true
    }
  } catch {
    ElMessage.error('获取用户信息失败')
  }
}

// 保存
const handleSave = async () => {
  await formRef.value?.validate(async (valid) => {
    if (!valid) return
    try {
      const baseData = {
        username: userForm.username!,
        account: userForm.account!,
        role: userForm.role!
      }
      
      if (isEdit.value) {
        const data: Partial<UserDto> = {
          id: userForm.id,
          ...baseData,
          ...(userForm.password ? { password: userForm.password } : {})
        }
        const res = await api.edit(data as UserDto)
        if (res?.code === 0 || res?.code === 200) {
          ElMessage.success('修改成功')
          dialogVisible.value = false
          fetchData()
        } else {
          ElMessage.error(res?.message || '修改失败')
        }
      } else {
        if (!userForm.password) {
          ElMessage.warning('请输入密码')
          return
        }
        const data: UserDto = {
          ...baseData,
          password: userForm.password!
        }
        const res = await api.add(data)
        if (res?.code === 0 || res?.code === 200) {
          ElMessage.success('新增成功')
          dialogVisible.value = false
          fetchData()
        } else {
          ElMessage.error(res?.message || '新增失败')
        }
      }
    } catch {
      ElMessage.error(isEdit.value ? '修改失败' : '新增失败')
    }
  })
}

// 对话框关闭回调，重置表单验证
const handleDialogClosed = () => {
  formRef.value?.resetFields()
}

// 删除
const handleDelete = (row: UserVo) => {
  ElMessageBox.confirm('确认删除该用户吗?', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      const ids = row.id ? [row.id] : []
      const res = await api.deleteById({ ids })
      if (res?.code === 0 || res?.code === 200) {
        ElMessage.success('删除成功')
        fetchData()
      } else {
        ElMessage.error(res?.message || '删除失败')
      }
    } catch {
      ElMessage.error('删除失败')
    }
  }).catch(() => {})
}

// 批量删除
const handleBatchDelete = () => {
  if (!selectedRows.value.length) {
    ElMessage.warning('请选择要删除的数据')
    return
  }
  ElMessageBox.confirm(`确认删除选中的 ${selectedRows.value.length} 条数据吗?`, '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      const ids = selectedRows.value.map(row => row.id).filter((id): id is string => id !== undefined)
      const res = await api.deleteById({ ids })
      if (res?.code === 0 || res?.code === 200) {
        ElMessage.success('删除成功')
        selectedRows.value = []
        fetchData()
      } else {
        ElMessage.error(res?.message || '删除失败')
      }
    } catch {
      ElMessage.error('删除失败')
    }
  }).catch(() => {})
}


// 切换启用状态
const handleToggleEnable = async (row: UserVo, newEnable: number) => {
  const oldEnable = row.enable
  row.enable = newEnable
  try {
    const res = await api.updateUserStatus({ id: row.id!, enable: newEnable })
    if (res?.code === 0 || res?.code === 200) {
      ElMessage.success('状态更新成功')
    } else {
      row.enable = oldEnable
      ElMessage.error(res?.message || '状态更新失败')
    }
  } catch {
    row.enable = oldEnable
    ElMessage.error('状态更新失败')
  }
}

// 表格选择
const handleSelectionChange = (rows: UserVo[]) => {
  selectedRows.value = rows
}

// 初始加载
fetchData()
</script>

<template>
  <div class="user-view">
    <!-- 搜索区域 -->
    <el-card class="search-card" shadow="never">
      <el-form ref="searchFormRef" :model="searchForm" inline>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable style="width: 180px" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="searchForm.account" placeholder="请输入账号" clearable style="width: 180px" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="searchForm.role" placeholder="请选择角色" clearable style="width: 150px">
            <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="enable">
          <el-select v-model="searchForm.enable" 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="handleSearch">
          <el-icon class="el-icon--left"><Search /></el-icon>搜索
        </el-button>
        <el-button @click="handleReset">
          <el-icon class="el-icon--left"><RefreshRight /></el-icon>重置
        </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮 -->
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">
          <el-icon class="el-icon--left"><Plus /></el-icon>新增
        </el-button>
        <el-button type="danger" :disabled="!selectedRows.length" @click="handleBatchDelete">
          <el-icon class="el-icon--left"><Delete /></el-icon>批量删除
        </el-button>
    </div>

    <!-- 表格 -->
    <el-card class="table-card" shadow="never">
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        stripe
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" label="ID" width="80" align="center" />
        <el-table-column prop="username" label="用户名" min-width="120" align="center" />
        <el-table-column prop="account" label="账号" min-width="150" align="center" />
        <el-table-column prop="role" label="角色" width="120" align="center">
          <template #default="{ row }">
            <el-tag :type="row.role === 'admin' ? 'danger' : row.role === 'user' ? 'success' : 'info'">
              {{ roleOptions.find(t => t.value === row.role)?.label || row.role }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="enable" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-switch
              :model-value="row.enable"
              :active-value="1"
              :inactive-value="0"
              @change="(val: number) => handleToggleEnable(row, val)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="avatar" label="头像" width="100" align="center">
          <template #default="{ row }">
            <el-avatar v-if="row.avatar" :src="row.avatar" :size="40" />
            <el-avatar v-else :size="40">{{ row.username?.charAt(0) || '?' }}</el-avatar>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>编辑
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.pageNum"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handlePageChange"
          @size-change="handleSizeChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" draggable @closed="handleDialogClosed">
      <el-form ref="formRef" :model="userForm" :rules="formRules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="userForm.account" placeholder="请输入账号" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="密码" :prop="isEdit ? '' : 'password'">
          <el-input v-model="userForm.password" type="password" placeholder="请输入密码" show-password />
          <span v-if="isEdit" class="tip">留空则不修改密码</span>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="userForm.role" placeholder="请选择角色" style="width: 100%">
            <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.user-view {
  padding: 20px;
}

.search-card {
  margin-bottom: 16px;
}

.toolbar {
  margin-bottom: 16px;
}

.table-card {
  margin-bottom: 16px;
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}

.tip {
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}
</style>