<template>
  <el-table v-loading="loading" :data="list" border class="evaluation-table">
    <el-table-column width="60" align="center">
      <template #header>
        <el-checkbox :model-value="isAllChecked" :indeterminate="isIndeterminate" @change="handleCheckAllChange" />
      </template>
      <template #default="{ row }">
        <el-checkbox :model-value="isRowChecked(row)" :disabled="row.delFlag === '1'" @change="(checked) => handleRowCheckedChange(row, checked)" />
      </template>
    </el-table-column>
    <el-table-column label="测评图片" width="88" align="center">
      <template #default="{ row }">
        <img :src="getImageUrl(row)" class="evaluation-image" alt="测评图片" />
      </template>
    </el-table-column>
    <el-table-column label="测评名称" prop="evaluationName" min-width="220">
      <template #default="{ row }">
        <div class="evaluation-info">
          <div class="evaluation-name">{{ row.evaluationName || '--' }}</div>
          <div class="evaluation-id">ID: {{ row.id }}</div>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="岗位类型" prop="positionType" min-width="140">
      <template #default="{ row }">
        <span>{{ getOptionLabel(positionTypeOptions, row.positionType) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="参与数" min-width="130" align="center">
      <template #default="{ row }">
        <el-button link type="primary" :disabled="row.delFlag === '1'" @click="emit('viewApplyList', row)">{{ Number(row.participantCount || 0) }}/{{ Number(row.totalCount || 0) }}</el-button>
      </template>
    </el-table-column>
    <el-table-column label="状态" width="110" align="center">
      <template #default="{ row }">
        <el-switch
          :model-value="row.status === '1'"
          inline-prompt
          active-text="开"
          inactive-text="关"
          :disabled="row.delFlag === '1'"
          :loading="switchLoadingMap[String(row.id)]"
          @change="emit('toggleStatus', row, $event)"
        />
      </template>
    </el-table-column>
    <el-table-column label="上架时间" min-width="120" align="center">
      <template #default="{ row }">
        <span>{{ formatDateTime(row.onTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="下架时间" min-width="120" align="center">
      <template #default="{ row }">
        <span>{{ formatDateTime(row.downTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="状态" min-width="120" align="center">
      <template #default="{ row }">
        <el-tag :type="getSaleStatusMeta(row).type" effect="light">
          {{ getSaleStatusMeta(row).label }}
        </el-tag>
      </template>
    </el-table-column>

    <el-table-column label="操作" width="120" align="center" fixed="right">
      <template #default="{ row }">
        <el-button link type="primary" :disabled="row.delFlag === '1'" @click="emit('viewApplyList', row)">查看</el-button>
      </template>
    </el-table-column>
  </el-table>

  <div class="table-footer">
    <div class="table-total">共 {{ total }} 条</div>
    <pagination
      v-show="total > 0"
      :page="pageNum"
      :limit="pageSize"
      :total="total"
      @update:page="emit('update:pageNum', $event)"
      @update:limit="emit('update:pageSize', $event)"
      @pagination="emit('pagination')"
    />
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import type { MainExamEvaluationVO } from '@/api/main/evaluation/types';
import { formatDateTime, getImageUrl, getSaleStatusMeta } from '../utils';

const props = defineProps<{
  loading: boolean;
  list: MainExamEvaluationVO[];
  total: number;
  pageNum: number;
  pageSize: number;
  gradeOptions: Array<{ label: string; value: string }>;
  positionTypeOptions: Array<{ label: string; value: string }>;
  switchLoadingMap: Record<string, boolean>;
  selectedIds: Array<string | number>;
}>();

const emit = defineEmits<{
  pagination: [];
  'update:pageNum': [number];
  'update:pageSize': [number];
  'update:selectedIds': [Array<string | number>];
  toggleStatus: [MainExamEvaluationVO, boolean | string | number];
  viewApplyList: [MainExamEvaluationVO];
}>();

const getOptionLabel = (options: Array<{ label: string; value: string }>, value?: string) => {
  if (!value) {
    return '--';
  }
  return options.find((item) => item.value === value)?.label || value;
};

const currentPageIds = computed(() => props.list.map((item) => item.id).filter((id): id is string | number => id !== undefined && id !== null));

const isAllChecked = computed(() => currentPageIds.value.length > 0 && currentPageIds.value.every((id) => props.selectedIds.includes(id)));

const isIndeterminate = computed(() => {
  const checkedCount = currentPageIds.value.filter((id) => props.selectedIds.includes(id)).length;
  return checkedCount > 0 && checkedCount < currentPageIds.value.length;
});

const isRowChecked = (row: MainExamEvaluationVO) => props.selectedIds.includes(row.id);

const handleCheckAllChange = (checked: boolean | string | number) => {
  if (checked) {
    emit('update:selectedIds', Array.from(new Set([...props.selectedIds, ...currentPageIds.value])));
    return;
  }
  emit('update:selectedIds', props.selectedIds.filter((id) => !currentPageIds.value.includes(id)));
};

const handleRowCheckedChange = (row: MainExamEvaluationVO, checked: boolean | string | number) => {
  if (checked) {
    emit('update:selectedIds', Array.from(new Set([...props.selectedIds, row.id])));
    return;
  }
  emit('update:selectedIds', props.selectedIds.filter((id) => id !== row.id));
};
</script>

<style scoped lang="scss">
.evaluation-table {
  flex: 1;
}

.evaluation-table :deep(.el-checkbox__inner) {
  border-radius: 2px;
}

.evaluation-table :deep(.el-checkbox__input.is-indeterminate .el-checkbox__inner) {
  border-radius: 2px;
}

.evaluation-table :deep(.el-table__header th) {
  background: #f7f8fa;
  color: #303133;
  font-weight: 600;
}

.evaluation-image {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  object-fit: cover;
  background: #f5f7fa;
}

.evaluation-info {
  line-height: 1.6;
}

.evaluation-name {
  color: #303133;
  font-size: 14px;
}

.evaluation-id {
  color: #909399;
  font-size: 12px;
}

.table-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 16px;
}

.table-total {
  color: #909399;
  font-size: 12px;
}
</style>
