<?php
include '../includes/db.php';
include 'includes/header.php';

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = $conn->real_escape_string($_POST['email']);
    $full_name = $conn->real_escape_string($_POST['full_name']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    // Update Basic Info
    $sql_update = "UPDATE users SET email='$email', full_name='$full_name' WHERE id=$admin_id";
    if ($conn->query($sql_update) === TRUE) {
        $_SESSION['full_name'] = $full_name; // Update session
        $message = "Profile updated successfully!";
    } else {
        $message = "Error updating profile: " . $conn->error;
    }

    // Update Password if provided
    if (!empty($password)) {
        if ($password === $confirm_password) {
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $sql_pass = "UPDATE users SET password='$hashed_password' WHERE id=$admin_id";
            if ($conn->query($sql_pass) === TRUE) {
                $message .= " Password changed successfully!";
            } else {
                $message .= " Error changing password.";
            }
        } else {
            $message .= " Passwords do not match.";
        }
    }
}

// Fetch current admin data
$sql = "SELECT * FROM users WHERE id = $admin_id";
$result = $conn->query($sql);
$admin = $result->fetch_assoc();
?>

<div class="dashboard-container">
    <div class="sidebar admin-sidebar">
        <?php include 'includes/sidebar.php'; ?>
    </div>

    <div class="main-content">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Edit Admin Profile</h3>
            </div>
            
            <?php if($message): ?>
                <div class="alert alert-info"><?php echo $message; ?></div>
            <?php endif; ?>

            <form method="POST" action="">
                <div class="form-group">
                    <label>Full Name</label>
                    <input type="text" name="full_name" class="form-control" value="<?php echo $admin['full_name']; ?>" required>
                </div>
                <div class="form-group">
                    <label>Email Address</label>
                    <input type="email" name="email" class="form-control" value="<?php echo $admin['email']; ?>" required>
                </div>
                
                <hr style="margin: 30px 0; border: 0; border-top: 1px solid #eee;">
                
                <h4 style="margin-bottom: 20px;">Change Password <small style="color: #999; font-weight: normal;">(Leave blank to keep current)</small></h4>
                <div class="form-group">
                    <label>New Password</label>
                    <input type="password" name="password" class="form-control">
                </div>
                <div class="form-group">
                    <label>Confirm New Password</label>
                    <input type="password" name="confirm_password" class="form-control">
                </div>

                <div style="margin-top: 30px;">
                    <button type="submit" class="btn-primary" style="padding: 10px 30px;">Update Profile</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
