<template>
  <div class="app-container">
    <el-row :gutter="16">
      <!-- 左侧区域树 -->
      <el-col :span="5">
        <el-card shadow="never">
          <template #header>
            <span>区域筛选</span>
          </template>
          <el-input 
            v-model="treeFilterText" 
            placeholder="输入关键字过滤" 
            clearable 
            style="margin-bottom: 12px" 
          />
          <el-scrollbar height="calc(100vh - 260px)">
            <el-tree
              ref="treeRef"
              :data="regionTree"
              :props="{ label: 'name', children: 'children' }"
              node-key="id"
              highlight-current
              :filter-node-method="filterNode"
              @node-click="handleNodeClick"
            />
          </el-scrollbar>
        </el-card>
      </el-col>

      <!-- 右侧学校树 -->
      <el-col :span="19">
        <!-- 工具栏 -->
        <el-card shadow="never" class="toolbar-wrapper">
          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-button type="primary" :icon="Plus" @click="handleAdd">新增学校</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :icon="Refresh" @click="handleRefresh">刷新</el-button>
            </el-col>
          </el-row>
        </el-card>

        <!-- 学校树表格 -->
        <el-card shadow="never" style="margin-top: 12px">
          <SchoolTree
            ref="schoolTreeRef"
            :region-id="selectedRegionId"
            @edit="handleEdit"
            @bind-grade="handleBindGrade"
            @bind-class="handleBindClass"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- 新增/编辑学校弹窗 -->
    <SchoolDialog 
      ref="schoolDialogRef" 
      :region-tree="regionTree" 
      @success="handleRefresh" 
    />

    <!-- 挂载年级弹窗 -->
    <BindGradeDialog 
      ref="bindGradeDialogRef" 
      @success="handleRefresh" 
    />

    <!-- 挂载班级弹窗 -->
    <BindClassDialog 
      ref="bindClassDialogRef" 
      @success="handleRefresh" 
    />
  </div>
</template>

<script setup>
/**
 * 学校管理页面
 * @author pangu
 */
import request from '@/utils/request'
import { Plus, Refresh } from '@element-plus/icons-vue'
import { onMounted, ref, watch } from 'vue'
import SchoolTree from './components/SchoolTree.vue'
import SchoolDialog from './components/SchoolDialog.vue'
import BindGradeDialog from './components/BindGradeDialog.vue'
import BindClassDialog from './components/BindClassDialog.vue'

// 区域树相关
const treeRef = ref()
const treeFilterText = ref('')
const regionTree = ref([])
const selectedRegionId = ref(null)

// 组件引用
const schoolTreeRef = ref()
const schoolDialogRef = ref()
const bindGradeDialogRef = ref()
const bindClassDialogRef = ref()

// 监听树过滤
watch(treeFilterText, (val) => {
  treeRef.value?.filter(val)
})

// 树节点过滤
const filterNode = (value, data) => {
  if (!value) return true
  return data.name.includes(value)
}

// 获取区域树
const getRegionTree = async () => {
  const res = await request.get('/api/region/tree')
  if (res.code === 200) {
    regionTree.value = res.data
  }
}

// 树节点点击
const handleNodeClick = (data) => {
  selectedRegionId.value = data.id
}

// 新增学校
const handleAdd = () => {
  schoolDialogRef.value?.open()
}

// 编辑学校
const handleEdit = (row) => {
  schoolDialogRef.value?.open(row)
}

// 挂载年级
const handleBindGrade = (row) => {
  bindGradeDialogRef.value?.open(row)
}

// 挂载班级
const handleBindClass = (row) => {
  bindClassDialogRef.value?.open(row)
}

// 刷新
const handleRefresh = () => {
  schoolTreeRef.value?.loadSchoolTree()
}

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

<style scoped>
.app-container {
  padding: 16px;
}

.toolbar-wrapper {
  margin-bottom: 0;
}
</style>
