<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Factură</title>
    <style>
        :root {
            --primary-color: #2563eb;
            --text-color: #1e293b;
            --text-light: #64748b;
            --border-color: #e2e8f0;
            --background-light: #f8fafc;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            line-height: 1.4;
            color: var(--text-color);
            font-size: 10px;
            padding: 1cm;
        }

        .invoice-container {
            max-width: 210mm;
            margin: 0 auto;
        }

        .invoice-header {
            display: grid;
            grid-template-columns: 1fr auto;
            gap: 1rem;
            margin-bottom: 1rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid var(--primary-color);
        }

        .invoice-title {
            color: var(--primary-color);
            font-size: 18px;
            font-weight: bold;
        }

        .invoice-number, .invoice-dates {
            font-size: 11px;
            color: var(--text-light);
        }

        .qr-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-left: -3rem;
            margin-top: -0.5rem;
        }

        #qrcode {
            width: 100px;
            height: 100px;
            margin-bottom: 0.5rem;
        }

        .e-invoice-info {
            font-size: 10px;
            color: var(--text-light);
            text-align: center;
            max-width: 120px;
        }

        .party-details {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1rem;
            margin-bottom: 1rem;
            font-size: 10px;
        }

        .party-box {
            padding: 0.5rem;
            background: var(--background-light);
            border: 1px solid var(--border-color);
        }

        .party-title {
            font-weight: bold;
            color: var(--primary-color);
            margin-bottom: 0.25rem;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 1rem;
            font-size: 9px;
        }

        .items-table th,
        .items-table td {
            padding: 4px;
            border-bottom: 1px solid var(--border-color);
        }

        .items-table th:nth-child(1),
        .items-table td:nth-child(1) {
            text-align: center;
        }

        .items-table th:nth-child(4),
        .items-table td:nth-child(4),
        .items-table th:nth-child(5),
        .items-table td:nth-child(5),
        .items-table th:nth-child(6),
        .items-table td:nth-child(6),
        .items-table th:nth-child(7),
        .items-table td:nth-child(7) {
            text-align: right;
        }

        .note-section {
            font-size: 9px;
            margin: 0.5rem 0;
            padding: 0.5rem;
            background: var(--background-light);
            border: 1px solid var(--border-color);
        }

        .totals-container {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            gap: 2rem;
            margin-top: 1rem;
        }

        .currency-info {
            font-size: 9px;
            padding: 0.5rem;
            background: var(--background-light);
            border: 1px solid var(--border-color);
            width: fit-content;
        }

        .currency-code {
            font-weight: bold;
            color: var(--primary-color);
        }

        .totals-section {
            font-size: 10px;
        }

        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 2px 0;
        }

        .total-row.final {
            border-top: 1px solid var(--primary-color);
            padding-top: 4px;
            margin-top: 4px;
            font-weight: bold;
            color: var(--primary-color);
        }

        .vat-breakdown {
            margin-top: 0.5rem;
            font-size: 9px;
        }

        .vat-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 4px;
            margin: 4px 0;
        }

        .footer {
            margin-top: 1rem;
            padding-top: 0.5rem;
            border-top: 1px solid var(--border-color);
            font-size: 8px;
            color: var(--text-light);
            text-align: center;
        }

        @media print {
            @page {
                size: A4;
                margin: 1cm;
            }
            
            body {
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
        }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
    <style>
        .pdf-toolbar { display: flex; gap: 8px; justify-content: flex-end; margin-bottom: 10px; }
        .pdf-toolbar button {
            padding: 5px 12px; font-size: 12px; border-radius: 4px; cursor: pointer; border: 1px solid;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
        }
        .pdf-toolbar .btn-pdf { background: #1e40af; color: #fff; border-color: #1e40af; }
        .pdf-toolbar .btn-pdf:hover { background: #1e3a8a; }
        .pdf-toolbar .btn-pdf:disabled { opacity: 0.6; cursor: not-allowed; }
        .pdf-toolbar .btn-print { background: transparent; color: #57534e; border-color: #d6d3d1; }
        .pdf-toolbar .btn-print:hover { background: #f5f5f4; }
        @media print { .pdf-toolbar { display: none !important; } }
    </style>
</head>
<body>
    <div class="pdf-toolbar">
        <button class="btn-print" onclick="window.print()">Printează</button>
        <button id="btnSavePdf" class="btn-pdf" onclick="savePdf()">Descarcă PDF</button>
    </div>
    <div class="invoice-container" id="invoice-content">
        <div class="invoice-header">
            <div>
                <h1 class="invoice-title">FACTURĂ</h1>
                <div class="invoice-number">Seria & Nr: <span id="print-invoice-number"></span></div>
                <div class="invoice-dates">
                    Data emiterii: <span id="print-issue-date"></span> |
                    Data scadentă: <span id="print-due-date"></span>
                </div>
            </div>
            <div class="qr-section">
                <div id="qrcode"></div>
                <div class="e-invoice-info"></div>
            </div>
        </div>

        <div class="party-details">
            <div class="party-box">
                <div class="party-title">Furnizor</div>
                <div id="print-supplier-details"></div>
            </div>
            <div class="party-box">
                <div class="party-title">Client</div>
                <div id="print-customer-details"></div>
            </div>
        </div>

        <table class="items-table">
            <thead>
                <tr>
                    <th>Nr.</th>
                    <th>Denumire</th>
                    <th>UM</th>
                    <th>Cant.</th>
                    <th>Preț</th>
                    <th>TVA</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody id="print-items"></tbody>
        </table>

        <div class="note-section" id="print-note" style="display: none;">
            <h3>Text Adițional:</h3>
            <div></div>
        </div>

        <div class="totals-container">
            <div class="currency-info">
                <p>Monedă Factură: <span class="currency-code" id="print-document-currency"></span></p>
                <p id="print-tax-currency-container" style="display: none;">
                    Monedă TVA: <span class="currency-code" id="print-tax-currency"></span>
                    <br>
                    Curs valutar: <span id="print-exchange-rate"></span>
                </p>
            </div>

            <div class="totals-section">
                <div class="total-row">
                    <span>Subtotal:</span>
                    <span id="print-subtotal"></span>
                </div>
                <div id="print-allowances-row" style="display: none;">
                    <div class="total-row">
                        <span>Reduceri:</span>
                        <span id="print-allowances"></span>
                    </div>
                </div>
                <div id="print-charges-row" style="display: none;">
                    <div class="total-row">
                        <span>Taxe:</span>
                        <span id="print-charges"></span>
                    </div>
                </div>
                <div class="total-row">
                    <span>Valoare Netă:</span>
                    <span id="print-net-amount"></span>
                </div>
                
                <div class="vat-breakdown">
                    <div class="vat-grid">
                        <div>Tip TVA</div>
                        <div>Cotă</div>
                        <div>Bază</div>
                        <div>TVA</div>
                    </div>
                    <div class="vat-grid" id="print-vat-breakdown"></div>
                    
                    <div class="total-row">
                        <span>Total TVA (<span id="print-vat-currency-main"></span>):</span>
                        <span id="print-vat-main"></span>
                    </div>
                    <div id="print-vat-secondary" class="total-row" style="display: none;">
                        <span>TVA (<span id="print-vat-currency-secondary"></span>):</span>
                        <span id="print-vat-secondary-amount"></span>
                    </div>
                </div>
                
                <div class="total-row final">
                    <span>Total cu TVA:</span>
                    <span id="print-total"></span>
                </div>
            </div>
        </div>

        <div class="footer">

        </div>
    </div>

    <script>
        function updateAllowancesChargesVisibility() {
            const allowancesRow = document.getElementById('print-allowances-row');
            const chargesRow = document.getElementById('print-charges-row');
            const allowancesAmount = parseFloat(document.getElementById('print-allowances').textContent) || 0;
            const chargesAmount = parseFloat(document.getElementById('print-charges').textContent) || 0;

            allowancesRow.style.display = allowancesAmount > 0 ? 'block' : 'none';
            chargesRow.style.display = chargesAmount > 0 ? 'block' : 'none';
        }

        window.addEventListener('load', updateAllowancesChargesVisibility);

        async function savePdf() {
            const btn = document.getElementById('btnSavePdf');
            if (btn) { btn.disabled = true; btn.textContent = 'Generare…'; }
            try {
                if (!window.html2pdf) {
                    await new Promise((resolve, reject) => {
                        const s = document.createElement('script');
                        s.src = '../js/vendor/html2pdf.bundle.min.js';
                        s.onload = resolve;
                        s.onerror = () => reject(new Error('Bundle html2pdf indisponibil'));
                        document.head.appendChild(s);
                    });
                }
                const element = document.getElementById('invoice-content');
                const invoiceNumber = document.getElementById('print-invoice-number')?.textContent || 'factura';
                await window.html2pdf().set({
                    margin: [8, 8, 8, 8],
                    filename: 'factura_' + invoiceNumber.replace(/[^a-zA-Z0-9_-]/g, '_') + '.pdf',
                    image: { type: 'jpeg', quality: 0.95 },
                    html2canvas: { scale: 2, useCORS: true },
                    jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }
                }).from(element).save();
            } catch (err) {
                alert('Eroare la generarea PDF: ' + err.message);
            } finally {
                if (btn) { btn.disabled = false; btn.textContent = 'Descarcă PDF'; }
            }
        }
    </script>
</body>
</html>