<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="项目类型" prop="projectTypeFilter">
              <el-select v-model="projectTypeFilter" placeholder="请选择项目类型" clearable @change="handleProjectTypeFilterChange">
                <el-option
                  v-for="dict in game_project_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="项目" prop="projectId">
              <el-select v-model="queryParams.projectId" placeholder="请选择项目" clearable>
                <el-option
                  v-for="project in filteredProjectList"
                  :key="project.projectId"
                  :label="project.projectName"
                  :value="project.projectId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="组别" prop="groupName">
              <el-input v-model="queryParams.groupName" placeholder="请输入组别名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:gameEventGroup:add']"> 新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:gameEventGroup:edit']"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:gameEventGroup:remove']"
              >删除
            </el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:gameEventGroup:export']">导出 </el-button>
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="gameEventGroupList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column label="组别id" align="center" prop="groupId" v-if="columns[0].visible" />
        <el-table-column label="项目类型" align="center" v-if="columns[1].visible">
          <template #default="scope">
            <dict-tag :options="game_project_type" :value="getProjectTypeByProjectId(scope.row.projectId) || ''" />
          </template>
        </el-table-column>
        <el-table-column label="项目" align="center" v-if="columns[2].visible">
          <template #default="scope">
            {{ getProjectNameByProjectId(scope.row.projectId) }}
          </template>
        </el-table-column>
        <el-table-column label="组别" align="center" prop="groupName" v-if="columns[3].visible" />
        <el-table-column label="符合条件人数" align="center" prop="eligibleAthleteCount" v-if="columns[11].visible" />
        <el-table-column label="人/组" align="center" prop="personNum" v-if="columns[4].visible" />
        <el-table-column label="组数" align="center" prop="includeGroupNum" v-if="columns[5].visible" />
        <el-table-column label="道数" align="center" prop="trackNum" v-if="columns[6].visible" />
        <el-table-column label="场地数量" align="center" prop="fieldNum" v-if="columns[7].visible" />
        <el-table-column label="每组用时(分钟)" align="center" prop="duration" v-if="columns[8].visible" />
        <el-table-column label="比赛时间" align="center" v-if="columns[9].visible">
          <template #default="scope">
            {{ formatFullDateTime(scope.row.beginTime) }} ~ {{ formatFullDateTime(scope.row.endTime) }}
            <!-- {{ scope.row.beginTime }} ~ {{ scope.row.endTime }} -->
          </template>
        </el-table-column>
        <el-table-column label="成员性别" align="center" prop="memberGender" v-if="columns[10].visible">
          <template #default="scope">
            <dict-tag :options="sys_group_sex" :value="scope.row.memberGender" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="分组" placement="top">
              <el-button link type="success" icon="Grid" @click="handleGroup(scope.row)" v-hasPermi="['system:gameEventGroup:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:gameEventGroup:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:gameEventGroup:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 添加或修改赛事分组对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="900px" append-to-body>
      <el-form ref="gameEventGroupFormRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="项目类型" prop="projectTypeFilter">
              <el-select
                v-model="formProjectTypeFilter"
                placeholder="请选择项目类型"
                style="width: 100%"
                @change="handleFormProjectTypeFilterChange"
                :disabled="!!form.groupId"
              >
                <el-option
                  v-for="dict in game_project_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目" prop="projectId">
              <el-select
                v-model="form.projectId"
                placeholder="请选择项目"
                style="width: 100%"
                @change="handleFormProjectChange"
                :disabled="!formProjectTypeFilter || !!form.groupId"
              >
                <el-option
                  v-for="project in filteredFormProjectList"
                  :key="project.projectId"
                  :label="project.projectName"
                  :value="project.projectId"
                />
              </el-select>
              <div v-if="form.projectId" style="margin-top: 5px;">
                <span style="font-size: 13px; color: #606266;">当前参赛人数：</span>
                <el-link type="primary" :underline="false" @click="handleViewAthletes" style="font-size: 13px; font-weight: bold;">
                  {{ athleteCount }} 人
                </el-link>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="组别名称" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入组别名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成员性别" prop="memberGender">
              <el-select v-model="form.memberGender" placeholder="请选择性别" style="width: 100%">
                <el-option
                  v-for="dict in sys_group_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="人/组" prop="personNum">
              <el-input-number v-model="form.personNum" :min="1" placeholder="请输入每组人数" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组数" prop="includeGroupNum">
              <el-input-number v-model="form.includeGroupNum" :min="1" placeholder="请输入组数" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="trackNum">
              <template #label>
                <span style="display: inline-flex; align-items: center;">
                  <span>{{ form.trackDisplayMode === '1' ? '序号' : '道数' }}</span>
                  <el-icon 
                    style="margin-left: 4px; cursor: pointer; color: #409eff;" 
                    @click="form.trackDisplayMode = form.trackDisplayMode === '1' ? '0' : '1'"
                  >
                    <ArrowDown />
                  </el-icon>
                </span>
              </template>
              <el-input-number v-model="form.trackNum" :min="1" placeholder="请输入数值" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="场地数量" prop="fieldNum">
              <el-input-number v-model="form.fieldNum" :min="1" placeholder="请输入场地数量" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="每组用时(分钟)" prop="duration">
              <el-input-number v-model="form.duration" :min="1" placeholder="请输入每组用时" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组别开始时间" prop="beginTime">
              <el-date-picker
                v-model="form.beginTime"
                type="datetime"
                placeholder="请选择开始时间"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                :disabled="!form.projectId"
                :disabled-date="disabledDate"
              />
            </el-form-item>
        </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="组别结束时间" prop="endTime">
              <el-input 
                v-model="calculatedEndTime" 
                placeholder="自动计算" 
                disabled 
                style="width: 100%" 
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 运动员列表对话框组件 -->
    <athlete-list-dialog
      v-model:visible="athleteDialogVisible"
      :project-id="athleteProjectId"
      :event-id="form.eventId"
    />
  </div>
