<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="handleFilter">
            <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="state.dataList"
        border
        stripe
        v-loading="state.loading"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column prop="majorName" header-align="center" align="center" label="专业" />
        <el-table-column prop="number" header-align="center" align="center" label="人数" />
        <el-table-column prop="rate" header-align="center" align="center" label="占比" />
      </el-table>
    </div>
  </div>
</template>

<script setup lang="ts" name="recruitprestudent-static">
import { ref, reactive, computed, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useUserInfo } from '/@/stores/userInfo'
import { BasicTableProps, useTable } from '/@/hooks/table'
import { useMessage } from '/@/hooks/message'
import { fetchListStatic } from '/@/api/recruit/recruitprestudent'
import { getList } from '/@/api/recruit/recruitstudentplangroup'
// @ts-ignore
import global from '/@/components/tools/commondict.vue'

// 使用 Pinia store
const userInfoStore = useUserInfo()
const { userInfos } = storeToRefs(userInfoStore)

// 创建权限对象
const permissions = computed(() => {
  const perms: Record<string, boolean> = {}
  userInfos.value.authBtnList.forEach((perm: string) => {
    perms[perm] = true
  })
  return perms
})

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

// 引用
const searchFormRef = ref()

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

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

// 表格状态
const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: queryForm,
  pageList: async (params: any) => {
    const response = await fetchListStatic(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 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 handleFilter = () => {
  getDataList()
}

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

// 导出
const dataExportHandle = async () => {
  try {
    exportLoading.value = true
    await downBlobFile(
      '/recruit/recruitprestudent/staticExport',
      queryForm,
      '预登记统计.xls'
    )
  } catch (error: any) {
    message.error(error.msg || '导出失败')
  } finally {
    exportLoading.value = false
  }
}

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