<template>
  <div class="teacher-list">
    <div class="page-header">
      <h2>教师管理</h2>
      <el-button type="primary" @click="handleAdd">添加教师</el-button>
    </div>
    
    <el-card class="filter-container">
      <el-form :inline="true" :model="queryParams" class="filter-form">
        <el-form-item label="教师姓名">
          <el-input v-model="queryParams.name" placeholder="请输入教师姓名" clearable />
        </el-form-item>
        
        <el-form-item label="所属部门">
          <el-select v-model="queryParams.department" placeholder="请选择部门" clearable>
            <el-option v-for="dept in departments" :key="dept.value" :label="dept.label" :value="dept.value" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="在职" value="active" />
            <el-option label="离职" value="inactive" />
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="fetchTeachers">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <el-card class="table-container">
      <el-table
        v-loading="loading"
        :data="teachers"
        border
        style="width: 100%"
      >
        <el-table-column type="index" width="50" />
        <el-table-column prop="name" label="姓名" min-width="120" />
        <el-table-column prop="gender" label="性别" width="80">
          <template #default="{ row }">
            {{ row.gender === 'male' ? '男' : row.gender === 'female' ? '女' : '未知' }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="联系电话" min-width="120" />
        <el-table-column prop="email" label="邮箱" min-width="180" />
        <el-table-column prop="department" label="所属部门" min-width="120" />
        <el-table-column prop="title" label="职称" min-width="120" />
        <el-table-column prop="courseCount" label="课程数量" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'info'">
              {{ row.status === 'active' ? '在职' : '离职' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="viewTeacher(row.id)">查看</el-button>
            <el-button type="primary" link @click="editTeacher(row.id)">编辑</el-button>
            <el-popconfirm
              title="确定要删除该教师吗？"
              @confirm="deleteTeacherHandler(row.id)"
            >
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="queryParams.page"
          v-model:page-size="queryParams.limit"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getTeachers, deleteTeacher } from '../../../api/teacher'

const router = useRouter()
const loading = ref(false)
const teachers = ref([])
const total = ref(0)

const departments = [
  { value: 'computer', label: '计算机科学系' },
  { value: 'math', label: '数学系' },
  { value: 'physics', label: '物理系' },
  { value: 'chemistry', label: '化学系' },
  { value: 'biology', label: '生物系' }
]

const queryParams = ref({
  page: 1,
  limit: 10,
  name: '',
  department: '',
  status: ''
})

const fetchTeachers = async () => {
  loading.value = true
  try {
    const res = await getTeachers(queryParams.value)
    teachers.value = res.data.list
    total.value = res.data.total
  } catch (error) {
    console.error('获取教师列表失败:', error)
  } finally {
    loading.value = false
  }
}

const resetQuery = () => {
  queryParams.value = {
    page: 1,
    limit: 10,
    name: '',
    department: '',
    status: ''
  }
  fetchTeachers()
}

const handleSizeChange = (val) => {
  queryParams.value.limit = val
  fetchTeachers()
}

const handleCurrentChange = (val) => {
  queryParams.value.page = val
  fetchTeachers()
}

const viewTeacher = (id) => {
  router.push(`/admin/teachers/${id}`)
}

const editTeacher = (id) => {
  router.push(`/admin/teachers/${id}/edit`)
}

const deleteTeacherHandler = async (id) => {
  try {
    await deleteTeacher(id)
    ElMessage.success('删除成功')
    fetchTeachers()
  } catch (error) {
    console.error('删除教师失败:', error)
  }
}

const handleAdd = () => {
  // 实现添加教师逻辑
}

const handleEdit = (row) => {
  // 实现编辑教师逻辑
}

const handleDelete = (row) => {
  // 实现删除教师逻辑
}

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

<style lang="scss" scoped>
.teacher-list {
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    h2 {
      margin: 0;
      font-size: 24px;
    }
  }
  
  .filter-container {
    margin-bottom: 20px;
  }
  
  .table-container {
    margin-bottom: 20px;
  }
  
  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style> 