/**
 * Componente para impressão de relatórios
 */

'use client';

import React from 'react';
import { Button } from 'antd';
import { Printer } from 'lucide-react';
import { ICON_SIZE_MD } from '@/components/icons';

interface PrintButtonProps {
    title?: string;
    content?: React.ReactNode;
    className?: string;
    style?: React.CSSProperties;
}

export default function PrintButton({ title, content, className, style }: PrintButtonProps) {
    const handlePrint = () => {
        // Criar janela de impressão
        const printWindow = window.open('', '_blank');
        if (!printWindow) return;

        // Estilos para impressão
        const printStyles = `
      <style>
        @media print {
          @page {
            margin: 1cm;
          }
          body {
            font-family: Arial, sans-serif;
            font-size: 12pt;
            color: #000;
          }
          .no-print {
            display: none !important;
          }
          table {
            border-collapse: collapse;
            width: 100%;
          }
          th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
          }
          th {
            background-color: #f2f2f2;
            font-weight: bold;
          }
        }
      </style>
    `;

        // Conteúdo a ser impresso
        const printContent = document.getElementById('print-content')?.innerHTML || content || '';

        printWindow.document.write(`
      <!DOCTYPE html>
      <html>
        <head>
          <title>${title || 'Relatório'}</title>
          ${printStyles}
        </head>
        <body>
          <h1>${title || 'Relatório'}</h1>
          <div id="print-content">
            ${typeof printContent === 'string' ? printContent : ''}
          </div>
        </body>
      </html>
    `);

        printWindow.document.close();
        printWindow.focus();

        // Asalvar carregamento e imprimir
        setTimeout(() => {
            printWindow.print();
            printWindow.close();
        }, 250);
    };

    return (
        <Button
            icon={<Printer size={ICON_SIZE_MD} aria-hidden />}
            onClick={handlePrint}
            className={className}
            style={style}
        >
            Imprimir
        </Button>
    );
}
