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

// Commandes fournisseurs
$commandes = $conn->query("
  SELECT cf.id, cf.numero, cf.date_commande, cf.statut,
         f.nom AS fournisseur,
         COUNT(d.id) AS nb_lignes
  FROM commandes_fournisseurs cf
  JOIN fournisseurs f ON f.id = cf.fournisseur_id
  LEFT JOIN commandes_fournisseurs_details d
         ON d.commande_id = cf.id
  GROUP BY cf.id
  ORDER BY cf.date_commande DESC
")->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="container-fluid px-2">
    <h1 class="mt-4">Achats fournisseurs</h1>

    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item">
            <a href="index.php?page=dashboard">Tableau de bord</a>
        </li>
        <li class="breadcrumb-item active">Achats 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-truck"></i> Liste des commandes fournisseurs
            </span>

            <a href="?page=commande_fournisseur_add"
               class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> Ajouter
            </a>
        </div>

        <div class="card-body">

          <table class="table table-bordered table-striped datatable">
          <thead class="table-dark">
          <tr>
            <th>N°</th>
            <th>Date</th>
            <th>Fournisseur</th>
            <th>Lignes</th>
            <th>Statut</th>
            <th>Actions</th>
          </tr>
          </thead>
          <tbody>

          <?php foreach ($commandes as $c): ?>
          <tr>
            <td><?= htmlspecialchars($c['numero']) ?></td>
            <td><?= date('d/m/Y H:i', strtotime($c['date_commande'])) ?></td>
            <td><?= htmlspecialchars($c['fournisseur']) ?></td>
            <td class="text-center"><?= $c['nb_lignes'] ?></td>
            <td>
              <span class="badge bg-<?= 
                $c['statut']=='reçue' ? 'success' :
                ($c['statut']=='partielle' ? 'warning' : 'secondary')
              ?>">
                <?= $c['statut'] ?>
              </span>
            </td>
            <td class="text-center">

              <!-- Réception -->
              <?php if (in_array($c['statut'], ['envoyee','partielle'])): ?>
                <a href="?page=reception_fournisseur&id=<?= $c['id'] ?>"
                  class="btn btn-success btn-sm"
                  title="Réception">
                  <i class="fa fa-truck"></i>
                </a>
              <?php endif; ?>

              <!-- Voir -->
              <a href="?page=commande_fournisseur_view&id=<?= $c['id'] ?>"
                class="btn btn-info btn-sm">
                <i class="fa fa-eye"></i>
              </a>

            </td>
          </tr>
          <?php endforeach ?>

          </tbody>
          </table>
</div>

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

<script>
$(function(){
  $('.datatable').DataTable({
    responsive:true,
    language:{ url:"/eagle_client_istanbulcups/public/js/fr-FR.json" }
  });
});
</script>
