<?php
session_start();
include "includes/layout.php";
include __DIR__ . '/../database/search_log_config.php';

$admin_id = $_SESSION['admin_id'];
$message = "";
$error = "";

/* ===== FETCH CURRENT ADMIN DATA ===== */
$stmt = $log_conn->prepare("SELECT admin_name, admin_email FROM admin_users WHERE id = ?");
$stmt->bind_param("i", $admin_id);
$stmt->execute();
$result = $stmt->get_result();
$admin = $result->fetch_assoc();
$stmt->close();

/* ===== UPDATE PROFILE ===== */
if($_SERVER["REQUEST_METHOD"] === "POST"){

    $new_name  = trim($_POST['admin_name']);
    $new_email = trim($_POST['admin_email']);
    $current_password = $_POST['current_password'];
    $new_password = $_POST['new_password'];

    // Verify current password
    $stmt = $log_conn->prepare("SELECT admin_password FROM admin_users WHERE id = ?");
    $stmt->bind_param("i", $admin_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    $stmt->close();

    if(password_verify($current_password, $row['admin_password'])){

        if(!empty($new_password)){
            $hashed = password_hash($new_password, PASSWORD_DEFAULT);
            $update = $log_conn->prepare("
                UPDATE admin_users 
                SET admin_name=?, admin_email=?, admin_password=? 
                WHERE id=?
            ");
            $update->bind_param("sssi", $new_name, $new_email, $hashed, $admin_id);
        } else {
            $update = $log_conn->prepare("
                UPDATE admin_users 
                SET admin_name=?, admin_email=? 
                WHERE id=?
            ");
            $update->bind_param("ssi", $new_name, $new_email, $admin_id);
        }

        $update->execute();
        $update->close();

        include "includes/activity_logger.php";
        logActivity($log_conn, $admin_id, "Updated Profile Settings");

        $_SESSION['admin_name'] = $new_name;

        $message = "Profile updated successfully!";

    } else {
        $error = "Current password is incorrect!";
    }
}
?>

<h1>Admin Profile Settings</h1>

<div class="card" style="max-width:600px;">

<?php if($message): ?>
<p style="color:green;"><?= $message ?></p>
<?php endif; ?>

<?php if($error): ?>
<p style="color:red;"><?= $error ?></p>
<?php endif; ?>

<form method="POST">

<label>Admin Name</label>
<input type="text" name="admin_name"
value="<?= htmlspecialchars($admin['admin_name']) ?>"
required
style="width:100%;padding:12px;margin-bottom:15px;border-radius:8px;border:1px solid #ddd;">

<label>Admin Email</label>
<input type="email" name="admin_email"
value="<?= htmlspecialchars($admin['admin_email']) ?>"
required
style="width:100%;padding:12px;margin-bottom:15px;border-radius:8px;border:1px solid #ddd;">

<hr style="margin:20px 0;">

<label>Current Password</label>
<input type="password" name="current_password"
required
style="width:100%;padding:12px;margin-bottom:15px;border-radius:8px;border:1px solid #ddd;">

<label>New Password (Leave blank to keep same)</label>
<input type="password" name="new_password"
style="width:100%;padding:12px;margin-bottom:15px;border-radius:8px;border:1px solid #ddd;">

<button class="btn">Update Profile</button>

</form>

</div>

<?php
$log_conn->close();
include "includes/footer.php";
?>