<?php
include('includes/config.php');


    //$selectQuery = "SELECT * FROM members_data ORDER BY created_at DESC";
    //$result = $conn->query($selectQuery);


    if (!isset($_SESSION['user_id'])) 
    {
        header("Location: index.php");
        exit();
    }


    if ($_SERVER["REQUEST_METHOD"] == "POST") 
    {
        $membershipType = $_POST['membershipType'];
        $membershipAmount = $_POST['membershipAmount'];

        $insertQuery = "INSERT INTO membership_types (type, amount) VALUES ('$membershipType', $membershipAmount)";
    
    if ($conn->query($insertQuery) === TRUE) {
        $successMessage = 'Membership type added successfully!';
        // header("Location: dashboard.php");
        // exit();
    } else {
        echo "Error: " . $insertQuery . "<br>" . $conn->error;
    }
}


?>

<?php include('includes/header.php');?>

<body class="hold-transition sidebar-mini layout-fixed layout-navbar-fixed layout-footer-fixed">
<div class="wrapper">
  <?php include('includes/nav.php');?>

 <?php include('includes/sidebar.php');?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    
  <?php include('includes/pagetitle.php');?>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Info boxes -->
        <div class="row">
        
        <div class="col-12">

        <div class="card">
    <div class="card-header">
        <h3 class="card-title">Table des membres enregistrés dans la database.</h3>
    </div>
    <!-- Visit codeastro.com for more projects -->
    <!-- /.card-header -->
    <div class="card-body">
    <table id="example1" class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>#</th>
                <th>Nom</th>
                <th>Prénom</th>
                <th>Email</th>
                <th>Statut</th>
                <th>Privilége</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $counter = 1;

            $selectQuery = "SELECT * FROM members_data ORDER BY created_at DESC";
            $result = $conn->query($selectQuery);

            while ($row = $result->fetch_assoc()) 
            {
                
                //$expiryDate = strtotime($row['expiry_date']);
                //$currentDate = time();
                //$daysDifference = floor(($expiryDate - $currentDate) / (60 * 60 * 24));
                //$membershipStatus = ($daysDifference < 0) ? 'Expired' : 'Active';

                //$membershipTypeId = $row['membership_type'];
                //$membershipTypeQuery = "SELECT type FROM membership_types WHERE id = $membershipTypeId";
                //$membershipTypeResult = $conn->query($membershipTypeQuery);
                //$membershipTypeRow = $membershipTypeResult->fetch_assoc();
                //$membershipTypeName = ($membershipTypeRow) ? $membershipTypeRow['type'] : 'Unknown';

/*
                `nom` varchar(255) NOT NULL,
                `prenom` varchar(255) NOT NULL,
                `email` varchar(255) NOT NULL,
                `user_id` varchar(20) NOT NULL,
                `statut` varchar(30) NOT NULL,
                `privilege` varchar(30) NOT NULL,
                `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                `expiry_date` date DEFAULT NULL,
*/
                echo "<tr>";
               
                echo "<td>{$row['user_id']}</td>";
                echo "<td>{$row['nom']}</td>";
                echo "<td>{$row['prenom']}</td>";
                echo "<td>{$row['email']}</td>";
               
                echo "<td>{$row['statut']}</td>";
                echo "<td>{$row['privilege']}</td>";

                echo "<td>";
/*
                if (!empty($row['expiry_date'])) {
*/                
                    echo "<a href='profile_membre.php?id={$row['id']}' class='btn btn-info' title='Voir le profile'><i class='fas fa-id-card'></i></a>";
/*                }
*/
                echo "
                    <a href='edition_membre.php?id={$row['id']}' class='btn btn-primary' title='Edition du profil' ><i class='fas fa-edit'></i></a>
                    <button class='btn btn-danger' title='Supprimer le membre' onclick='deleteMember({$row['id']})'><i class='fas fa-trash'></i></button>
                </td>";
                echo "</tr>";

                $counter++;
            }
            ?>
        </tbody>
    </table>
</div>

    <!-- /.card-body -->
</div>
            <!-- /.card -->
          </div>
          <!-- /.col -->

        </div>
        <!-- /.row -->

        
      </div><!--/. container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  <!-- Visit codeastro.com for more projects -->
  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->

  <!-- Main Footer -->
  <footer class="main-footer">
    <strong> &copy; <?php echo date('Y');?> Patoche</a> -</strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
      <b>Developed By</b> <a href="https://analog-design.net/">Zoulou</a>
    </div>
  </footer>
</div>
<!-- ./wrapper -->

<?php include('includes/footer.php');?>
<script>
  $(function () {
    $("#example1").DataTable({
      "responsive": true,
      "autoWidth": false,
      
      
          "language": {
              "url": " https://cdn.datatables.net/plug-ins/1.12.0/i18n/fr-FR.json"
          } 


    });
    $('#example2').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false,
      "responsive": true,
    });
  });
</script>

<script>
    function deleteMember(id) {
        if (confirm("Are you sure you want to delete this member?")) {
            window.location.href = 'delete_members.php?id=' + id;
        }
    }
</script>

</body>
</html>