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

error_reporting(0);

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

    $response = array('success' => false, 'message' => '');

    //$membershipTypesQuery = "SELECT id, type, amount FROM membership_types";
    //$membershipTypesResult = $conn->query($membershipTypesQuery);

    if (isset($_GET['id'])) 
    {
        $memberId = $_GET['id'];

        $fetchMemberQuery = "SELECT * FROM members_data WHERE id = $memberId";
        $fetchMemberResult = $conn->query($fetchMemberQuery);

        if ($fetchMemberResult->num_rows > 0) 
        {
            $memberDetails = $fetchMemberResult->fetch_assoc();
        } 
        else 
        {
            header("Location: members_list.php");
            exit();
        }
}

function generateUniqueFileName($filename)
{
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $basename = pathinfo($filename, PATHINFO_FILENAME);
    $uniqueName = $basename . '_' . time() . '.' . $ext;
    return $uniqueName;
}

    if ($_SERVER["REQUEST_METHOD"] == "POST") 
    {
        $nom = $_POST['nom'];
        $prenom = $_POST['prenom'];
        $email = $_POST['email'];
        $statut = $_POST['statut'];
        $privilege = $_POST['privilege'];
        
        //$photoUpdate = "";
        //$uploadedPhoto = $_FILES['photo'];

        /*if (!empty($uploadedPhoto['name'])) 
        {
            $uniquePhotoName = generateUniqueFileName($uploadedPhoto['name']);
            move_uploaded_file($uploadedPhoto['tmp_name'], 'uploads/member_photos/' . $uniquePhotoName);
            $photoUpdate = ", photo='$uniquePhotoName'";
        }*/

       /* $updateQuery = "UPDATE members SET fullname='$fullname', dob='$dob', gender='$gender', 
                    contact_number='$contactNumber', email='$email', address='$address', country='$country', 
                    postcode='$postcode', occupation='$occupation' $photoUpdate
                    WHERE id = $memberId";
*/
        $updateQuery = "UPDATE members_data SET nom='$nom', prenom='$prenom', email='$email', statut='$statut', privilege='$privilege' WHERE id = $memberId";
                    

        if ($conn->query($updateQuery) === TRUE) 
        {
            $response['success'] = true;
            $response['message'] = 'Member updated successfully!';

            // mise à jour
            $fetchMemberQuery = "SELECT * FROM members_data WHERE id = $memberId";
            $fetchMemberResult = $conn->query($fetchMemberQuery);
    
            if ($fetchMemberResult->num_rows > 0) 
            {
                $memberDetails = $fetchMemberResult->fetch_assoc();
            } 

           // header("Location: gestion_membres.php");
           // exit();
        } 
        else 
        {
            $response['message'] = 'Error: ' . $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">
                    <!-- left column -->
                    <div class="col-md-12">

                        <?php if ($response['success']): ?>
                            <div class="alert alert-success alert-dismissible">
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                <h5><i class="icon fas fa-check"></i> Success</h5>
                                <?php echo $response['message']; ?>
                            </div>
                        <?php elseif (!empty($response['message'])): ?>
                            <div class="alert alert-danger alert-dismissible">
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                <h5><i class="icon fas fa-ban"></i> Error</h5>
                                <?php echo $response['message']; ?>
                            </div>
                        <?php endif; ?>

                        <!-- general form elements -->
                        <div class="card card-primary">
                            <div class="card-header">
                                <h3 class="card-title"><i class="fas fa-keyboard"></i> Edit Member Details</h3>
                            </div>
                            <!-- /.card-header -->
                            <!-- form start -->
                            <form method="post" action="" enctype="multipart/form-data">
                            <input type="hidden" name="member_id" value="<?php echo $memberId; ?>">
                                <div class="card-body">

                                    <div class="row">
                                    <div class="col-sm-6">
                                    <label for="nom">Nom</label>
                                    <input type="text" class="form-control" id="nom" name="nom"
                                        placeholder="Saisir le nom" required value="<?php echo $memberDetails['nom']; ?>">
                                    </div>
                                    </div>
    
                                    <div class="row mt-3">
                                        <div class="col-sm-6">
                                            <label for="contactNumber">Prénom</label>
                                            <input type="text" class="form-control" id="prenom" name="prenom" 
                                            placeholder="Saisir le prénom" value="<?php echo $memberDetails['prenom']; ?>" required>
                                    </div>
                                    </div>

                                    <div class="row mt-3">
                                    <div class="col-sm-6">
                                            <label for="address">Email</label>
                                            <input type="text" class="form-control" id="email" name="email"
                                                   placeholder="Saisir l'email" value="<?php echo $memberDetails['email']; ?>" required>
                                    </div>
                                    </div>

                                    <div class="row mt-3">
                                    <div class="col-sm-6">
                                            <label for="postcode">Statut</label>
                                            <input type="text" class="form-control" id="statut" name="statut"
                                                   placeholder="Enter postcode" value="<?php echo $memberDetails['statut']; ?>" required>
                                    </div>
                                    </div>
                                    
                                    <!--
                                    <div class="row mt-3">
                                    <div class="col-sm-6">
                                        <label for="photo">Member Photo</label>
                                        <input type="file" class="form-control" id="photo" name="photo">
                                        <small class="text-muted">Leave it blank if you don't want to change the photo.</small>
                                    </div>
                                    </div>
                                    -->

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

                                <div class="card-footer">
                                    <button type="submit" class="btn btn-primary">Submit</button>
                                </div>
                            </form>
                        </div>
                        <!-- /.card -->

                    </div>
                    <!--/.col (left) -->

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


            </div><!--/. container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

    <!-- 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');?> codeastro.com</a> -</strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
      <b>Developed By</b> <a href="https://codeastro.com/">CodeAstro</a>
    </div>
  </footer>
</div>
<!-- ./wrapper -->

<?php include('includes/footer.php'); ?>
</body>
</html>