</template>

<script setup name="GameEventGroup" lang="ts">
import { nextTick, ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { listGameEventGroup, getGameEventGroup, delGameEventGroup, addGameEventGroup, updateGameEventGroup } from '@/api/system/gameEventGroup';
import { listGameEventProject } from '@/api/system/gameEventProject';
import { listGameAthlete, getAthleteCount as getAthleteCountApi } from '@/api/system/gameAthlete';
import AthleteListDialog from '@/components/AthleteListDialog/index.vue';
import { GameEventGroupVO, GameEventGroupQuery, GameEventGroupForm } from '@/api/system/gameEventGroup/types';
import { GameEventProjectVO } from '@/api/system/gameEventProject/types';
import { useGameEventStore } from '@/store/modules/gameEvent';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const gameEventStore = useGameEventStore();
const router = useRouter();

// 字典数据
const { game_project_type, sys_group_sex } = toRefs<any>(proxy?.useDict('game_project_type', 'sys_group_sex'));

const gameEventGroupList = ref<GameEventGroupVO[]>([]);
const projectList = ref<GameEventProjectVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

// 参赛人数及运动员列表相关
const athleteCount = ref(0);
const athleteDialogVisible = ref(false);
const athleteProjectId = ref<string | number>('');

// 项目类型过滤器（用于查询和表单，不存储在数据中）
const projectTypeFilter = ref<string>('');
const formProjectTypeFilter = ref<string>('');

// 列显隐数据
const columns = ref<FieldOption[]>([
  { key: 0, label: '组别id', visible: false },
  { key: 1, label: '项目类型', visible: true },
  { key: 2, label: '项目', visible: true },
  { key: 3, label: '组别', visible: true },
  { key: 4, label: '人/组', visible: true },
  { key: 5, label: '组数', visible: true },
  { key: 6, label: '道数', visible: true },
  { key: 7, label: '场地数量', visible: true },
  { key: 8, label: '每组用时(分钟)', visible: true },
  { key: 9, label: '比赛时间', visible: true },
  { key: 10, label: '成员性别', visible: true },
  { key: 11, label: '符合条件人数', visible: true },
]);

const queryFormRef = ref<ElFormInstance>();
const gameEventGroupFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: GameEventGroupForm = {
  groupId: undefined,
  eventId: undefined,
  groupName: undefined,
  projectList: undefined,
  selectedProjects: [],
  memberGender: undefined,
  sortOrder: undefined,
  sortRule: undefined,
  status: undefined,
  remark: undefined,
  includeGroupNum: undefined,
  projectId: undefined,
  personNum: undefined,
  beginTime: undefined,
  endTime: undefined,
  trackNum: undefined,
  fieldNum: undefined,
  duration: undefined,
  trackDisplayMode: '0'
};

const data = reactive<PageData<GameEventGroupForm, GameEventGroupQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: undefined,
    isAsc: undefined,
    projectId: undefined,
    groupName: undefined
  },
  rules: {
    projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
    groupName: [{ required: true, message: '组别名称不能为空', trigger: 'blur' }],
    memberGender: [{ required: true, message: '成员性别不能为空', trigger: 'change' }],
    personNum: [{ required: true, message: '每组人数不能为空', trigger: 'blur' }],
    includeGroupNum: [{ required: true, message: '组数不能为空', trigger: 'blur' }],
    trackNum: [{ required: true, message: '道数不能为空', trigger: 'blur' }],
    fieldNum: [{ required: true, message: '场地数量不能为空', trigger: 'blur' }],
    duration: [{ required: true, message: '每组用时不能为空', trigger: 'blur' }],
    beginTime: [{ required: true, message: '开始时间不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

// 过滤后的项目列表（用于查询）
const filteredProjectList = computed(() => {
  if (!projectTypeFilter.value) return projectList.value;
  return projectList.value.filter(project => project.projectType === projectTypeFilter.value);
});

// 过滤后的项目列表（用于表单）
const filteredFormProjectList = computed(() => {
  if (!formProjectTypeFilter.value) return [];
  return projectList.value.filter(project => project.projectType === formProjectTypeFilter.value);
});

// 根据项目ID获取项目类型
const getProjectTypeByProjectId = (projectId: string | number) => {
  if (!projectId) return '';
  const project = projectList.value.find(p => p.projectId === projectId);
  return project?.projectType || '';
};

// 根据项目ID获取项目名称
const getProjectNameByProjectId = (projectId: string | number) => {
  if (!projectId) return '';
  const project = projectList.value.find(p => p.projectId === projectId);
  return project?.projectName || '';
};

// 根据项目ID获取项目开始时间
const getProjectStartTimeByProjectId = (projectId: string | number) => {
  if (!projectId) return '';
  const project = projectList.value.find(p => p.projectId === projectId);
  if (project && project.startTime) {
    try {
      const startDate = new Date(project.startTime);
      if (!isNaN(startDate.getTime())) {
        const year = startDate.getFullYear();
        const month = String(startDate.getMonth() + 1).padStart(2, '0');
        const day = String(startDate.getDate()).padStart(2, '0');
        const hours = String(startDate.getHours()).padStart(2, '0');
        const minutes = String(startDate.getMinutes()).padStart(2, '0');
        const seconds = String(startDate.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      }
    } catch (error) {
      console.warn('项目开始时间解析失败:', error);
    }
  }
  return '';
};

// 计算预计结束时间
const calculatedEndTime = computed(() => {
  if (!form.value.beginTime || !form.value.duration || !form.value.includeGroupNum) {
    return '';
  }

  const beginTime = new Date(form.value.beginTime);
  if (isNaN(beginTime.getTime())) return '';
  
  const totalMinutes = form.value.duration * form.value.includeGroupNum;
  const endTime = new Date(beginTime.getTime() + totalMinutes * 60 * 1000);

  const year = endTime.getFullYear();
  const month = String(endTime.getMonth() + 1).padStart(2, '0');
  const day = String(endTime.getDate()).padStart(2, '0');
  const hours = String(endTime.getHours()).padStart(2, '0');
  const minutes = String(endTime.getMinutes()).padStart(2, '0');
  const seconds = String(endTime.getSeconds()).padStart(2, '0');
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
});

// 禁用日期（限制在项目时间范围内）
const disabledDate = (time: Date) => {
  if (!form.value.projectId) return false;
  const project = projectList.value.find(p => p.projectId === form.value.projectId);
  if (!project || !project.startTime || !project.endTime) return false;

  const projectStart = new Date(project.startTime);
  const projectEnd = new Date(project.endTime);

  // 禁用项目时间范围外的日期
  return time <= projectStart || time >= projectEnd;
};

// 格式化完整日期时间
const formatFullDateTime = (dateTime: string | null | undefined): string => {
  if (!dateTime) return '-';
  
  // 增加对空字符串的检查
  if (dateTime === '') return '-';
  
  let date: Date;
  try {
    // 尝试解析时间字符串
    date = new Date(dateTime);
    if (isNaN(date.getTime())) {
      return '-';
    }
  } catch (error) {
    console.warn('时间格式化失败:', error);
    return '-';
  }
  
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

/** 查询赛事分组列表 */
const getList = async () => {
  loading.value = true;
  const res = await listGameEventGroup(queryParams.value);
  gameEventGroupList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

// 获取赛事项目列表
const getProjectList = async () => {
  const res = await listGameEventProject({
    pageNum: 1,
    pageSize: 1000,
    orderByColumn: '',
    isAsc: ''
  });
  projectList.value = res.rows;
};

// 查询条件中项目类型变化
const handleProjectTypeFilterChange = () => {
  queryParams.value.projectId = undefined;
};

// 表单中项目类型变化
const handleFormProjectTypeFilterChange = () => {
  form.value.projectId = undefined;
  form.value.beginTime = undefined;
  form.value.endTime = undefined;
  form.value.trackNum = undefined;
  form.value.fieldNum = undefined;
  form.value.duration = undefined;

  // 重新获取项目列表以更新过滤
  getProjectList();
};

// 表单中项目变化
const handleFormProjectChange = () => {
  if (form.value.projectId) {
    const selectedProject = projectList.value.find(p => p.projectId === form.value.projectId);
    if (selectedProject) {
      // 可以在这里设置一些默认值或者进行其他处理
    }
    getAthleteCount();
  } else {
    athleteCount.value = 0;
  }
};

/** 获取运动员人数 */
const getAthleteCount = async () => {
  if (!form.value.projectId) return;
  const res = await getAthleteCountApi(form.value.projectId, form.value.eventId);
  athleteCount.value = res.data || 0;
};

/** 查看运动员列表 */
const handleViewAthletes = () => {
  if (athleteCount.value === 0) return;
  athleteProjectId.value = form.value.projectId!;
  athleteDialogVisible.value = true;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  formProjectTypeFilter.value = '';
  gameEventGroupFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  projectTypeFilter.value = '';
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: GameEventGroupVO[]) => {
  ids.value = selection.map((item) => item.groupId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加赛事分组';
  // 获取项目列表
  nextTick(() => {
    getProjectList();
    // 监听项目选择变化，设置默认开始时间
    watch(() => form.value.projectId, (newProjectId) => {
      if (newProjectId && !form.value.beginTime) {
        form.value.beginTime = getProjectStartTimeByProjectId(newProjectId);
      }
    });
  });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: GameEventGroupVO) => {
  reset();
  const _groupId = row?.groupId || ids.value[0];
  const res = await getGameEventGroup(_groupId);
  Object.assign(form.value, res.data);
  
  // 获取项目参赛人数
  getAthleteCount();

  // 根据项目ID设置项目类型过滤器
  if (res.data.projectId) {
    const project = projectList.value.find(p => p.projectId === res.data.projectId);
    if (project) {
      formProjectTypeFilter.value = project.projectType;
    }
  }

  // 处理时间字段的回显，增加安全检查
  if (res.data.beginTime) {
    try {
      // 尝试解析时间字符串
      const date = new Date(res.data.beginTime);
      if (!isNaN(date.getTime())) {
        // 如果解析成功，转换为正确的格式
        form.value.beginTime = formatFullDateTime(date.toString());
        // form.value.beginTime = date.toISOString().replace('T', ' ');
      } else {
        // 如果解析失败，设置为默认值
        console.warn('开始时间格式无效:', res.data.beginTime);
        form.value.beginTime = '';
      }
    } catch (error) {
      console.warn('开始时间解析失败:', error);
      form.value.beginTime = '';
    }
  }
  
  if (res.data.endTime) {
    try {
      // 尝试解析时间字符串
      const date = new Date(res.data.endTime);
      if (!isNaN(date.getTime())) {
        // 如果解析成功，转换为正确的格式
        form.value.endTime = formatFullDateTime(date.toString());
      } else {
        // 如果解析失败，设置为默认值
        console.warn('结束时间格式无效:', res.data.endTime);
        form.value.endTime = '';
      }
    } catch (error) {
      console.warn('结束时间解析失败:', error);
      form.value.endTime = '';
    }
  }

  dialog.visible = true;
  dialog.title = '修改赛事分组';
  // 获取项目列表
  nextTick(() => {
    getProjectList();
  });
};

/** 提交按钮 */
const submitForm = () => {
  gameEventGroupFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      // 校验组数和道数的关系
      if (form.value.includeGroupNum && form.value.trackNum && form.value.personNum) {
        const totalCapacity = form.value.includeGroupNum * form.value.trackNum;
        if (totalCapacity < form.value.personNum) {
          const recommendedGroupNum = Math.ceil(form.value.personNum / form.value.trackNum);
          proxy?.$modal.msgError(`组数过小请重新输入，推荐组数：${recommendedGroupNum}`);
          return;
        }
      }

      // 自动计算结束时间并赋值
      if (form.value.beginTime && form.value.duration && form.value.includeGroupNum) {
        try {
          const beginTime = new Date(form.value.beginTime);
          if (isNaN(beginTime.getTime())) {
            proxy?.$modal.msgError('开始时间格式无效');
            return;
          }
          
          const totalMinutes = form.value.duration * form.value.includeGroupNum;
          const endTime = new Date(beginTime.getTime() + totalMinutes * 60 * 1000);
          
          const year = endTime.getFullYear();
          const month = String(endTime.getMonth() + 1).padStart(2, '0');
          const day = String(endTime.getDate()).padStart(2, '0');
          const hours = String(endTime.getHours()).padStart(2, '0');
          const minutes = String(endTime.getMinutes()).padStart(2, '0');
          const seconds = String(endTime.getSeconds()).padStart(2, '0');
          
          form.value.endTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        } catch (error) {
          proxy?.$modal.msgError('计算结束时间失败');
          return;
        }
      }

      // 验证时间范围
      if (form.value.beginTime && form.value.endTime) {
        try {
          const beginTime = new Date(form.value.beginTime);
          const endTime = new Date(form.value.endTime);
          
          if (isNaN(beginTime.getTime()) || isNaN(endTime.getTime())) {
            proxy?.$modal.msgError('时间格式无效');
            return;
          }
          
          if (beginTime >= endTime) {
            proxy?.$modal.msgError('组别结束时间必须晚于开始时间');
            return;
          }

          // 验证组别时间是否在项目时间范围内
          if (form.value.projectId) {
            const selectedProject = projectList.value.find(p => p.projectId === form.value.projectId);
            if (selectedProject && selectedProject.startTime && selectedProject.endTime) {
              const projectStart = new Date(selectedProject.startTime);
              const projectEnd = new Date(selectedProject.endTime);

              if (beginTime <= projectStart || endTime >= projectEnd) {
                proxy?.$modal.msgError('组别比赛时间必须在项目比赛时间范围内');
                return;
              }
            }
          }
        } catch (error) {
          proxy?.$modal.msgError('时间验证失败');
          return;
        }
      }

      buttonLoading.value = true;
      const submitForm = { ...form.value };

      if (form.value.groupId) {
        await updateGameEventGroup(submitForm).finally(() => (buttonLoading.value = false));
      } else {
        await addGameEventGroup(submitForm).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: GameEventGroupVO) => {
  const _groupIds = row?.groupId || ids.value;
  await proxy?.$modal.confirm('是否确认删除赛事分组编号为"' + _groupIds + '"的数据项？').finally(() => (loading.value = false));
  await delGameEventGroup(_groupIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/gameEventGroup/export',
    {
      ...queryParams.value
    },
    `${gameEventStore.defaultEventInfo?.eventCode || ''}_赛事分组.xlsx`
  );
};

/** 分组按钮操作 */
const handleGroup = (row: GameEventGroupVO) => {
  router.push({
    name: 'GameEventGroupDetail',
    params: { groupId: row.groupId }
  });
};

onMounted(() => {
  getList();
  getProjectList();
});
</script>
