<?php
$formatMoney = static fn($value): string => number_format((float)$value, 2, ',', ' ') . ' DH';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-receipt-cutoff me-2"></i>Avoirs
        </h1>
        <p class="text-muted mb-0">Liste des avoirs créés depuis les factures.</p>
    </div>

    <a href="index.php?page=documents" class="btn btn-outline-secondary">
        <i class="bi bi-folder2-open me-1"></i> Documents
    </a>
</div>

<div class="darlink-card darlink-table-card">
    <form method="get" class="darlink-table-toolbar">
        <input type="hidden" name="page" value="avoirs">

        <div class="darlink-table-toolbar-left">
            <div class="darlink-table-search">
                <i class="bi bi-search"></i>
                <input type="text"
                       name="q"
                       value="<?= htmlspecialchars($filters['q'] ?? '') ?>"
                       placeholder="Rechercher un avoir...">
            </div>
        </div>

        <div class="darlink-table-toolbar-right">
            <button class="darlink-toolbar-btn" type="submit">
                <i class="bi bi-search"></i> Rechercher
            </button>

            <a href="index.php?page=avoirs" class="darlink-toolbar-btn">
                <i class="bi bi-arrow-counterclockwise"></i> Réinitialiser
            </a>
        </div>
    </form>

    <div class="table-responsive darlink-table-wrapper">
        <table class="table align-middle darlink-table darlink-table-modern" id="avoirsTable">
            <thead>
            <tr>
                <th class="darlink-sortable" data-column="0">Numéro <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="1">Client <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="2">Date <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="3">Facture liée <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="4">Statut <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="5">Total TTC <span class="sort-indicator">↕</span></th>
                <th class="text-end">Actions</th>
            </tr>
            </thead>

            <tbody>
            <?php if (empty($documents)): ?>
                <tr>
                    <td colspan="7" class="text-center text-muted py-5">
                        Aucun avoir enregistré.
                    </td>
                </tr>
            <?php endif; ?>

            <?php foreach ($documents as $document): ?>
                <tr data-edit-url="index.php?page=documents_show&id=<?= (int)$document['id'] ?>">
                    <td>
                        <strong class="darlink-table-code">
                            <?= htmlspecialchars($document['numero']) ?>
                        </strong>
                    </td>

                    <td class="fw-semibold">
                        <?= htmlspecialchars($document['client_nom']) ?>
                    </td>

                    <td>
                        <?= htmlspecialchars(date('d/m/Y', strtotime((string)$document['date_document']))) ?>
                    </td>

                    <td>
                        <?php if (!empty($document['parent_numero'])): ?>
                            <?= htmlspecialchars(($document['parent_type'] ?? '') . ' ' . $document['parent_numero']) ?>
                        <?php else: ?>
                            <span class="text-muted">—</span>
                        <?php endif; ?>
                    </td>

                    <td>
                        <span class="darlink-badge darlink-badge-light">
                            <?= htmlspecialchars($document['statut']) ?>
                        </span>
                    </td>

                    <td class="text-end fw-semibold">
                        <?= $formatMoney($document['total_ttc']) ?>
                    </td>

                    <td class="text-end darlink-actions">
                        <a class="darlink-icon-btn darlink-icon-btn-view"
                           href="index.php?page=documents_show&id=<?= (int)$document['id'] ?>"
                           title="Voir">
                            <i class="bi bi-eye"></i>
                        </a>

                        <a class="darlink-icon-btn darlink-icon-btn-edit"
                           href="index.php?page=avoirs_pdf&id=<?= (int)$document['id'] ?>"
                           target="_blank"
                           title="PDF">
                            <i class="bi bi-file-earmark-pdf"></i>
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>