import { ref } from 'vue'
import { listAllSchool, getAllSchool } from "@/api/dz/school"
import { listCampus } from "@/api/dz/campus"
import { getClassesBySchoolId, getClassesByCampusId } from "@/api/dz/classes"

/**
 * 通用的学校、班级、校区、校区班级选择 Hook
 * @param {Object} options 配置选项
 * @param {Boolean} options.enableCampus 是否启用校区选择（默认false）
 * @param {Boolean} options.multipleClass 班级是否多选（默认false）
 * @param {Boolean} options.autoLoad 是否自动加载学校列表（默认false）
 */
export default function useSchoolClassSelect(options = {}) {
  const {
    enableCampus = false,
    multipleClass = false,
    autoLoad = false
  } = options

  // 学校相关
  const schoolOptions = ref([])
  const schoolClassOptions = ref([])
  
  // 校区相关
  const campusOptions = ref([])
  const campusClassOptions = ref([])

  // 表单数据
  const formData = ref({
    schoolId: null,
    schoolClassIds: multipleClass ? [] : null,
    campusId: null,
    campusClassIds: multipleClass ? [] : null
  })

  /** 加载学校列表 */
  function loadSchoolList(useGetAll = false) {
    if (useGetAll) {
      return getAllSchool().then(response => {
        schoolOptions.value = response.data || []
        return schoolOptions.value
      })
    }
    return listAllSchool({
      pageNum: 1,
      pageSize: 9999,
    }).then(response => {
      schoolOptions.value = response.data || []
      return schoolOptions.value
    })
  }

  /** 加载校区列表 */
  function loadCampusList() {
    if (!enableCampus) {
      return Promise.resolve([])
    }
    return listCampus().then(response => {
      campusOptions.value = response.rows || []
      return campusOptions.value
    })
  }

  /** 根据学校ID加载班级列表 */
  function loadClassesBySchoolId(schoolId) {
    if (!schoolId) {
      schoolClassOptions.value = []
      return Promise.resolve([])
    }
    return getClassesBySchoolId({ schoolId }).then(response => {
      schoolClassOptions.value = response.data || []
      return schoolClassOptions.value
    })
  }

  /** 根据校区ID加载班级列表 */
  function loadClassesByCampusId(campusId) {
    if (!enableCampus || !campusId) {
      campusClassOptions.value = []
      return Promise.resolve([])
    }
    return getClassesByCampusId({ campusId }).then(response => {
      campusClassOptions.value = response.data || []
      return campusClassOptions.value
    })
  }

  /** 学校变化处理 */
  function handleSchoolChange(schoolId) {
    // 如果选择了学校，清空校区（如果启用校区）
    if (schoolId && enableCampus) {
      formData.value.campusId = null
      formData.value.campusClassIds = multipleClass ? [] : null
      campusClassOptions.value = []
    } else if (!schoolId) {
      // 如果清空了学校，清空学校班级
      formData.value.schoolClassIds = multipleClass ? [] : null
      schoolClassOptions.value = []
      return
    }
    
    // 清空学校班级选择
    formData.value.schoolClassIds = multipleClass ? [] : null
    schoolClassOptions.value = []
    
    // 加载学校班级
    if (schoolId) {
      return loadClassesBySchoolId(schoolId)
    }
    return Promise.resolve([])
  }

  /** 校区变化处理 */
  function handleCampusChange(campusId) {
    if (!enableCampus) {
      return Promise.resolve([])
    }
    
    // 如果选择了校区，清空学校
    if (campusId) {
      formData.value.schoolId = null
      formData.value.schoolClassIds = multipleClass ? [] : null
      schoolClassOptions.value = []
    } else {
      // 如果清空了校区，清空校区班级
      formData.value.campusClassIds = multipleClass ? [] : null
      campusClassOptions.value = []
      return Promise.resolve([])
    }
    
    // 清空校区班级选择
    formData.value.campusClassIds = multipleClass ? [] : null
    campusClassOptions.value = []
    
    // 加载校区班级
    if (campusId) {
      return loadClassesByCampusId(campusId)
    }
    return Promise.resolve([])
  }

  /** 重置所有数据 */
  function reset() {
    formData.value = {
      schoolId: null,
      schoolClassIds: multipleClass ? [] : null,
      campusId: null,
      campusClassIds: multipleClass ? [] : null
    }
    schoolClassOptions.value = []
    campusClassOptions.value = []
  }

  // 自动加载
  if (autoLoad) {
    loadSchoolList()
    if (enableCampus) {
      loadCampusList()
    }
  }

  return {
    // 数据
    schoolOptions,
    schoolClassOptions,
    campusOptions,
    campusClassOptions,
    formData,
    
    // 方法
    loadSchoolList,
    loadCampusList,
    loadClassesBySchoolId,
    loadClassesByCampusId,
    handleSchoolChange,
    handleCampusChange,
    reset
  }
}

