<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h5 class="m-0">Facture <?= $invoice['invoice_number'] ?></h5>
        <small class="text-muted"><?= ucfirst($invoice['invoice_type']) ?> - <?= date('d/m/Y', strtotime($invoice['date'])) ?></small>
    </div>
    <div class="d-flex gap-2">
        <a href="/invoices/pdf/<?= $invoice['id'] ?>" class="btn btn-danger" target="_blank">
            <i class="bi bi-file-pdf"></i> PDF
        </a>
        <?php if ($invoice['status'] === 'brouillon'): ?>
            <a href="/invoices/edit/<?= $invoice['id'] ?>" class="btn btn-warning">
                <i class="bi bi-pencil"></i> Modifier
            </a>
        <?php endif; ?>
        <?php if (in_array($invoice['status'], ['envoyee', 'partielle', 'retard'])): ?>
            <a href="/payments/create/<?= $invoice['id'] ?>" class="btn btn-success">
                <i class="bi bi-cash"></i> Paiement
            </a>
        <?php endif; ?>
        <a href="/invoices" class="btn btn-outline-secondary">Retour</a>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-8">
        <!-- Invoice Preview Card -->
        <div class="table-card">
            <div class="row mb-4">
                <div class="col-6">
                    <h6 class="text-primary fw-bold"><?= $invoice['client']['company_name'] ?? 'N/A' ?></h6>
                    <p class="mb-1"><small>Code: <?= $invoice['client']['code'] ?? 'N/A' ?></small></p>
                    <p class="mb-1"><small><?= $invoice['client']['address'] ?? '' ?></small></p>
                    <p class="mb-1"><small><?= $invoice['client']['city'] ?? '' ?></small></p>
                    <p class="mb-0"><small>MF: <?= $invoice['client']['tax_id'] ?? '' ?></small></p>
                </div>
                <div class="col-6 text-end">
                    <span class="badge-status status-<?= $invoice['status'] ?> fs-6"><?= ucfirst($invoice['status']) ?></span>
                </div>
            </div>

            <h6 class="border-bottom pb-2 mb-3">Informations de transit</h6>
            <div class="row mb-4">
                <div class="col-md-6">
                    <table class="table table-sm table-borderless">
                        <tr><td class="text-muted" style="width:40%">Code douane:</td><td><?= $invoice['customs_code'] ?: '-' ?></td></tr>
                        <tr><td class="text-muted">Reper. N:</td><td><?= $invoice['reper_number'] ?: '-' ?></td></tr>
                        <tr><td class="text-muted">Designation:</td><td class="fw-bold"><?= $invoice['designation_global'] ?: '-' ?></td></tr>
                        <tr><td class="text-muted">Poids Brut:</td><td><?= $invoice['gross_weight'] ? number_format($invoice['gross_weight'], 0, ',', ' ') . ' ' . $invoice['weight_unit'] : '-' ?></td></tr>
                    </table>
                </div>
                <div class="col-md-6">
                    <table class="table table-sm table-borderless">
                        <tr><td class="text-muted" style="width:40%">Nbre. Colis:</td><td><?= $invoice['package_count'] ?: '-' ?></td></tr>
                        <tr><td class="text-muted">Valeur devise:</td><td><?= $invoice['currency_value'] ? number_format($invoice['currency_value'], 2, ',', ' ') . ' ' . $invoice['currency_unit'] : '-' ?></td></tr>
                        <tr><td class="text-muted">Expediteur:</td><td><?= $invoice['shipper'] ?: '-' ?></td></tr>
                        <tr><td class="text-muted">Declaration:</td><td><?= $invoice['declaration_number'] ?: '-' ?></td></tr>
                    </table>
                </div>
            </div>

            <h6 class="border-bottom pb-2 mb-3">Detail des frais</h6>
            <table class="table table-bordered">
                <thead class="table-dark">
                    <tr>
                        <th>N</th>
                        <th>Designation</th>
                        <th class="text-end">Montant (TND)</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($invoice['lines'] as $idx => $line): ?>
                        <tr>
                            <td style="width:5%"><?= $idx + 1 ?></td>
                            <td><?= $line['designation'] ?></td>
                            <td class="text-end"><?= number_format($line['amount'], 2, ',', ' ') ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

            <div class="row mt-4">
                <div class="col-md-6">
                    <div class="p-3 bg-light rounded">
                        <small class="text-muted fst-italic">
                            <strong>ARRETEE LA PRESENTE A LA SOMME DE:</strong><br>
                            <?= $invoice['amount_in_words'] ?>
                        </small>
                    </div>
                </div>
                <div class="col-md-6">
                    <table class="table table-sm">
                        <tr><td class="text-muted">Debours:</td><td class="text-end fw-bold"><?= number_format($invoice['subtotal'], 2, ',', ' ') ?> TND</td></tr>
                        <tr><td class="text-muted">Taxables:</td><td class="text-end fw-bold"><?= number_format($invoice['vat_base'], 2, ',', ' ') ?> TND</td></tr>
                        <tr><td class="text-muted">TVA (<?= $invoice['vat_rate'] * 100 ?>%):</td><td class="text-end fw-bold"><?= number_format($invoice['vat_amount'], 2, ',', ' ') ?> TND</td></tr>
                        <tr><td class="text-muted">Droit de timbres:</td><td class="text-end fw-bold"><?= number_format($invoice['stamp_duty'], 2, ',', ' ') ?> TND</td></tr>
                        <tr class="table-primary"><td class="fw-bold">Total Facture:</td><td class="text-end fw-bold"><?= number_format($invoice['total_ttc'], 2, ',', ' ') ?> TND</td></tr>
                        <tr><td class="text-muted">Avance:</td><td class="text-end"><?= number_format($invoice['advance_amount'], 2, ',', ' ') ?> TND</td></tr>
                        <tr class="table-success"><td class="fw-bold">Net a payer:</td><td class="text-end fw-bold fs-5"><?= number_format($invoice['net_payable'], 2, ',', ' ') ?> TND</td></tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <!-- Payment History -->
        <div class="table-card mb-4">
            <h6 class="mb-3">Historique des paiements</h6>
            <?php if (empty($invoice['payments'])): ?>
                <p class="text-muted text-center py-3">Aucun paiement enregistre</p>
            <?php else: ?>
                <?php foreach ($invoice['payments'] as $payment): ?>
                    <div class="d-flex justify-content-between align-items-center py-2 border-bottom">
                        <div>
                            <div class="fw-semibold"><?= number_format($payment['amount'], 2, ',', ' ') ?> TND</div>
                            <small class="text-muted"><?= date('d/m/Y', strtotime($payment['payment_date'])) ?> - <?= ucfirst($payment['payment_method']) ?></small>
                        </div>
                        <span class="badge bg-success">Paye</span>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>

            <?php 
            $totalPaid = array_sum(array_column($invoice['payments'], 'amount'));
            $remaining = $invoice['net_payable'] - $totalPaid;
            ?>
            <div class="mt-3 pt-3 border-top">
                <div class="d-flex justify-content-between">
                    <span class="fw-bold">Total paye:</span>
                    <span class="fw-bold text-success"><?= number_format($totalPaid, 2, ',', ' ') ?> TND</span>
                </div>
                <?php if ($remaining > 0): ?>
                    <div class="d-flex justify-content-between mt-2">
                        <span class="fw-bold">Reste a payer:</span>
                        <span class="fw-bold text-danger"><?= number_format($remaining, 2, ',', ' ') ?> TND</span>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Invoice Info -->
        <div class="table-card">
            <h6 class="mb-3">Informations</h6>
            <table class="table table-sm table-borderless">
                <tr><td class="text-muted">Cree par:</td><td><?= $invoice['user']['first_name'] ?? '' ?> <?= $invoice['user']['last_name'] ?? '' ?></td></tr>
                <tr><td class="text-muted">Cree le:</td><td><?= date('d/m/Y H:i', strtotime($invoice['created_at'])) ?></td></tr>
                <tr><td class="text-muted">Modifie le:</td><td><?= date('d/m/Y H:i', strtotime($invoice['updated_at'])) ?></td></tr>
                <?php if ($invoice['sent_at']): ?>
                    <tr><td class="text-muted">Envoyee le:</td><td><?= date('d/m/Y H:i', strtotime($invoice['sent_at'])) ?></td></tr>
                <?php endif; ?>
                <?php if ($invoice['paid_at']): ?>
                    <tr><td class="text-muted">Payee le:</td><td><?= date('d/m/Y H:i', strtotime($invoice['paid_at'])) ?></td></tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
