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

// Récupération factures
$factures = $conn->query("
    SELECT f.*, c.nom AS client_nom
    FROM factures_clients f
    JOIN clients c ON c.idClient = f.client_id
    ORDER BY f.id DESC
")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid px-2">

    <h1 class="mt-4">Encaissements clients</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">Encaissements</li>
    </ol>

    <div class="card">

        <div class="card-header d-flex justify-content-between align-items-center">
            <span><i class="fa fa-file-invoice"></i> Liste des encaissements</span>
        </div>

        <div class="card-body">

            <!-- FILTRE -->
            <!-- <div class="mb-3">
                <select id="filtreStatut" class="form-select w-auto">
                    <option value="">-- Tous les statuts --</option>
                    <option value="payee">Payée</option>
                    <option value="partiellement_payee">Partielle</option>
                    <option value="validee">Validée</option>
                    <option value="annulee">Annulée</option>
                </select>
            </div> -->

            <div class="table-responsive">

                <table id="facturesTable" class="table table-bordered table-striped datatable">

                    <thead class="table-dark">
                        <tr>
                            <th>N°</th>
                            <th>Date</th>
                            <th>Client</th>
                            <th>Total TTC</th>
                            <th>Payé</th>
                            <th>Reste</th>
                            <th>Statut</th>
                            <th style="width:160px;">Actions</th>
                        </tr>
                    </thead>

                    <tbody>

                    <?php foreach ($factures as $f): ?>
                    <tr>

                        <td><?= htmlspecialchars($f['numero']) ?></td>

                        <td><?= date('d/m/Y', strtotime($f['date_facture'])) ?></td>

                        <td><?= htmlspecialchars($f['client_nom']) ?></td>

                        <td><?= number_format($f['total_ttc'],2,',',' ') ?> FCFA</td>

                        <td class="text-success">
                            <?= number_format($f['montant_paye'],2,',',' ') ?>
                        </td>

                        <td class="text-danger">
                            <?= number_format($f['reste_a_payer'],2,',',' ') ?>
                        </td>

                        <td>
                            <?php
                            switch($f['statut']) {
                                case 'payee':
                                    echo '<span class="badge bg-success">Payée</span>';
                                break;

                                case 'partiellement_payee':
                                    echo '<span class="badge bg-warning text-dark">Partielle</span>';
                                break;

                                case 'validee':
                                    echo '<span class="badge bg-primary">Validée</span>';
                                break;

                                case 'annulee':
                                    echo '<span class="badge bg-danger">Annulée</span>';
                                break;

                                default:
                                    echo '<span class="badge bg-secondary">Brouillon</span>';
                            }
                            ?>
                        </td>

                        <td>

                            <!-- VOIR -->
                            <button class="btn btn-info btn-sm"
                                    data-bs-toggle="modal"
                                    data-bs-target="#voirFacture<?= $f['id'] ?>">
                                <i class="fa fa-eye"></i>
                            </button>

                            <!-- ENCAISSER -->
                            <?php if($f['reste_a_payer'] > 0): ?>
                            <button class="btn btn-success btn-sm"
                                    data-bs-toggle="modal"
                                    data-bs-target="#encaisser<?= $f['id'] ?>">
                                <i class="fa fa-money-bill"></i>
                            </button>
                            <?php endif; ?>

                            <!-- IMPRIMER -->
                               <?php if($f['reste_a_payer'] == 0): ?>
                            <a href="pages/rapports/utils/impression.php?type=facture&id=<?= $f['id'] ?>"
                               target="_blank"
                               class="btn btn-secondary btn-sm">
                                <i class="fa fa-print"></i>
                            </a>
                               <?php endif; ?>


                        </td>

                    </tr>
                    <?php endforeach; ?>

                    </tbody>

                </table>

            </div>
        </div>
    </div>
</div>


<?php foreach ($factures as $f): ?>

<?php
$details = $conn->query("
    SELECT fd.*, p.nom as produit
    FROM factures_clients_details fd
    JOIN produits p ON p.id = fd.produit_id
    WHERE fd.facture_id = ".$f['id']
)->fetchAll(PDO::FETCH_ASSOC);
?>

<?php include __DIR__."/modal_voir_facture.php"; ?>
<?php include __DIR__."/modal_encaisser.php"; ?>

<?php endforeach; ?>

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

<script>
$(document).ready(function() {

    var table = $('#facturesTable').DataTable({
        responsive: true,
        language: {
            url: "/eagle_client_istanbulcups/public/js/fr-FR.json"
        }
    });

    // filtre statut
    $('#filtreStatut').on('change', function () {
        var value = $(this).val();
        table.column(6).search(value).draw();
    });

});
</script>