<!--
  -    Copyright (c) 2018-2025, cyweb All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  -
  -->

<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="getTabStaticDataList" style="width: 150px;">
            <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="getTabStaticDataList">查询</el-button>
          <el-button type="primary" plain icon="Refresh" class="ml10" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table
        ref="tableRef"
        :data="tableData"
        v-loading="dataListLoading"
        border
        stripe
        show-summary
        :summary-method="getSummaries"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column prop="groupId" label="招生计划" align="center" show-overflow-tooltip>
          <template #default="scope">
            {{ getPlanName(scope.row.groupId) }}
          </template>
        </el-table-column>
        <el-table-column prop="majorName" label="专业名称" align="center" show-overflow-tooltip />
        <el-table-column prop="wlx" label="未联系" align="center" show-overflow-tooltip />
        <el-table-column prop="yjbd" label="已经报到" align="center" show-overflow-tooltip />
        <el-table-column prop="tcbd" label="推迟报到" align="center" show-overflow-tooltip />
        <el-table-column prop="fqbd" label="放弃报到" align="center" show-overflow-tooltip />
        <el-table-column prop="fwlx" label="无法联系" align="center" show-overflow-tooltip />
      </el-table>
    </div>
  </div>
</template>

<script setup lang="ts" name="backSchoolCheckinStaticIndex">
import { ref, reactive, onMounted } from 'vue'
import { list } from '/@/api/recruit/recruitstudentplangroup'
import { getTabStaticDataList as getTabStaticDataListApi } from '/@/api/recruit/recruitstudentsignup'
// @ts-ignore
import global from '@/components/tools/commondict'
import { useTable } from '/@/hooks/table'

// 表格引用
const tableRef = ref()
const searchFormRef = ref()

// 数据
const planList = ref<any[]>([])
const tableData = ref<any[]>([])
const dataListLoading = ref(false)

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

// 获取计划名称
const getPlanName = (groupId: string) => {
  const item = planList.value.find(item => item.id === groupId)
  return item ? item.groupName : ''
}

// 表格样式
const { tableStyle } = useTable({ queryForm: queryForm, pageList: async () => ({ data: { records: [], total: 0 } }), createdIsNeed: false })

// 获取汇总数据
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: number) => {
          const value = Number(curr)
          if (!isNaN(value)) {
            return prev + curr
          } else {
            return prev
          }
        }, 0)
      } else {
        sums[index] = '--'
      }
    } else {
      sums[index] = '--'
    }
  })
  return sums
}

// 获取统计数据
const getTabStaticDataList = async () => {
  try {
    tableData.value = []
    dataListLoading.value = true
    const response = await getTabStaticDataListApi(queryForm)
    tableData.value = response.data || []
  } catch (error) {
    console.error('获取统计数据失败', error)
  } finally {
    dataListLoading.value = false
  }
}

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

// 初始化
const init = async () => {
  try {
    const data = await list()
    planList.value = data.data || []
    if (planList.value.length > 0) {
      queryForm.groupId = planList.value[0].id
      getTabStaticDataList()
    }
  } catch (error) {
    console.error('初始化失败', error)
  }
}

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

<style lang="scss" scoped>
</style>
