<?php

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

if(isset($_POST['create'])){
     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']);

   $adminExist = $db_handle->runBaseQuery("SELECT * FROM admins WHERE username = '$username' OR email = '$email'");

if($adminExist){
$_SESSION['msg-e'] = "Email or Username already exists";
}else{

    $password = password_hash($password, PASSWORD_DEFAULT);

    $insert = $db_handle->insertRecord("INSERT INTO admins(email,password,username,role) VALUES(?,?,?,?)","ssss",array($email,$password,$username,$role));

if($insert){

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

}
 ?>
 <div class="main-content app-content">
<div class="container-fluid">
                            
                            <!--Modal: order-modal-->
                            <div class="modal fade" id="order-modal" tabindex="-1" role="dialog"
                                aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div class="modal-dialog modal-notify modal-info" role="document">
                                    <div class="modal-content text-center">
                                        <div class="modal-header d-flex justify-content-center">
                                            <p class="heading">Be up to date</p>
                                        </div>
                                        <div class="modal-body"><i class="fa fa-bell fa-4x animated rotateIn mb-4"></i>
                                            <p>New order arrived</p>
                                        </div>
                                        <div class="modal-footer flex-center">
                                            <a role="button" class="btn btn-outline-secondary-modal waves-effect"
                                                onClick="window.location.reload();"
                                                data-bs-dismiss="modal">Okay</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                               
    <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">Create Admin</h5>
                                    </div>
                                    <form method="POST"
                                        action=""
                                        enctype="multipart/form-data">
                                        <?php echo $csrf->csrfInput(); ?>                                     
                                            <div class="form-group mb-2">
                                                <label class="form-label">Username<span
                                                        class="text-danger"> * </span></label>
                                                <input type="text" class="form-control modern-input" name="username"
                                                    value=""
                                                    placeholder="Name" required>

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

                                            </div>

                                            <div class="form-group mb-2">
                                                <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-2">
                                                    <label for="city"
                                                        class="form-label">Admin Type<span
                                                            class="text-danger">
                                                            * </span></label>
                                                    <select name="role" class="form-select modern-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 btn-secondary  "
                                                     type="submit" name="create" >Add 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"); 
 ?>