<?php
session_start();
require_once 'db_connect.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if (!$id) {
    echo "Invalid landlord ID.";
    exit();
}

// Fetch current data
$stmt = $conn_rbe->prepare("SELECT * FROM rbe_users WHERE id = ?");
$stmt->execute([$id]);
$landlord = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$landlord) {
    echo "Landlord not found.";
    exit();
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $fullname = trim($_POST['fullname']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);

    if ($fullname && $email && $phone) {
        $stmt = $conn_rbe->prepare("UPDATE rbe_users SET fullname = ?, email = ?, phone = ? WHERE id = ?");
        $stmt->execute([$fullname, $email, $phone, $id]);

        header("Location: manage-landlords.php?updated=1");
        exit();
    } else {
        $error = "All fields are required.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Landlord</title>
    <style>
        body {
            background: #0a1a2f;
            color: #ffffff;
            font-family: Arial, sans-serif;
            padding: 40px;
        }

        .container {
            background: #122c4c;
            max-width: 500px;
            margin: auto;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 0 20px rgba(0,0,0,0.4);
        }

        h2 {
            text-align: center;
            color: #7ed957;
            margin-bottom: 30px;
        }

        label {
            font-weight: bold;
            margin-top: 10px;
            display: block;
        }

        input[type="text"],
        input[type="email"] {
            width: 100%;
            padding: 12px;
            margin-top: 5px;
            margin-bottom: 20px;
            border: none;
            border-radius: 8px;
            background: #1b3a4d;
            color: #fff;
        }

        button {
            width: 100%;
            background: #7ed957;
            color: #000;
            font-weight: bold;
            padding: 12px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background: #00cc77;
        }

        .error {
            background: #ff5555;
            color: #fff;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
        }

        a.back-link {
            color: #7ed957;
            display: inline-block;
            margin-top: 20px;
            text-align: center;
            width: 100%;
            text-decoration: none;
        }

        a.back-link:hover {
            color: #00cc77;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>✏️ Edit Landlord</h2>

        <?php if (!empty($error)): ?>
            <div class="error"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <form method="post">
            <label for="fullname">Full Name:</label>
            <input type="text" name="fullname" id="fullname" value="<?= htmlspecialchars($landlord['fullname']) ?>" required>

            <label for="email">Email Address:</label>
            <input type="email" name="email" id="email" value="<?= htmlspecialchars($landlord['email']) ?>" required>

            <label for="phone">Phone Number:</label>
            <input type="text" name="phone" id="phone" value="<?= htmlspecialchars($landlord['phone']) ?>" required>

            <button type="submit">💾 Save Changes</button>
        </form>

        <a class="back-link" href="manage-landlords.php">⬅ Back to Manage Landlords</a>
    </div>
</body>
</html>
