<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增班级"
    width="500px"
    :close-on-click-modal="false"
    destroy-on-close
  >
    <div style="margin-bottom: 16px;">
      <span style="font-weight: bold;">学校：</span>
      <span>{{ currentSchool?.schoolName }}</span>
    </div>
    
    <el-form ref="formRef" :model="form" label-width="80px">
      <el-form-item label="选择年级" prop="gradeId">
        <el-select v-model="form.gradeId" placeholder="请先选择年级" style="width: 100%">
          <el-option
            v-for="item in gradeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="选择班级" prop="classIds">
        <el-checkbox-group v-model="form.classIds">
          <el-row :gutter="10">
            <el-col :span="8" v-for="item in classOptions" :key="item.value">
              <el-checkbox :label="item.value" style="margin-bottom: 8px;">
                {{ item.label }}
              </el-checkbox>
            </el-col>
          </el-row>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitLoading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
/**
 * 新增班级弹窗
 * @author pangu
 */
import { ElMessage } from 'element-plus'
import { onMounted, ref } from 'vue'
import { addGradeClass, getClassOptions, getGradeOptions } from '@/api/pangu/school'

const emit = defineEmits(['success'])

const dialogVisible = ref(false)
const submitLoading = ref(false)
const formRef = ref(null)
const currentSchool = ref(null)
const gradeOptions = ref([])
const classOptions = ref([])

// 表单数据
const form = ref({
  schoolId: null,
  gradeId: '',
  classIds: []
})

// 获取年级选项
const fetchGradeOptions = async () => {
  try {
    const res = await getGradeOptions()
    if (res.code === 200) {
      gradeOptions.value = res.data
    }
  } catch (error) {
    console.error('获取年级选项失败:', error)
  }
}

// 获取班级选项
const fetchClassOptions = async () => {
  try {
    const res = await getClassOptions()
    if (res.code === 200) {
      classOptions.value = res.data
    }
  } catch (error) {
    console.error('获取班级选项失败:', error)
  }
}

// 打开弹窗
const open = (school) => {
  dialogVisible.value = true
  currentSchool.value = school
  form.value = {
    schoolId: school.id,
    gradeId: '',
    classIds: []
  }
}

// 提交表单
const handleSubmit = async () => {
  if (!form.value.gradeId) {
    ElMessage.warning('请选择年级')
    return
  }
  if (form.value.classIds.length === 0) {
    ElMessage.warning('请至少选择一个班级')
    return
  }
  
  submitLoading.value = true
  try {
    const res = await addGradeClass(form.value)
    if (res.code === 200) {
      ElMessage.success('添加班级成功')
      dialogVisible.value = false
      emit('success')
    }
  } catch (error) {
    console.error('添加班级失败:', error)
  } finally {
    submitLoading.value = false
  }
}

// 初始化
onMounted(() => {
  fetchGradeOptions()
  fetchClassOptions()
})

// 暴露方法给父组件
defineExpose({ open })
</script>
