<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-form :model="queryForm" inline class="mb-4" ref="searchFormRef">
        <el-form-item label="招生计划" prop="groupId">
          <el-select v-model="queryForm.groupId" filterable placeholder="请选择招生计划">
            <el-option
              v-for="item in planList"
              :key="item.id"
              :label="item.groupName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学制" prop="learnYear">
          <el-select v-model="queryForm.learnYear" filterable placeholder="请选择学制" clearable>
            <el-option
              v-for="item in majorYears"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否联院" prop="isUnion">
          <el-select v-model="queryForm.isUnion" filterable placeholder="请选择是否联院" clearable>
            <el-option key="0" label="否" value="0" />
            <el-option key="1" label="是" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleFilter">查询</el-button>
          <el-button type="primary" plain icon="Refresh" class="ml10" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="mb15">
        <el-button type="warning" plain icon="Download" :loading="exportLoading" @click="dataExportHandle">导出</el-button>
      </div>

      <el-table
        :data="dataList"
        border
        stripe
        :row-style="changeRowColor"
        v-loading="dataListLoading"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column prop="indexNum" header-align="center" align="center" label="序号">
          <template #default="scope">
            {{ scope.row.indexNum }}
          </template>
        </el-table-column>
        <el-table-column prop="deptCode" header-align="center" align="center" label="系部">
          <template #default="scope">
            {{ getDeptType(scope.row.deptCode) }}
          </template>
        </el-table-column>
        <el-table-column prop="majorName" header-align="center" align="center" label="所报专业" />
        <el-table-column prop="maxScore" header-align="center" align="center" label="最高分" />
        <el-table-column prop="minScore" header-align="center" align="center" label="最低分" />
        <el-table-column prop="avgScore" header-align="center" align="center" label="平均分" />
        <el-table-column prop="majorPeopleNum" header-align="center" align="center" label="专业人数" />
<!--        <el-table-column prop="avgScoreDB" header-align="center" align="center" label="均分对比" />-->
      </el-table>
    </div>
  </div>
</template>

<script setup lang="ts" name="recruitstudentsignup-juniorlneStatic">
import { ref, reactive, onMounted } from 'vue'
import { useTable } from '/@/hooks/table'
import { useMessage } from '/@/hooks/message'
import { useDict } from '/@/hooks/dict'
import { juniorlneStatic } from '/@/api/recruit/recruitstudentsignup'
import { getList } from '/@/api/recruit/recruitstudentplangroup'
import { getDeptListByLevelTwo } from '/@/api/basic/basicdept'
import {getDictsByTypes} from "/@/api/admin/dict";

// 消息提示 hooks
const message = useMessage()

// 字典
// const { useDict } = useDict()

// 引用
const searchFormRef = ref()

// 状态
const deptCodes = ref<any[]>([])
const planList = ref<any[]>([])
const dataList = ref<any[]>([])
const majorYears = ref<any[]>([])
const dataListLoading = ref(false)
const exportLoading = ref(false)

// 查询表单
const queryForm = reactive({
  groupId: '',
  learnYear: '',
  isUnion: ''
})

// 使用 table hook 获取样式
const { tableStyle, downBlobFile } = useTable()

const queryDictData=()=>{
  getDictsByTypes(['basic_major_years']).then((res:any)=>{
      majorYears.value = res.data.basic_major_years || []
  })
}
// 初始化
const init = async () => {
  try {
    queryDictData()

    const [deptResponse, planData] = await Promise.all([
      getDeptListByLevelTwo(),
      getList()
    ])

    deptCodes.value = deptResponse.data || []
    planList.value = planData.data || []

    if (planList.value.length > 0) {
      queryForm.groupId = planList.value[0].id
      getDataList()
    }
  } catch (error) {
    console.error('初始化失败', error)
  }
}

// 获取数据列表
const getDataList = async () => {
  try {
    dataListLoading.value = true
    dataList.value = []
    const response = await juniorlneStatic(queryForm)
    dataList.value = response.data || []
  } catch (error) {
    console.error('获取数据失败', error)
  } finally {
    dataListLoading.value = false
  }
}

// 导出
const dataExportHandle = async () => {
  try {
    exportLoading.value = true
    await downBlobFile('/recruit/recruitstudentsignup/juniorlneStaticExport', queryForm, '初中分数统计.xls')
  } catch (error: any) {
    message.error(error.msg || '导出失败')
  } finally {
    exportLoading.value = false
  }
}

// 改变行颜色
const changeRowColor = ({ row }: { row: any }) => {
  if (row.majorName === '合计') {
    return {
      color: 'red'
    }
  }
}

// 获取系部名称
const getDeptType = (type: string) => {
  const dept = deptCodes.value.find((item: any) => item.deptCode === type)
  return dept ? dept.deptName : ''
}

// 查询
const handleFilter = () => {
  getDataList()
}

// 重置查询
const resetQuery = () => {
  searchFormRef.value?.resetFields()
  if (planList.value.length > 0) {
    queryForm.groupId = planList.value[0].id
  }
  queryForm.learnYear = ''
  queryForm.isUnion = ''
  getDataList()
}

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