<template>
  <!-- 成绩查询 首页 -->
  <div class="page_report_main" ref="mainContent">
    <div class="page_filter">
      <FiltersItem :data="filtersData" @select="handleSelectChange" />
    </div>
    <router-view />
    <div class="report_bottom">
      <div class="bottom_no_more">
        没有更多了，<span @click="goToPageTop">回到顶部</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import FiltersItem from '@/components/FiltersItem.vue'
import { findCommonSelectList } from '@/api/analysis'
import { onMounted, ref } from 'vue'
import { useAnalysisStore } from '@/store/analysis'

const analysisStore = useAnalysisStore()
const mainContent = ref<HTMLElement | null>(null)

// 子组件实例类型定义
interface ChildComponent {
  PageInit: () => void
}
const childView = ref<ChildComponent | null>(null)

// 强类型定义
interface FilterOption {
  label: string
  value: string
  [key: string]: any
}

interface FilterItem {
  label: string
  type: 'subjectName' | 'schoolName' | 'registrationType' | 'scoreType' | 'classType' | 'className'
  list: FilterOption[]
  value: string
}

interface SubjectItem {
  subjectName: string
  subjectCode: string
  subjectId: string
  isTotal: boolean
  subjectGroupType: number
  selectSchoolVoList: SchoolItem[]
  subjectGroupCodes: string
}

interface SchoolItem {
  schoolId: string
  schoolName: string
  schoolLevel: string
  schoolGroupId: string
  selectStatusVoList: StatusItem[]
  schoolGroupNames: string
}

interface StatusItem {
  statusName: string
  statusGroupType: string
  statusGroupId: string
  examCommonSelectScoreList: ScoreTypeItem[]
  statusGroupNames: string
}

interface ScoreTypeItem {
  scoreType: string
  typeName: string
  selectClassVoList: ClassTypeItem[]
}

interface ClassTypeItem {
  typeName: string
  classType: string
  selectInfoVoList: ClassItem[]
}

interface ClassItem {
  className: string
  classLevel: string
  classGroupId: string
  classIdCode: string
  classCode: string
  classGroupNames: string
}

interface FilterParams {
  examId: string
  subjectCode: string
  subjectName: string
  subjectId: string
  subjectGroupType: number
  isTotal: boolean
  subjectGroupCodes: string
  schoolId: string
  schoolLevel: string
  schoolGroupId: string
  schoolGroupName: string | null
  schoolName: string | null
  schoolGroupNames: string
  registrationType: string
  registrationName: string | null
  registrationGroupId: string
  statusGroupNames: string
  scoreType: string
  classType: string
  classIdCode: string
  classLevel: string | number
  classGroupId: string
  classGroupName: string
  classGroupNames: string[]
}

// 筛选数据
const filtersData = ref<FilterItem[]>([
  { label: '科目名称', type: 'subjectName', list: [], value: '' },
  { label: '学校名称', type: 'schoolName', list: [], value: '' },
  { label: '学生类型', type: 'registrationType', list: [], value: '' },
  { label: '分数类型', type: 'scoreType', list: [], value: '' },
  { label: '班级类型', type: 'classType', list: [], value: '' },
  { label: '班级名称', type: 'className', list: [], value: '' },
])

// 筛选联动工具函数
const updateBySchool = (school: FilterOption) => {
  if (!school) return
  const registrationTypeList = school.selectStatusVoList.map((item: any) => ({
    label: item.statusName,
    value: `${item.statusGroupType || ''}${item.statusGroupId || ''}${item.statusName || ''}`,
    statusGroupType: item.statusGroupType,
    statusName: item.statusName,
    statusGroupId: item.statusGroupId,
    examCommonSelectScoreList: item.examCommonSelectScoreList,
    statusGroupNames: item.statusGroupNames,
  }))
  filtersData.value[2].list = registrationTypeList
  filtersData.value[2].value = registrationTypeList[0]?.value || ''
  updateByStatus(registrationTypeList[0])
}

const updateByStatus = (status: FilterOption) => {
  if (!status) return
  const scoreTypeList = status.examCommonSelectScoreList.map((item: any) => ({
    label: item.typeName,
    value: item.scoreType,
    selectClassVoList: item.selectClassVoList,
  }))
  filtersData.value[3].list = scoreTypeList
  filtersData.value[3].value = scoreTypeList[0]?.value || ''
  updateByScoreType(scoreTypeList[0])
}

const updateByScoreType = (type: FilterOption) => {
  if (!type) return
  const classTypeList = type.selectClassVoList.map((item: any) => ({
    label: item.typeName,
    value: item.classType,
    selectInfoVoList: item.selectInfoVoList,
  }))
  filtersData.value[4].list = classTypeList
  filtersData.value[4].value = classTypeList[0]?.value || ''
  updateByClassType(classTypeList[0])
}

const updateByClassType = (classType: FilterOption) => {
  if (!classType) return
  const classList = (classType.selectInfoVoList || []).map((item: any) => ({
    label: item.className,
    value: `${item.classLevel || ''}${item.classGroupId || ''}${item.classIdCode || ''}`,
    classLevel: item.classLevel,
    className: item.className,
    classCode: item.classCode,
    classIdCode: item.classIdCode,
    classGroupId: item.classGroupId,
    classGroupNames: item.classGroupNames,
  }))
  filtersData.value[5].list = classList
  filtersData.value[5].value = classList[0]?.value || ''
}

