<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 clearable placeholder="请选择招生计划" @change="chanMajor">
            <el-option
              v-for="item in planList"
              :key="item.id"
              :label="item.groupName"
              :value="item.id"
            />
          </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="handleExport">导出</el-button>
      </div>

      <el-table
        :data="state.dataList"
        border
        stripe
        height="700px"
        v-loading="state.loading"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column width="150" prop="deptCode" header-align="center" align="center" label="学院">
          <template #default="scope">
            {{ global.getLabelValueByPropes(deptList, scope.row.deptCode, { key: 'deptCode', value: 'deptName' }) }}
          </template>
        </el-table-column>
        <el-table-column width="150" prop="majorCode" header-align="center" align="center" label="专业代码" />
        <el-table-column prop="majorName" header-align="center" align="center" label="专业名称" />
        <el-table-column width="80" prop="scoreLine" header-align="center" align="center" label="分数线" />
        <el-table-column width="80" prop="planNum" header-align="center" align="center" label="计划总数" />
        <el-table-column width="80" prop="recruitmentNum" header-align="center" align="center" label="拟招人数" />
        <el-table-column width="80" prop="hasNum" header-align="center" align="center" label="已招人数" />
        <el-table-column width="80" prop="boyNum" header-align="center" align="center" label="已招(男)" />
        <el-table-column width="80" prop="girlNum" header-align="center" align="center" label="已招(女)" />
        <el-table-column width="80" prop="cityFrom" header-align="center" align="center" label="市平台" />
        <el-table-column width="80" prop="schoolFrom" header-align="center" align="center" label="校平台" />
        <el-table-column width="80" prop="xyNum" header-align="center" align="center" label="剩余人数" />
      </el-table>
    </div>
  </div>
</template>

<script setup lang="ts" name="recruitstudentsignup-static">
import { ref, reactive, onMounted } from 'vue'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { useMessage } from '/@/hooks/message'
import { useDict } from '/@/hooks/dict'
import { list } from '/@/api/recruit/recruitstudentplangroup'
import { fetchListByStatic, listPlanByCondition as planMajor } from '/@/api/recruit/recruitstudentplan'
import { getDeptList } from '/@/api/basic/basicclass'
// @ts-ignore
import global from '/@/components/tools/commondict.vue'

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

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

// 引用
const searchFormRef = ref()

// 状态
const planList = ref<any[]>([])
const eduList = ref<any[]>([])
const planMajorList = ref<any[]>([])
const deptList = ref<any[]>([])
const exportLoading = ref(false)

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

// 表格状态
const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: queryForm,
  pageList: async (params: any) => {
    const response = await fetchListByStatic(params)
    return {
      data: {
        records: response.data || [],
        total: response.data?.length || 0
      }
    }
  },
  createdIsNeed: false
})

// 使用 table hook
const { getDataList, tableStyle, downBlobFile } = useTable(state)

// 初始化
const init = async () => {
  try {
    // 查询二级学院信息
    const deptData = await getDeptList()
    deptList.value = deptData.data || []
    deptList.value.push({ deptCode: '小计(高中/职技校)', deptName: '小计(高中/职技校)' })
    deptList.value.push({ deptCode: '小计(初中)', deptName: '小计(初中)' })
    deptList.value.push({ deptCode: '小计(初中_联院大专)', deptName: '小计(初中_联院大专)' })
    deptList.value.push({ deptCode: '合计', deptName: '合计' })

    // 获取招生计划列表
    const planData = await list()
    planList.value = planData.data || []
    if (planList.value.length > 0) {
      queryForm.groupId = planList.value[0].id
      getDataList()
      chanMajor()
    }

    // 获取字典数据
    const eduRes = await getTypeValue('education_type')
    eduList.value = eduRes.data || []
  } catch (error) {
    console.error('初始化失败', error)
  }
}

// 导出
const handleExport = async () => {
  try {
    exportLoading.value = true
    await downBlobFile('/recruit/recruitstudentsignup/exportExcel', queryForm, '招生统计.xls')
  } catch (error: any) {
    message.error(error.msg || '导出失败')
  } finally {
    exportLoading.value = false
  }
}

// 切换专业
const chanMajor = async () => {
  try {
    planMajorList.value = []
    if (queryForm.groupId) {
      const data = await planMajor({ groupId: queryForm.groupId })
      planMajorList.value = data.data || []
    }
  } catch (error) {
    console.error('获取专业列表失败', error)
  }
}

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

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

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