/**
 * 交叉评查文件信息组件
 */

// 导入统一的下载方法和提示服务
import { downloadFile } from '~/api/axios-client';
import { toastService } from '~/components/ui/Toast';

interface FileInfoProps {
  fileInfo: {
    fileName: string;
    contractNumber: string;
    fileSize?: string;
    fileFormat?: string;
    pageCount?: number;
    uploadTime?: string;
    uploadUser?: string;
    auditStatus?: number;
    path?: string;
    previousRoute?: string;
    fileType?: string;
  };
  onConfirmResults: () => void;
}

export function FileInfo({ fileInfo, onConfirmResults }: FileInfoProps) {

  // 使用统一的下载方法（与 rules-files.tsx 相同）
  const handleDownloadFile = async () => {
    if (!fileInfo.path) {
      toastService.error('文件路径不存在，无法下载');
      return;
    }

    try {
      // 使用axios封装的下载方法
      const blob = await downloadFile(fileInfo.path);

      // 创建Blob URL
      const blobUrl = URL.createObjectURL(blob);

      // 创建一个隐藏的a标签并点击它
      const a = document.createElement('a');
      a.style.display = 'none';
      a.href = blobUrl;
      a.download = fileInfo.fileName || 'document';
      document.body.appendChild(a);
      a.click();

      // 清理
      setTimeout(() => {
        document.body.removeChild(a);
        URL.revokeObjectURL(blobUrl);
      }, 100);
    } catch (error) {
      console.error('下载文件失败:', error);
      toastService.error(`下载文件失败: ${error instanceof Error ? error.message : '未知错误'}`);
    }
  };

  const handleExportReport = () => {
    alert('导出交叉评查报告功能');
  };

  return (
    <div className="mb-4 file-info-header">
      <div className="flex justify-between items-center">
        <div className="flex-1">
          {/* 文件基本信息已在面包屑区域显示，这里可以不重复显示 */}
        </div>
        
        {/* 操作按钮区域 */}
        <div className="flex items-center gap-3">
          {/* 下载原文件按钮 */}
          <button 
            onClick={handleDownloadFile}
            className="inline-flex items-center px-3 py-1.5 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
          >
            <i className="fas fa-download mr-1.5"></i>
            下载原文件
          </button>

          {/* 导出评查报告按钮 */}
          <button 
            onClick={handleExportReport}
            className="inline-flex items-center px-3 py-1.5 text-sm font-medium text-white bg-blue-600 border border-transparent rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
          >
            <i className="fas fa-file-export mr-1.5"></i>
            导出评查报告
          </button>

          {/* 确认评查结果按钮 - 只在未审核通过时显示 */}
          {fileInfo.auditStatus !== 1 && (
            <button 
              onClick={onConfirmResults}
              className="inline-flex items-center px-3 py-1.5 text-sm font-medium text-white bg-green-600 border border-transparent rounded-md hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500"
            >
              <i className="fas fa-check mr-1.5"></i>
              确认评查结果
            </button>
          )}

          {/* 已确认状态显示 */}
          {fileInfo.auditStatus === 1 && (
            <span className="inline-flex items-center px-3 py-1.5 text-sm font-medium text-green-700 bg-green-100 border border-green-200 rounded-md">
              <i className="fas fa-check-circle mr-1.5"></i>
              评查结果已确认
            </span>
          )}
        </div>
      </div>
    </div>
  );
} 