<?php

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

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

    $adminID = $_GET['admin'];
}

$admin = $db_handle->runBaseQuery("SELECT * FROM admins WHERE id = $adminID");


$username = $admin[0]['username'];
$email = $admin[0]['email'];



if(isset($_POST['edit'])){
     if (!isset($_POST['csrf_token']) || !$csrf->validateToken($_POST['csrf_token'])) {
        die("CSRF token validation failed!");
    }
    $email = $db_handle->sanitize($_POST['email']);
    $password = $db_handle->sanitize($_POST['password']);
    $username = $db_handle->sanitize($_POST['username']);
    $role = $db_handle->sanitize($_POST['role']);

   


    $password = password_hash($password, PASSWORD_DEFAULT);

    $insert = $db_handle->update("UPDATE admins SET email = ?, password = ?, username = ?, role = ? WHERE id = ?","ssssi",array($email,$password,$username,$role,$adminID));

if($insert){

$_SESSION['msg-s'] = "Admin edited successfully.";
}else{
$_SESSION['msg-e'] = "Could not edit admin.";
}
}

 ?>
 <div class="main-content app-content">
<div class="container-fluid">
                            
                            <!--Modal: order-modal-->
                           
                              
    <div class="row settings mt-3">

        

        <?php if (isset($_SESSION['msg-s'])) { ?>
        <div class="alert alert-success alert-dismissible">
                    
                    <h4><i class="icon fa fa-check"></i> Success!</h4> <?php echo $_SESSION['msg-s']; ?>
                </div>
<?php unset($_SESSION['msg-s']); } ?>

<?php if (isset($_SESSION['msg-e'])) { ?>
        <div class="alert alert-danger alert-dismissible" role="alert">
                   
                    <h4><i class="icon fa fa-check"></i> Error!</h4> <?php echo $_SESSION['msg-e']; ?>
                </div>
<?php unset($_SESSION['msg-e']); } ?>
        
        <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 Admin</h5>
                                    </div>
                                    <form method="POST"
                                        action=""
                                        enctype="multipart/form-data">
                                        <?php echo $csrf->csrfInput(); ?>                                      
                                            <div class="form-group mb-3">
                                                <label class="form-label">Username<span
                                                        class="text-danger"> * </span></label>
                                                <input type="text" class="form-control modern-input" name="username"
                                                    value="<?php echo $username; ?>"
                                                    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" required>

                                            </div>

                                            <div class="form-group mb-3">
                                                <label class="form-label">Password<span
                                                        class="text-danger"> * </span></label>
                                                <input type="password" class="form-control modern-input" name="password"
                                                    value=""
                                                    placeholder="Password" required>

                                            </div>
                                                 
                                                <div class="form-group mb-3">
                                                    <label for="city"
                                                        class="form-label">Admin Type<span
                                                            class="text-danger">
                                                            * </span></label>
                                                    <select name="role" class="form-select form-input" id="role" required>
                                                        <option value="">-- Select --</option>

                                                            <option value="Admin"selected>Normal Admin</option>
                                                            <option value="Super-Admin"selected>Super Admin</option>
                                                    </select>
                                                </div>
                                                
                                                                                        <div class="form-group text-end">
                                                <button
                                                    class="btn modern-btn"
                                                     type="submit" name="edit" value="1" >Edit Admin</button>
                                            </div>
                                        
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
               



                                                                                                                                                                                                                                                                                                    <div id="delete_profile">
                        
                    </div>
                  
                               

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

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