<?php include 'layouts/session.php'; ?>
<?php include 'layouts/main.php'; ?>
<?php 

    include 'layouts/config.php';

    $getFournisseurs = mysqli_query($link, "SELECT * FROM `fournisseurs`");

    $getPaysAdd = mysqli_query($link, "SELECT * FROM `pays`");

?>
<head>

<?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Fournisseurs')); ?>

<!--datatable css-->
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap5.min.css" />
<!--datatable responsive css-->
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.9/css/responsive.bootstrap.min.css" />

<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.2.2/css/buttons.dataTables.min.css">


<?php include 'layouts/head-css.php'; ?>

</head>

<body>

    <!-- Begin page -->
    <div id="layout-wrapper">

        <?php include 'layouts/menu.php'; ?>

        <!-- ============================================================== -->
        <!-- Start right Content here -->
        <!-- ============================================================== -->
        <div class="main-content">

            <div class="page-content">
                <div class="container-fluid">

                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Referentiel', 'title' => 'fournisseurs')); ?>

                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card" id="customerList">
                                <div class="card-header border-bottom-dashed">

                                    <div class="row g-4 align-items-center">
                                        <div class="col-sm">
                                            <div>
                                                <h5 class="card-title mb-0">Liste des fournisseurs</h5>
                                            </div>
                                        </div>
                                        <div class="col-sm-auto">
                                            <div class="d-flex flex-wrap align-items-start gap-2">
                                                <button class="btn btn-soft-danger" id="remove-actions" onClick="deleteMultiple()"><i class="ri-delete-bin-2-line"></i></button>
                                                <button type="button" class="btn btn-primary add-btn" data-bs-toggle="modal" id="create-btn" data-bs-target="#showModal"><i class="ri-add-line align-bottom me-1"></i> Ajouter un fournisseur</button>
                                                <!-- <button type="button" class="btn btn-secondary"><i class="ri-file-download-line align-bottom me-1"></i> Importer</button> -->
                                            </div>
                                        </div>
                                    </div>
                                    
                                </div>
                                </br>
                                <div class="row">
                                   <?php include('confirmation.php');?>
                                </div>
                                <div class="card-body">
                                    <table id="example" class="table table-bordered dt-responsive nowrap table-striped align-middle" style="width:100%">
                                        <thead class="table-light text-muted">
                                            <tr>
                                                <th>CODE</th>
                                                <th>NOM</th>
                                                <th>PAYS</th>
                                                <th>Telephone</th>
                                                <th class="sort" data-sort="action">Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                                                
                                                while($row =mysqli_fetch_assoc($getFournisseurs))
                                                {
                                                $id = $row['id'];
                                                $code_fn = $row['code_fournisseur'];
                                                $nom_fournisseur = $row['nom_fournisseur'];
                                                $pays = $row['pays'];
                                                $telephone = $row['telephone'];

                                                $getPaysEdit = mysqli_query($link, "SELECT * FROM `pays` WHERE pays <> '$pays'");
                                                ?>
                                                <tr>
                                                    <td><?php echo $code_fn;?></a></td>
                                                    <td><?php echo $nom_fournisseur;?></a></td>
                                                    <td><?php echo $pays;?></a></td>
                                                    <td><?php echo $telephone;?></a></td>

                                                    <td>
                                                        <ul class="list-inline hstack gap-2 mb-0">
                                                            <li class="list-inline-item edit" data-bs-toggle="tooltip" data-bs-trigger="hover" data-bs-placement="top" title="Edit">
                                                                <a href="#editModal<?php echo $code_fn;?>" data-bs-toggle="modal" class="text-primary d-inline-block edit-item-btn">
                                                                    <i class="ri-pencil-fill fs-16"></i>
                                                                </a>
                                                            </li>
                                                            <li class="list-inline-item" data-bs-toggle="tooltip" data-bs-trigger="hover" data-bs-placement="top" title="Remove">
                                                                <a class="text-danger d-inline-block remove-item-btn" data-bs-toggle="modal" href="#deleteModal<?php echo $code_fn;?>">
                                                                    <i class="ri-delete-bin-5-fill fs-16"></i>
                                                                </a>
                                                            </li>
                                                        </ul>
                                                    </td>
                                                </tr>
                                                 <!-- EDIT MODAL -->
                                                <div class="modal fade" id="editModal<?php echo $code_fn;?>" tabindex="-1" aria-hidden="true">
                                                    <div class="modal-dialog modal-dialog-centered">
                                                        <div class="modal-content">
                                                            <div class="modal-header bg-light p-3">
                                                                <h5 class="modal-title" id="exampleModalLabel"></h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="close-modal"></button>
                                                            </div>
                                                            <form  action="controllers/controller_gen.php" method="POST" >
                                                                <div class="modal-body">
                                                                    <input type="hidden" name="code_fn" value="<?php echo $code_fn;?>">

                                                                    <div class="mb-3">
                                                                        <label for="customername-field" class="form-label">Founisseur</label>
                                                                        <input type="text" name="nom_fournisseur" class="form-control" value="<?php echo $nom_fournisseur; ?>" required />
                                                                    </div>

                                                                    <div class="mb-3">
                                                                        <label for="customername-field" class="form-label">Pays</label>
                                                                        <select name="pays" class="form-control" required>
                                                                            <option value="<?php echo $pays; ?>"><?php echo $pays; ?></option>
                                                                                <?php while($row2 =mysqli_fetch_assoc($getPaysEdit)) { 
                                                                                $pays_r=$row2['pays'];
                                                                                // if ($pays_r != $pays) {
                                                                                ?>
                                                                                <option value="<?php echo $pays_r; ?>"><?php echo $pays_r; ?></option>
                                                                            <?php } ?>
                                                                           
                                                                        </select>
                                                                    </div>

                                                                    <div class="mb-3">
                                                                        <label for="phone-field" class="form-label">Telephone</label>
                                                                        <input type="text" name="telephone" class="form-control" value="<?php echo $telephone; ?>" required />
                                                                    </div>

                                                                </div>
                                                                <div class="modal-footer">
                                                                    <div class="hstack gap-2 justify-content-end">
                                                                        <button type="button" class="btn btn-light" data-bs-dismiss="modal">Fermer</button>
                                                                        <button type="submit" name="updateFournisseur" class="btn btn-success">Modifier</button>
                                                                        <!-- <button type="button" class="btn btn-success" id="edit-btn">Update</button> -->
                                                                    </div>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>

                                                 <!-- Delete Modal -->
                                                <div class="modal fade zoomIn" id="deleteModal<?php echo $code_fn;?>" tabindex="-1" aria-hidden="true">
                                                    <div class="modal-dialog modal-dialog-centered">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <button type="button" class="btn-close" id="deleteRecord-close" data-bs-dismiss="modal" aria-label="Close" id="btn-close"></button>
                                                            </div>
                                                            <form  action="controllers/controller_gen.php" method="POST" >
                                                                <input type="hidden" name="code_fn" value="<?php echo $code_fn;?>">
                                                                <div class="modal-body">
                                                                    <div class="mt-2 text-center">
                                                                        <lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop" colors="primary:#f7b84b,secondary:#f06548" style="width:100px;height:100px"></lord-icon>
                                                                        <div class="mt-4 pt-2 fs-15 mx-4 mx-sm-5">
                                                                            <h4>Suppresion de <?php echo $nom_fournisseur;?></h4>
                                                                            <p class="text-muted mx-4 mb-0">Souhaitez-vous vraiment supprimer le client ?</p>
                                                                        </div>
                                                                    </div>
                                                                    <div class="d-flex gap-2 justify-content-center mt-4 mb-2">
                                                                        <button type="button" class="btn w-sm btn-light" data-bs-dismiss="modal">Fermer</button>
                                                                        <button type="submit" name="deleteFournisseur" class="btn w-sm btn-danger" id="delete-record">Oui, supprimer !</button>
                                                                    </div>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                                <!--end modal -->

                                            <?php } ?>
                                        </tbody>
                                       
                                    </table>
                                    <!-- ADD MODAL -->
                                    <div class="modal fade" id="showModal" tabindex="-1" aria-hidden="true">
                                        <div class="modal-dialog modal-dialog-centered">
                                            <div class="modal-content">
                                                <div class="modal-header bg-light p-3">
                                                    <h5 class="modal-title" id="exampleModalLabel"></h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="close-modal"></button>
                                                </div>
                                                <form  action="controllers/controller_gen.php" method="POST" >
                                                    <div class="modal-body">
                                                        <div class="mb-3">
                                                            <label for="customername-field" class="form-label">Fournisseur</label>
                                                            <input type="text" name="nom_fournisseur" class="form-control" placeholder="Nom du fournisseur" required />
                                                        </div>

                                                        <div class="mb-3">
                                                            <label for="customername-field" class="form-label">Pays</label>
                                                            <select name="pays" class="form-control" required>
                                                            <?php while($row =mysqli_fetch_assoc($getPaysAdd)) { 
                                                                $pays=$row['pays'];
                                                                ?>
                                                                <option value="<?php echo $pays; ?>"><?php echo $pays; ?></option>
                                                            <?php } ?>
                                                            </select>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label for="phone-field" class="form-label">Telephone</label>
                                                            <input type="number" name="telephone" class="form-control" value="<?php echo $telephone; ?>" required />
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <div class="hstack gap-2 justify-content-end">
                                                            <button type="button" class="btn btn-light" data-bs-dismiss="modal">Fermer</button>
                                                            <button type="submit" name="addFournisseur" class="btn btn-success">Ajouter client</button>
                                                            <!-- <button type="button" class="btn btn-success" id="edit-btn">Update</button> -->
                                                        </div>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <!--end col-->
                    </div>
                    <!--end row-->

                </div>
                <!-- container-fluid -->
            </div>
            <!-- End Page-content -->

            <?php include 'layouts/footer.php'; ?>
        </div>
        <!-- end main content-->

    </div>
    <!-- END layout-wrapper -->

    <?php include 'layouts/vendor-scripts.php'; ?>
 
    <script src="assets/libs/datatables/jquery-3.6.0.min.js"></script>

    <!--datatable js-->
    <script src="assets/libs/datatables/datatables.min.js"></script>
    <script src="assets/libs/datatables/datatables.bootstrap5.min.js"></script>

    <script src="assets/js/pages/datatables.init.js"></script>
    <!-- App js -->
    <script src="assets/js/app.js"></script>

</body>

</html>