<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <header style="font-size: 40px; text-align: center; margin-bottom: 20px;">
        历年常州地区初中生分数段人数统计(本省本市、初中生源)
      </header>

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

      <el-table
        :data="dataList"
        border
        stripe
        v-loading="dataListLoading"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column prop="socre" header-align="center" align="center" label="分数段" />
        <el-table-column
          v-for="(item, index) in headList"
          :key="index"
          :prop="item.year"
          :label="item.year"
          header-align="center"
        >
          <el-table-column
            :prop="item.propOne"
            header-align="center"
            align="center"
            label="人数"
          />
          <el-table-column
            :prop="item.propTwo"
            header-align="center"
            align="center"
            label="占比"
          />
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

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

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

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

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

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

// 初始化
const init = async () => {
  try {
    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 inSchoolSocreStatic(queryForm)
    if (response.data && response.data.length > 0) {
      response.data.forEach((e: any) => {
        dataList.value.push(e.map)
      })
      headList.value = response.data[0].list || []
    }
  } catch (error) {
    console.error('获取数据失败', error)
  } finally {
    dataListLoading.value = false
  }
}

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

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