<!--
  学生管理主页面
  @author pangu
-->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 左侧：学校树 -->
      <el-col :span="6">
        <SchoolTree @node-click="handleNodeClick" />
      </el-col>

      <!-- 右侧：列表区域 -->
      <el-col :span="18">
        <!-- 搜索区域 -->
        <el-card shadow="never" class="search-wrapper">
          <el-form :model="queryParams" :inline="true">
            <el-form-item label="姓名">
              <el-input v-model="queryParams.studentName" placeholder="请输入学生姓名" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="学号">
              <el-input v-model="queryParams.studentNo" placeholder="请输入学号" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="性别">
              <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable style="width: 100px">
                <el-option label="男" value="1" />
                <el-option label="女" value="2" />
              </el-select>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="queryParams.memberPhone" placeholder="请输入手机号" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :icon="Search" @click="handleQuery">查询</el-button>
              <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>

        <!-- 表格区域 -->
        <el-card shadow="never" style="margin-top: 12px;">
          <!-- 工具栏 -->
          <el-row style="margin-bottom: 12px;">
            <el-button type="primary" :icon="Plus" @click="handleAdd">新增</el-button>
            <el-button type="success" :icon="Upload" @click="handleImport">批量导入</el-button>
          </el-row>

          <!-- 数据表格 -->
          <el-table v-loading="loading" :data="studentList" border>
            <el-table-column prop="studentName" label="姓名" min-width="80" show-overflow-tooltip />
            <el-table-column prop="studentNo" label="学号" width="100" />
            <el-table-column prop="genderName" label="性别" width="60" align="center">
              <template #default="{ row }">
                <el-tag v-if="row.gender === '1'" type="primary">{{ row.genderName }}</el-tag>
                <el-tag v-else-if="row.gender === '2'" type="danger">{{ row.genderName }}</el-tag>
                <el-tag v-else type="info">{{ row.genderName }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="birthday" label="出生年月" width="100" />
            <el-table-column prop="regionPath" label="地区" min-width="150" show-overflow-tooltip />
            <el-table-column prop="schoolName" label="学校" min-width="140" show-overflow-tooltip />
            <el-table-column prop="gradeName" label="年级" width="80" />
            <el-table-column prop="className" label="班级" width="60" />
            <el-table-column prop="memberNickname" label="用户昵称" width="100" />
            <el-table-column prop="memberPhone" label="用户手机号" width="120" />
            <el-table-column label="操作" width="120" fixed="right" align="center">
              <template #default="{ row }">
                <el-button type="primary" link :icon="Edit" @click="handleEdit(row)">编辑</el-button>
                <el-popconfirm title="确认删除该学生吗？" @confirm="handleDelete(row)">
                  <template #reference>
                    <el-button type="danger" link :icon="Delete">删除</el-button>
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <el-pagination
            v-model:current-page="queryParams.pageNum"
            v-model:page-size="queryParams.pageSize"
            :total="total"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="getList"
            @current-change="getList"
            style="margin-top: 12px;"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- 学生编辑弹窗 -->
    <StudentDialog
      v-model:visible="dialogVisible"
      :student-id="currentStudentId"
      @success="getList"
    />

    <!-- 批量导入弹窗 -->
    <ImportDialog
      v-model:visible="importVisible"
      @success="getList"
    />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh, Plus, Upload, Edit, Delete } from '@element-plus/icons-vue'
import { listStudent, deleteStudent } from '@/api/student'
import SchoolTree from './components/SchoolTree.vue'
import StudentDialog from './components/StudentDialog.vue'
import ImportDialog from './components/ImportDialog.vue'

// 查询参数
const queryParams = reactive({
  studentName: '',
  studentNo: '',
  gender: '',
  memberPhone: '',
  schoolId: null,
  schoolGradeId: null,
  schoolClassId: null,
  pageNum: 1,
  pageSize: 10
})

// 数据列表
const studentList = ref([])
const total = ref(0)
const loading = ref(false)

// 弹窗控制
const dialogVisible = ref(false)
const importVisible = ref(false)
const currentStudentId = ref(null)

// 查询学生列表
async function getList() {
  loading.value = true
  try {
    const res = await listStudent(queryParams)
    if (res.code === 200) {
      studentList.value = res.rows || []
      total.value = res.total || 0
    }
  } catch (error) {
    ElMessage.error('查询失败')
  } finally {
    loading.value = false
  }
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  Object.assign(queryParams, {
    studentName: '',
    studentNo: '',
    gender: '',
    memberPhone: '',
    schoolId: null,
    schoolGradeId: null,
    schoolClassId: null,
    pageNum: 1,
    pageSize: 10
  })
  getList()
}

// 学校树节点点击
function handleNodeClick(node) {
  if (node.nodeType === 'school') {
    queryParams.schoolId = node.nodeId
    queryParams.schoolGradeId = null
    queryParams.schoolClassId = null
  } else if (node.nodeType === 'grade') {
    queryParams.schoolGradeId = node.nodeId
    queryParams.schoolClassId = null
  } else if (node.nodeType === 'class') {
    queryParams.schoolClassId = node.nodeId
  }
  handleQuery()
}

// 新增
function handleAdd() {
  currentStudentId.value = null
  dialogVisible.value = true
}

// 编辑
function handleEdit(row) {
  currentStudentId.value = row.studentId
  dialogVisible.value = true
}

// 删除
async function handleDelete(row) {
  try {
    const res = await deleteStudent(row.studentId)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      getList()
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  } catch (error) {
    ElMessage.error('删除失败')
  }
}

// 批量导入
function handleImport() {
  importVisible.value = true
}

// 初始化
getList()
</script>

<style scoped>
.search-wrapper :deep(.el-form-item) {
  margin-bottom: 0;
}
</style>
