<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>
          <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
        v-loading="dataListLoading"
        :summary-method="getSummaries"
        show-summary
        :row-style="changeRowColor"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column align="center" header-align="center" prop="schoolName" label="学校名称" />
        <el-table-column align="center" header-align="center" prop="peopleNum" label="人数" />
        <el-table-column align="center" header-align="center" prop="peopleRate" label="占比" />
      </el-table>
    </div>
  </div>
</template>

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

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

// 引用
const searchFormRef = ref()

// 状态
const dataList = ref<any[]>([])
const planList = ref<any[]>([])
const indexArray = 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 data = await getList()
    planList.value = data.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 = []
    indexArray.value = []
    const response = await getSchoolStatic(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/getSchoolStaticExport', queryForm, '按学校导出.xls')
  } catch (error: any) {
    message.error(error.msg || '导出失败')
  } finally {
    exportLoading.value = false
  }
}

// 改变行颜色
const changeRowColor = ({ row }: { row: any }) => {
  const specialSchools = [
    '智能装备学院',
    '智能制造学院',
    '信息服务学院',
    '交通运输学院',
    '医药康养学院',
    '招生就业处'
  ]
  if (specialSchools.includes(row.schoolName)) {
    return {
      color: 'red'
    }
  }
}

// 合计方法
const getSummaries = (param: any) => {
  const { columns, data } = param
  const sums: any[] = []
  columns.forEach((column: any, index: number) => {
    if (index === 0) {
      sums[index] = '总计'
    } else if (index === 1) {
      const values = data.map((item: any) => Number(item[column.property]))
      if (!values.every((value: any) => isNaN(value))) {
        sums[index] = values.reduce((prev: number, curr: any) => {
          const value = Number(curr)
          if (!isNaN(value)) {
            return prev + curr
          } else {
            return prev
          }
        }, 0) / 2
      } else {
        sums[index] = '--'
      }
    } else {
      sums[index] = '--'
    }
  })
  return sums
}

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

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

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