/**
 * KRA - Export Excel Component
 * Export data to Excel file
 */
import React, { useState } from 'react';
import { Button, message } from 'antd';
import { DownloadOutlined } from '@ant-design/icons';
import { exportExcel } from '@/services/kratos/exportTemplate';

interface ExportExcelProps {
  templateId: string;
  params?: Record<string, any>;
  fileName?: string;
  buttonText?: string;
  buttonType?: 'primary' | 'default' | 'dashed' | 'link' | 'text';
  className?: string;
  style?: React.CSSProperties;
}

const ExportExcel: React.FC<ExportExcelProps> = ({
  templateId,
  params = {},
  fileName,
  buttonText = '导出Excel',
  buttonType = 'primary',
  className,
  style,
}) => {
  const [loading, setLoading] = useState(false);

  const handleExport = async () => {
    try {
      setLoading(true);
      const res = await exportExcel({ templateID: templateId, ...params });
      if (res.code === 0) {
        // Handle download URL or blob
        message.success('导出成功');
      } else {
        message.error(res.msg || '导出失败');
      }
    } catch (error) {
      message.error('导出失败');
    } finally {
      setLoading(false);
    }
  };

  return (
    <Button
      type={buttonType}
      icon={<DownloadOutlined />}
      loading={loading}
      onClick={handleExport}
      className={className}
      style={style}
    >
      {buttonText}
    </Button>
  );
};

export default ExportExcel;
