<?php
$formatMoney = static fn($value): string => number_format((float)$value, 2, ',', ' ') . ' DH';
$statusLabels = [
    'non_reglee' => 'Non réglée',
    'partiellement_reglee' => 'Partiellement réglée',
    'reglee' => 'Réglée',
];
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Factures fournisseurs</h1>
        <p class="text-muted mb-0">Suivi des factures fournisseurs et du reste dû.</p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=factures_fournisseurs_dashboard" class="btn btn-outline-primary" role="button">Tableau fournisseurs</a>
        <a href="index.php?page=factures_fournisseurs_echeancier" class="btn btn-outline-primary">Échéancier</a>
        <a href="index.php?page=reglements_fournisseurs" class="btn btn-outline-primary">Règlements fournisseurs</a>
        <form method="get" action="index.php" class="d-inline">
            <input type="hidden" name="page" value="factures_fournisseurs_create">
            <button type="submit" class="btn btn-darlink-primary">+ Enregistrer une facture fournisseur reçue</button>
        </form>
    </div>
</div>

<div class="darlink-card p-4 mb-3">
    <form method="get" class="row g-2 align-items-end">
        <input type="hidden" name="page" value="factures_fournisseurs">
        <div class="col-md-4">
            <label class="form-label">Recherche</label>
            <input type="text" name="q" class="form-control" value="<?= htmlspecialchars($filters['q'] ?? '') ?>" placeholder="Numéro, fournisseur, commande...">
        </div>
        <div class="col-md-3">
            <label class="form-label">Fournisseur</label>
            <select name="fournisseur_id" class="form-select">
                <option value="0">Tous</option>
                <?php foreach ($fournisseurs as $fournisseur): ?>
                    <option value="<?= (int)$fournisseur['id'] ?>" <?= (int)($filters['fournisseur_id'] ?? 0) === (int)$fournisseur['id'] ? 'selected' : '' ?>>
                        <?= htmlspecialchars($fournisseur['raison_sociale']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2"><label class="form-label">État</label><select name="etat" class="form-select"><option value="actives" <?= ($filters['etat']??'actives')==='actives'?'selected':'' ?>>Actives</option><option value="annulees" <?= ($filters['etat']??'')==='annulees'?'selected':'' ?>>Annulées</option><option value="toutes" <?= ($filters['etat']??'')==='toutes'?'selected':'' ?>>Toutes</option></select></div>
        <div class="col-md-3">
            <label class="form-label">Statut</label>
            <select name="statut" class="form-select">
                <option value="">Tous</option>
                <?php foreach ($statusLabels as $key => $label): ?>
                    <option value="<?= htmlspecialchars($key) ?>" <?= ($filters['statut'] ?? '') === $key ? 'selected' : '' ?>><?= htmlspecialchars($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <button class="btn btn-outline-primary w-100">Filtrer</button>
        </div>
    </form>
</div>

<div class="darlink-card p-4">
    <div class="table-responsive">
        <table class="table darlink-table align-middle">
            <thead>
            <tr>
                <th class="darlink-sortable" data-column="0">Facture <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="1">Fournisseur <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="2">Commande <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="3">Date <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="4">Échéance <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="5">TTC <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="6">Réglé <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="7">Reste dû <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="8">Statut <span class="sort-indicator">↕</span></th>
                <th class="text-end">Action</th>
            </tr>
            </thead>
            <tbody>
            <?php if (empty($factures)): ?>
                <tr><td colspan="10" class="text-center text-muted py-4">Aucune facture fournisseur.</td></tr>
            <?php endif; ?>
            <?php foreach ($factures as $facture): ?>
                <tr>
                    <td data-sort-value="<?= htmlspecialchars($facture['numero_interne']) ?>">
                        <strong><?= htmlspecialchars($facture['numero_interne']) ?></strong><br>
                        <span class="text-muted"><?= htmlspecialchars($facture['numero_facture_fournisseur']) ?></span>
                    </td>
                    <td data-sort-value="<?= htmlspecialchars($facture['fournisseur_nom']) ?>"><?= htmlspecialchars($facture['fournisseur_nom']) ?></td>
                    <td data-sort-value="<?= htmlspecialchars($facture['commande_numero'] ?: '') ?>"><?= htmlspecialchars($facture['commande_numero'] ?: '-') ?></td>
                    <td data-sort-value="<?= htmlspecialchars((string)$facture['date_facture']) ?>"><?= htmlspecialchars(date('d/m/Y', strtotime((string)$facture['date_facture']))) ?></td>
                    <td data-sort-value="<?= htmlspecialchars((string)($facture['date_echeance'] ?? '')) ?>"><?= !empty($facture['date_echeance']) ? htmlspecialchars(date('d/m/Y', strtotime((string)$facture['date_echeance']))) : '-' ?></td>
                    <td class="text-end" data-sort-value="<?= (float)$facture['montant_ttc'] ?>"><strong><?= $formatMoney($facture['montant_ttc']) ?></strong></td>
                    <td class="text-end" data-sort-value="<?= (float)$facture['montant_regle'] ?>"><?= $formatMoney($facture['montant_regle']) ?></td>
                    <td class="text-end" data-sort-value="<?= (float)$facture['reste_du'] ?>"><strong><?= $formatMoney($facture['reste_du']) ?></strong></td>
                    <td data-sort-value="<?= htmlspecialchars($statusLabels[$facture['statut']] ?? $facture['statut']) ?>"><?= htmlspecialchars($statusLabels[$facture['statut']] ?? $facture['statut']) ?></td>
                    <td class="text-end">
                        <div class="d-flex justify-content-end gap-1">
                            <a href="index.php?page=factures_fournisseurs_show&id=<?= (int)$facture['id'] ?>" class="btn btn-sm btn-outline-primary">Ouvrir</a>
                            <?php if((int)$facture['actif']===1): ?><a href="index.php?page=factures_fournisseurs_edit&id=<?= (int)$facture['id'] ?>" class="btn btn-sm btn-outline-secondary">Modifier</a><?php else: ?><span class="badge bg-danger">Annulée</span><?php endif; ?>
                            <?php if ((float)$facture['reste_du'] > 0): ?>
                                <a href="index.php?page=factures_fournisseurs_reglement&id=<?= (int)$facture['id'] ?>" class="btn btn-sm btn-success">Régler</a>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