// 构建筛选参数并刷新子页面
const buildAndSaveFilterParams = () => {
  const courseObj = filtersData.value[0].list.find(item => item.value === filtersData.value[0].value)
  const schoolObj = filtersData.value[1].list.find(item => item.value === filtersData.value[1].value)
  const statusObj = filtersData.value[2].list.find(item => item.value === filtersData.value[2].value)
  const classObj = filtersData.value[5].list.find(item => item.value === filtersData.value[5].value)

  if (!courseObj || !schoolObj || !statusObj) return

  const filterObject: FilterParams = {
    examId:'2036963589738971137',
    subjectCode: courseObj.subjectCode,
    subjectName: courseObj.subjectName,
    subjectId: courseObj.subjectId,
    subjectGroupType: courseObj.subjectGroupType,
    isTotal: courseObj.isTotal,
    subjectGroupCodes: courseObj.subjectGroupCodes,

    schoolId: schoolObj.schoolId,
    schoolLevel: schoolObj.schoolLevel,
    schoolGroupId: schoolObj.schoolGroupId,
    schoolGroupName: (schoolObj.schoolLevel === '0' || schoolObj.schoolLevel === '2') ? null : schoolObj.schoolName,
    schoolName: schoolObj.schoolLevel === '2' ? schoolObj.schoolName : null,
    schoolGroupNames: schoolObj.schoolGroupNames,

    registrationType: statusObj.statusGroupType,
    registrationName: statusObj.statusName === '全部' ? null : statusObj.statusName,
    registrationGroupId: statusObj.statusGroupId,
    statusGroupNames: statusObj.statusGroupNames,

    scoreType: filtersData.value[3].value,
    classType: filtersData.value[4].value,

    classIdCode: classObj?.classIdCode || '',
    classLevel: classObj?.classLevel || 0,
    classGroupId: classObj?.classGroupId || '',
    classGroupName: classObj?.className || '',
    classGroupNames: classObj?.classGroupNames || [],
  }

  analysisStore.setFilterObject(filterObject)
}

// 初始化加载
const getCommonSelectList = async () => {
  try {
    const res = await findCommonSelectList({ aiExamId: '2036963589738971137' })
    if (res.code !== 200 || !res.data?.length) return

    const subjectList: FilterOption[] = (res.data as SubjectItem[]).map(item => ({
      label: item.subjectName,
      value: item.subjectCode,
      subjectName: item.subjectName,
      subjectId: item.subjectId,
      subjectCode: item.subjectCode,
      isTotal: item.isTotal,
      subjectGroupType: item.subjectGroupType,
      isTotalScore: item.subjectGroupType === 1,
      isMulCourse: item.subjectGroupType === 1,
      selectSchoolVoList: item.selectSchoolVoList,
      subjectGroupCodes: item.subjectGroupCodes,
    }))

    if (!subjectList.length) return
    filtersData.value[0].list = subjectList
    filtersData.value[0].value = subjectList[0].value

    const schoolList = subjectList[0].selectSchoolVoList.map((item: any) => ({
      label: item.schoolName,
      value: `${item.schoolLevel || ''}${item.schoolGroupId || ''}${item.schoolId || ''}`,
      schoolId: item.schoolId,
      schoolName: item.schoolName,
      schoolLevel: item.schoolLevel,
      schoolGroupId: item.schoolGroupId,
      selectStatusVoList: item.selectStatusVoList,
      schoolGroupNames: item.schoolGroupNames,
    }))

    filtersData.value[1].list = schoolList
    filtersData.value[1].value = schoolList[0]?.value || ''
    updateBySchool(schoolList[0])
    buildAndSaveFilterParams()
  } catch (err) {
    console.error('获取筛选数据失败：', err)
  }
}

// 筛选切换
const handleSelectChange = (index: number, value: string) => {
  filtersData.value[index].value = value
  const selectedItem = filtersData.value[index].list.find(item => item.value === value)
  if (!selectedItem) return

  const { type } = filtersData.value[index]

  if (type === 'subjectName') {
    const schoolList = selectedItem.selectSchoolVoList.map((item: any) => ({
      label: item.schoolName,
      value: `${item.schoolLevel || ''}${item.schoolGroupId || ''}${item.schoolId || ''}`,
      schoolId: item.schoolId,
      schoolName: item.schoolName,
      schoolLevel: item.schoolLevel,
      schoolGroupId: item.schoolGroupId,
      selectStatusVoList: item.selectStatusVoList,
      schoolGroupNames: item.schoolGroupNames,
    }))
    filtersData.value[1].list = schoolList
    filtersData.value[1].value = schoolList[0]?.value || ''
    updateBySchool(schoolList[0])
  }

  if (type === 'schoolName') updateBySchool(selectedItem)
  if (type === 'registrationType') updateByStatus(selectedItem)
  if (type === 'scoreType') updateByScoreType(selectedItem)
  if (type === 'classType') updateByClassType(selectedItem)

  buildAndSaveFilterParams()
}
// 回到顶部
const goToPageTop = () => {
  window.scrollTo({ top: 0, behavior: 'smooth' })
}

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

<style lang="scss" scoped>
.page_report_main {
  width: 100%;
}

.report_bottom {
  width: 100%;
  padding: 20px 0;
  text-align: center;
}

.bottom_no_more {
  font-size: 14px;
  color: #666;

  span {
    color: #2e64fa;
    font-weight: 500;
    cursor: pointer;
  }
}
</style>