<template>
  <ElTablePro
    :data="tableData"
    :total="total"
    v-model:page="query.page"
    v-model:page-size="query.pageSize"
    table-key="user"
    row-key="id"
    border
    stripe
    v-loading="loading"
    @pagination-change="handlePaginationChange"
  >
    <template #toolbar>
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </template>

    <el-table-column type="index" label="序号" width="80" />
    <el-table-column prop="name" label="姓名" />
    <el-table-column prop="age" label="年龄" width="80" />
    <el-table-column prop="email" label="邮箱" />
    <el-table-column prop="role" label="角色" width="120">
      <template #default="{ row }">
        <el-tag :type="row.role === '管理员' ? 'danger' : row.role === '编辑' ? 'warning' : 'info'">
          {{ row.role }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="status" label="状态" width="100">
      <template #default="{ row }">
        <el-tag :type="row.status ? 'success' : 'danger'">
          {{ row.status ? '启用' : '禁用' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180">
      <template #default="{ row }">
        <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
        <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
      </template>
    </el-table-column>
  </ElTablePro>
</template>

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

const loading = ref(false)
const total = ref(0)

const query = reactive({
  page: 1,
  pageSize: 10,
})

const allData = [
  { id: 1, name: '张三', age: 28, email: 'zhangsan@example.com', role: '管理员', status: true },
  { id: 2, name: '李四', age: 32, email: 'lisi@example.com', role: '编辑', status: true },
  { id: 3, name: '王五', age: 25, email: 'wangwu@example.com', role: '用户', status: false },
  { id: 4, name: '赵六', age: 29, email: 'zhaoliu@example.com', role: '用户', status: true },
  { id: 5, name: '孙七', age: 35, email: 'sunqi@example.com', role: '编辑', status: true },
  { id: 6, name: '周八', age: 27, email: 'zhouba@example.com', role: '用户', status: true },
  { id: 7, name: '吴九', age: 31, email: 'wujiu@example.com', role: '管理员', status: true },
  { id: 8, name: '郑十', age: 24, email: 'zhengshi@example.com', role: '用户', status: false },
  { id: 9, name: '刘一', age: 33, email: 'liuyi@example.com', role: '编辑', status: true },
  { id: 10, name: '陈二', age: 26, email: 'chener@example.com', role: '用户', status: true },
  { id: 11, name: '杨三', age: 30, email: 'yangsan@example.com', role: '用户', status: true },
  { id: 12, name: '黄四', age: 28, email: 'huangsi@example.com', role: '编辑', status: false },
]

const tableData = ref<any[]>([])

const getList = () => {
  loading.value = true
  setTimeout(() => {
    const start = (query.page - 1) * query.pageSize
    const end = start + query.pageSize
    tableData.value = allData.slice(start, end)
    total.value = allData.length
    loading.value = false
  }, 300)
}

const handlePaginationChange = (_value: { page: number; pageSize: number }) => {
  getList()
}

const handleAdd = () => {
  ElMessage.info('新增用户')
}

const handleEdit = (row: any) => {
  ElMessage.info(`编辑用户: ${row.name}`)
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确认删除用户 ${row.name}？`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    ElMessage.success('删除成功')
  }).catch(() => {})
}

onMounted(() => {
  getList()
})
</script>
