<?php
require_once __DIR__ . "/../../config/db.php";

$stmt = $conn->query("
    SELECT pf.*, a.numero,
           f.nom AS fournisseur_nom,
           mp.libelle AS mode_paiement
    FROM paiements_fournisseurs pf
    JOIN achats a ON a.id = pf.achat_id
    JOIN fournisseurs f ON f.id = pf.fournisseur_id
    JOIN modes_paiement mp ON mp.id = pf.mode_paiement_id
    ORDER BY pf.date_paiement DESC
");

$paiements = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid px-2">
    <h1 class="mt-4">Paiements Fournisseurs</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="/eagle_client_istanbulcups/index.php?page=dashboard">Tableau de bord</a></li>
        <li class="breadcrumb-item active">Paiements Fournisseurs</li>
    </ol>

  <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span><i class="fa fa-money-bill-wave"></i> iste des paiements fournisseurs</span>       
        </div>
        
        <div class="card-body">
            <table id="paiementsTable" class="table table-bordered table-striped datatable">
                <thead class="table-dark">
                    <tr>
                        <th>Date</th>
                        <th>Achat</th>
                        <th>Fournisseur</th>
                        <th>Montant</th>
                        <th>Mode</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($paiements as $p): ?>
                    <tr>
                        <td><?= date("d/m/Y H:i", strtotime($p['date_paiement'])) ?></td>
                        <td><?= htmlspecialchars($p['numero']) ?></td>
                        <td><?= htmlspecialchars($p['fournisseur_nom']) ?></td>
                        <td><?= number_format($p['montant'],0,'',' ') ?> FCFA</td>
                        <td><?= htmlspecialchars($p['mode_paiement']) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>


<?php include __DIR__ . "/../../includes/layout_end.php"; ?>

<script>

$(document).ready(function() {
    var table = $('#paiementsTable').DataTable({
        responsive: true,
        language: {
            url: "/eagle_client_istanbulcups/public/js/fr-FR.json"
        }
    });
});
</script>