<script setup lang="ts">
import { ArrowDown } from "@element-plus/icons-vue";

defineProps<{
  rows: any[];
  summaryTaskMap: Record<number, {
    taskId?: number;
    token?: string;
    expireAt?: string;
    status?: string;
    errorMessage?: string;
    finishedAt?: string;
  }>;
  canCreate: boolean;
  canSubmit: boolean;
  canWithdraw: boolean;
  canDelete: boolean;
  canCancel: boolean;
  canMaterialSave: boolean;
  canMaterialHistory: boolean;
  canMaterialExport: boolean;
  toYuan: (cent?: number) => string;
  statusFormatter: (row: any, column: any, value: unknown) => string;
  auditNodeFormatter: (row: any, column: any, value: unknown) => string;
  isMeetingAuditApproved: (row: any) => boolean;
}>();

defineEmits<{
  openDetail: [row: any];
  openAuditProgress: [row: any];
  openEdit: [row: any];
  openBindExperts: [row: any];
  submit: [id: number];
  withdraw: [id: number];
  deleteDraft: [id: number];
  cancel: [id: number];
  openMaterial: [row: any];
  uploadLaborAgreement: [row: any];
  openHistory: [row: any];
  exportMaterials: [id: number];
  generateSummary: [id: number];
  checkSummaryStatus: [id: number];
  refreshSummaryToken: [id: number];
  downloadSummary: [id: number];
}>();

const normalizeText = (value: unknown) => String(value || "").trim().toUpperCase();

const canDeleteDraft = (row: any) =>
  !row?.deleted && normalizeText(row?.auditStatus) === "PENDING" && !String(row?.lastSubmitAt || "").trim();

const canCancelMeeting = (row: any) => !row?.deleted && normalizeText(row?.status) === "NOT_STARTED";
const summaryStatusText = (rowId: number, statusFormatter: (row: any, column: any, value: unknown) => string, summaryTaskMap: Record<number, { status?: string }>) =>
  statusFormatter(null, null, summaryTaskMap[rowId]?.status || "PENDING");
</script>

<template>
  <el-table :data="rows" empty-text="暂无数据">
    <el-table-column prop="projectName" label="项目名称" width="240" />
    <el-table-column prop="topic" label="会议主题" />
    <!-- <el-table-column label="删除标记" width="100">
      <template #default="{ row }">
        <el-tag v-if="row.deleted" type="danger" size="small">已删除</el-tag>
        <span v-else>-</span>
      </template>
    </el-table-column> -->
    <el-table-column prop="status" label="会议状态" width="100" :formatter="statusFormatter" />
    <el-table-column label="预算（元）" width="100" >
      <template #default="{ row }">{{ toYuan(row.budgetCent) }}</template>
    </el-table-column>
    <el-table-column prop="auditStatus" label="审核状态" width="140" :formatter="statusFormatter" />
    <!-- <el-table-column prop="currentAuditNode" label="当前审核节点" width="160" :formatter="auditNodeFormatter" />
    <el-table-column prop="currentAuditorUserId" label="当前审核节点" width="160" :formatter="auditorNameFormatter" /> -->
    <el-table-column label="操作" width="420">
      <template #default="{ row }">
        <div class="action-buttons">
          <el-button size="small" @click="$emit('openDetail', row)">详情</el-button>
          <el-button
            v-if="canCreate"
            size="small"
            type="primary"
            :disabled="isMeetingAuditApproved(row)"
            @click="$emit('openEdit', row)"
          >
            编辑
          </el-button>
          <el-button v-if="canSubmit" size="small" type="success" @click="$emit('submit', row.id)">
            提交审核
          </el-button>
          <el-button
            v-if="canDelete && canDeleteDraft(row)"
            size="small"
            type="danger"
            plain
            @click="$emit('deleteDraft', row.id)"
          >
            删除草稿
          </el-button>
          <el-button
            v-if="canCancel && canCancelMeeting(row)"
            size="small"
            type="warning"
            plain
            @click="$emit('cancel', row.id)"
          >
            取消会议
          </el-button>
          <el-dropdown v-if="canMaterialSave || canMaterialExport" trigger="click">
            <el-button size="small">
              更多<el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item
                  v-if="canMaterialSave"
                  @click="$emit('openBindExperts', row)"
                >
                  专家管理
                </el-dropdown-item>
                <el-dropdown-item v-if="canMaterialSave" @click="$emit('openMaterial', row)">
                  会议资料
                </el-dropdown-item>
                <el-dropdown-item v-if="canMaterialExport" @click="$emit('exportMaterials', row.id)">
                  导出资料
                </el-dropdown-item>
                <el-dropdown-item v-if="canMaterialExport" divided @click="$emit('generateSummary', row.id)">
                  生成总结
                </el-dropdown-item>
                <el-dropdown-item v-if="canMaterialExport" @click="$emit('checkSummaryStatus', row.id)">
                  查看总结状态
                  {{ summaryTaskMap[row.id]?.taskId ? `（${summaryStatusText(row.id, statusFormatter, summaryTaskMap)}）` : "" }}
                </el-dropdown-item>
                <el-dropdown-item
                  v-if="canMaterialExport"
                  :disabled="!summaryTaskMap[row.id]?.taskId || summaryTaskMap[row.id]?.status !== 'SUCCESS'"
                  @click="$emit('refreshSummaryToken', row.id)"
                >
                  刷新总结令牌
                </el-dropdown-item>
                <el-dropdown-item
                  v-if="canMaterialExport"
                  :disabled="summaryTaskMap[row.id]?.status !== 'SUCCESS' || !summaryTaskMap[row.id]?.taskId || !summaryTaskMap[row.id]?.token"
                  @click="$emit('downloadSummary', row.id)"
                >
                  下载总结
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </template>
    </el-table-column>
  </el-table>
</template>

<style scoped>
.action-buttons {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
</style>
