<?php
/**
 * Template PDF - Facture
 * Variables disponibles : $invoice, $client, $company, $lines
 */
defined('ABSPATH') || exit;

use ComptaFlowPro\Services\PDFGenerator;
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Facture <?php echo esc_html($invoice['invoice_number']); ?></title>
    <style>
        @page { margin: 0; }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 10pt;
            line-height: 1.5;
            color: #2d3436;
            background: #fff;
        }
        .container { padding: 40px 50px; }

        /* En-tete */
        .header { display: table; width: 100%; margin-bottom: 40px; }
        .header-left, .header-right { display: table-cell; vertical-align: top; width: 50%; }
        .header-right { text-align: right; }
        .logo { max-width: 180px; max-height: 70px; margin-bottom: 10px; }
        .company-name { font-size: 16pt; font-weight: bold; color: #2d3436; margin-bottom: 8px; }
        .company-info { font-size: 8.5pt; color: #636e72; line-height: 1.6; }

        /* Titre document */
        .document-header { margin-bottom: 40px; border-bottom: 2px solid #f1f2f6; padding-bottom: 20px; }
        .document-title { font-size: 22pt; font-weight: bold; color: #2d3436; }
        .document-number { color: #636e72; font-size: 12pt; margin-top: 5px; }

        /* Section infos */
        .info-section { display: table; width: 100%; margin-bottom: 40px; }
        .info-box { display: table-cell; width: 50%; vertical-align: top; }
        .info-content { padding-right: 20px; }
        .info-right .info-content { padding-right: 0; padding-left: 30px; border-left: 1px solid #f1f2f6; }
        .label-sm { text-transform: uppercase; font-size: 7.5pt; font-weight: bold; color: #b2bec3; margin-bottom: 8px; display: block; letter-spacing: 0.5px; }
        .client-name { font-size: 13pt; font-weight: bold; margin-bottom: 5px; color: #2d3436; }
        .info-text { font-size: 9pt; color: #636e72; line-height: 1.6; }
        .info-row { margin-bottom: 4px; }
        .info-label { font-weight: bold; color: #2d3436; }

        /* Badges statut */
        .status-badge { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 8pt; font-weight: bold; text-transform: uppercase; }
        .status-paid { background: #e3fcef; color: #00875a; }
        .status-pending { background: #fff9db; color: #f08c00; }
        .status-overdue { background: #ffe3e3; color: #e03131; }
        .status-cancelled { background: #f1f2f6; color: #636e72; }

        /* Tableau des lignes */
        .lines-table { width: 100%; border-collapse: collapse; margin-bottom: 30px; }
        .lines-table th { background: #f8f9fa; color: #636e72; padding: 12px 10px; text-align: left; font-size: 8pt; text-transform: uppercase; border-bottom: 2px solid #dfe6e9; letter-spacing: 0.3px; }
        .lines-table th.text-right { text-align: right; }
        .lines-table td { padding: 14px 10px; border-bottom: 1px solid #f1f2f6; font-size: 9.5pt; }
        .lines-table td.text-right { text-align: right; }
        .description-cell { font-weight: 600; color: #2d3436; }

        /* Totaux */
        .totals-section { display: table; width: 100%; margin-bottom: 30px; }
        .totals-notes { display: table-cell; width: 55%; vertical-align: top; padding-right: 40px; }
        .totals-notes p { font-size: 9pt; color: #636e72; }
        .totals-right { display: table-cell; width: 45%; vertical-align: top; }
        .totals-table { width: 100%; border-collapse: collapse; }
        .totals-table td { padding: 10px 12px; }
        .totals-table .label { text-align: right; color: #636e72; font-size: 9.5pt; }
        .totals-table .value { text-align: right; width: 120px; font-weight: bold; color: #2d3436; }
        .totals-table .subtotal-row { border-bottom: 1px solid #f1f2f6; }
        .totals-table .grand-total { background: #2d3436; color: #fff; }
        .totals-table .grand-total td { padding: 14px 12px; }
        .totals-table .grand-total .label { color: #fff; font-size: 10pt; font-weight: bold; }
        .totals-table .grand-total .value { font-size: 14pt; color: #fff; }

        /* Section paiement */
        .payment-section { margin-top: 40px; background: #f8f9fa; padding: 20px; border-radius: 6px; }
        .payment-section .label-sm { color: #2d3436; margin-bottom: 12px; }
        .payment-text { font-size: 9pt; color: #636e72; margin-bottom: 10px; }
        .bank-details { font-family: 'DejaVu Sans Mono', 'Courier', monospace; font-size: 9pt; background: #fff; padding: 12px; border-radius: 4px; border: 1px solid #dfe6e9; }
        .bank-details strong { color: #2d3436; }

        /* Mentions legales */
        .legal-section { margin-top: 30px; padding-top: 20px; border-top: 1px solid #f1f2f6; }
        .legal-section p { font-size: 7.5pt; color: #b2bec3; margin-bottom: 4px; }
        .legal-section .important { color: #636e72; font-weight: bold; }

        /* Footer */
        .footer { position: fixed; bottom: 25px; left: 50px; right: 50px; text-align: center; font-size: 7.5pt; color: #b2bec3; border-top: 1px solid #f1f2f6; padding-top: 12px; }
    </style>
</head>
<body>
    <div class="container">

        <!-- En-tete -->
        <div class="header">
            <div class="header-left">
                <?php if (!empty($company['logo'])): ?>
                    <img src="<?php echo $company['logo']; ?>" class="logo" alt="Logo">
                <?php endif; ?>
                <div class="company-name"><?php echo esc_html($company['name']); ?></div>
                <div class="company-info">
                    <?php if (!empty($company['address'])): ?>
                        <?php echo esc_html($company['address']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($company['postal_code']) || !empty($company['city'])): ?>
                        <?php echo esc_html(trim($company['postal_code'] . ' ' . $company['city'])); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($company['phone'])): ?>
                        Tel : <?php echo esc_html($company['phone']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($company['email'])): ?>
                        <?php echo esc_html($company['email']); ?>
                    <?php endif; ?>
                </div>
            </div>
            <div class="header-right">
                <div class="company-info">
                    <?php if (!empty($company['siret'])): ?>
                        SIRET : <?php echo esc_html($company['siret']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($company['vat_number'])): ?>
                        TVA Intra : <?php echo esc_html($company['vat_number']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($company['rcs_city'])): ?>
                        RCS <?php echo esc_html($company['rcs_city']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($company['naf_code'])): ?>
                        NAF : <?php echo esc_html($company['naf_code']); ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Titre -->
        <div class="document-header">
            <div class="document-title">FACTURE</div>
            <div class="document-number">N° <?php echo esc_html($invoice['invoice_number']); ?></div>
        </div>

        <!-- Infos facture + client -->
        <div class="info-section">
            <div class="info-box info-left">
                <div class="info-content">
                    <span class="label-sm">Details de la facture</span>
                    <div class="info-text">
                        <div class="info-row">
                            <span class="info-label">Date d'emission :</span>
                            <?php echo PDFGenerator::formatDate($invoice['date_invoice']); ?>
                        </div>
                        <div class="info-row">
                            <span class="info-label">Date d'echeance :</span>
                            <?php echo PDFGenerator::formatDate($invoice['date_due']); ?>
                        </div>
                        <div class="info-row">
                            <span class="info-label">Statut :</span>
                            <?php
                            $status_class = match($invoice['status']) {
                                'PAID' => 'status-paid',
                                'PENDING' => 'status-pending',
                                'OVERDUE' => 'status-overdue',
                                'CANCELLED' => 'status-cancelled',
                                default => ''
                            };
                            $status_label = match($invoice['status']) {
                                'PAID' => 'Payee',
                                'PENDING' => 'A regler',
                                'OVERDUE' => 'En retard',
                                'CANCELLED' => 'Annulee',
                                'DRAFT' => 'Brouillon',
                                default => $invoice['status']
                            };
                            ?>
                            <span class="status-badge <?php echo $status_class; ?>"><?php echo $status_label; ?></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="info-box info-right">
                <div class="info-content">
                    <span class="label-sm">Facture a</span>
                    <div class="client-name"><?php echo esc_html($client['name']); ?></div>
                    <div class="info-text">
                        <?php if (!empty($client['address'])): ?>
                            <?php echo esc_html($client['address']); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($client['postal_code']) || !empty($client['city'])): ?>
                            <?php echo esc_html(trim($client['postal_code'] . ' ' . $client['city'])); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($client['email'])): ?>
                            <?php echo esc_html($client['email']); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($client['siret'])): ?>
                            SIRET : <?php echo esc_html($client['siret']); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($client['vat_number'])): ?>
                            TVA : <?php echo esc_html($client['vat_number']); ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Tableau des lignes -->
        <table class="lines-table">
            <thead>
                <tr>
                    <th style="width: 45%;">Description</th>
                    <th class="text-right" style="width: 10%;">Qte</th>
                    <th class="text-right" style="width: 18%;">Prix unit. HT</th>
                    <th class="text-right" style="width: 10%;">TVA</th>
                    <th class="text-right" style="width: 17%;">Total HT</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lines as $line): ?>
                <tr>
                    <td class="description-cell"><?php echo esc_html($line['description']); ?></td>
                    <td class="text-right"><?php echo number_format(floatval($line['quantity']), 2, ',', ' '); ?></td>
                    <td class="text-right"><?php echo PDFGenerator::formatAmount(floatval($line['unit_price_ht'])); ?></td>
                    <td class="text-right"><?php echo number_format(floatval($line['vat_rate']), 1, ',', ' '); ?>%</td>
                    <td class="text-right"><?php echo PDFGenerator::formatAmount(floatval($line['total_ht'])); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <!-- Totaux -->
        <div class="totals-section">
            <div class="totals-notes">
                <?php if (!empty($invoice['notes'])): ?>
                    <span class="label-sm">Notes</span>
                    <p><?php echo nl2br(esc_html($invoice['notes'])); ?></p>
                <?php endif; ?>
            </div>
            <div class="totals-right">
                <table class="totals-table">
                    <tr class="subtotal-row">
                        <td class="label">Total HT</td>
                        <td class="value"><?php echo PDFGenerator::formatAmount(floatval($invoice['total_ht'])); ?></td>
                    </tr>
                    <tr class="subtotal-row">
                        <td class="label">TVA</td>
                        <td class="value"><?php echo PDFGenerator::formatAmount(floatval($invoice['total_tva'])); ?></td>
                    </tr>
                    <tr class="grand-total">
                        <td class="label">NET A PAYER</td>
                        <td class="value"><?php echo PDFGenerator::formatAmount(floatval($invoice['total_ttc'])); ?></td>
                    </tr>
                </table>
            </div>
        </div>

        <!-- Paiement (si non paye) -->
        <?php if ($invoice['status'] !== 'PAID' && (!empty($company['iban']) || !empty($company['payment_terms']))): ?>
        <div class="payment-section">
            <span class="label-sm">Modalites de paiement</span>
            <?php if (!empty($company['payment_terms'])): ?>
                <p class="payment-text"><?php echo esc_html($company['payment_terms']); ?></p>
            <?php endif; ?>
            <?php if (!empty($company['iban'])): ?>
                <div class="bank-details">
                    <strong>IBAN :</strong> <?php echo esc_html($company['iban']); ?>
                    <?php if (!empty($company['bic'])): ?>
                        <br><strong>BIC :</strong> <?php echo esc_html($company['bic']); ?>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <!-- Mentions legales -->
        <div class="legal-section">
            <?php if (($company['vat_regime'] ?? '') === 'FRANCHISE'): ?>
                <p class="important">TVA non applicable, article 293 B du CGI</p>
            <?php endif; ?>
            <?php if (!empty($company['late_payment_penalties'])): ?>
                <p><?php echo esc_html($company['late_payment_penalties']); ?></p>
            <?php endif; ?>
            <?php if (!empty($company['recovery_indemnity'])): ?>
                <p><?php echo esc_html($company['recovery_indemnity']); ?></p>
            <?php endif; ?>
            <?php if (!empty($company['no_discount'])): ?>
                <p><?php echo esc_html($company['no_discount']); ?></p>
            <?php endif; ?>
        </div>

        <!-- Footer -->
        <div class="footer">
            <?php echo esc_html($company['name']); ?>
            <?php if (!empty($company['siret'])): ?>
                - SIRET <?php echo esc_html($company['siret']); ?>
            <?php endif; ?>
            <?php if (!empty($company['rcs_city'])): ?>
                - RCS <?php echo esc_html($company['rcs_city']); ?>
            <?php endif; ?>
            <?php if (!empty($company['website'])): ?>
                <br><?php echo esc_html($company['website']); ?>
            <?php endif; ?>
        </div>

    </div>
</body>
</html>
