<?php

include("includes/header.php"); 
include("includes/sidebar.php");

if(isset($_GET['user'])){

    $userID = $_GET['user'];
}

$user = $db_handle->runBaseQuery("SELECT * FROM users WHERE id = $userID");

$fname = $user[0]['first_name'];
$surname = $user[0]['surname'];
$email = $user[0]['email'];
$phone = $user[0]['phone'];
$state = $user[0]['state'];
$city = $user[0]['city'];
$address = $user[0]['address'];



if(isset($_POST['updateprofile'])){
    
    if (!isset($_POST['csrf_token']) || !$csrf->validateToken($_POST['csrf_token'])) {
        die("CSRF token validation failed!");
    }
$fname = $db_handle->sanitize($_POST['fname']);
$lname = $db_handle->sanitize($_POST['lname']);
$phone = $db_handle->sanitize($_POST['phone']);
$email = $db_handle->sanitize($_POST['email']);
$address = $db_handle->sanitize($_POST['address']);


$update = $db_handle->update("update users SET first_name = ?, surname = ?, address = ?, phone = ? WHERE id = ?","ssssi",array($fname,$lname,$address,$phone,$userID));

if($update){
   

    $msg =  "<div class='alert alert-success text-center'>Profile updated successfully.</div>";
        
    }

    }




 ?>
 <div class="main-content app-content">
<div class="container-fluid">
                            
                            <!--Modal: order-modal-->
                           
                                <h5 class="text-uppercase">
        Account Settings</h5>
    <div class="row settings mt-3">
        
        <div class="col-xl-12">
            <div id="settingmenuContent">
                
                                <div id="editprofile">
                    <div class="row mb-5">
                        <div class="col-12">
                            <div class="card border-0 box-shadow">
                                <div class="card-body pb-0">
                                    <div class="d-flex align-items-center mb-3">
                                        <h5 class="text-uppercase">Edit Profile</h5>
                                    </div>
                                    <form method="POST"
                                        action=""
                                        enctype="multipart/form-data">
                                        <?php echo $msg; ?>
                                        <?php echo $csrf->csrfInput(); ?>                                        
                                            <div class="form-group  mb-3">
                                                <label class="form-label">First Name<span
                                                        class="text-danger"> * </span></label>
                                                <input type="text" class="form-control modern-input" name="fname"
                                                    value="<?php echo $fname; ?>"
                                                    placeholder="Name" required>

                                            </div>
                                            <div class="form-group  mb-3">
                                                <label class="form-label">Last Name<span
                                                        class="text-danger"> * </span></label>
                                                <input type="text" class="form-control modern-input" name="lname"
                                                    value="<?php echo $surname; ?>"
                                                    placeholder="Name" required>

                                            </div>
                                            <div class="form-group  mb-3">
                                                <label class="form-label">Email<span
                                                        class="text-danger"> * </span></label>
                                                <input type="email" class="form-control modern-input" name="email"
                                                    value="<?php echo $email; ?>"
                                                    placeholder="Email" readonly>

                                            </div>
                                            <div class="form-group  mb-3">
                                                <label class="form-label"
                                                    for="mobile">Mobile<span
                                                        class="text-danger"> * </span></label>
                                                <input type="text" class="form-control modern-input mobile-number" name="phone"
                                                    id="mobile" value="<?php echo $phone; ?>"
                                                    placeholder="Mobile" required>

                                            </div>
                                            
                                             <div class="form-group  mb-3">
                                                <label class="form-label"
                                                    for="mobile">Address<span
                                                        class="text-danger"> * </span></label>
                                                <input type="text" class="form-control modern-input mobile-number" name="address"
                                                     value="<?php echo $address; ?>"
                                                    placeholder="Address" required>

                                            </div>
                                          
                                                                                       <div class="form-group text-end">
                                                <button
                                                    class="btn btn-secondary  "
                                                     type="submit" name="updateprofile" >Update</button>
                                            </div>
                                       
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
               



                                                                                                                                                                                                                                                                                                     

            </div>
        </div>
    </div>
    
   
                        </div>
                    </div>
                </div>
  <?php

include("includes/footer.php"); 
 ?>