<?php
// Include session check for admin protection
include('session_check.php');

// Include database configuration
include('../../config/database.php');

// Initialize variables
$title = $name = $area = $contact = $username = '';
$errors = [];

// Get assessor details for editing
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['id'])) {
    $id = intval($_GET['id']);
    $sql = "SELECT * FROM assessors WHERE id = ?";
    if ($stmt = $conn->prepare($sql)) {
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            $title = $row['title'];
            $name = $row['name'];
            $area = $row['area'];
            $contact = $row['contact'];
            $username = $row['username'];
        } else {
            $errors[] = "Assessor not found.";
        }
        $stmt->close();
    } else {
        $errors[] = "Database error: Unable to prepare statement.";
    }
} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['id'])) {
    $id = intval($_POST['id']);
    $title = $_POST['title'];
    $name = $_POST['name'];
    $area = $_POST['area'];
    $contact = $_POST['contact'];
    $username = $_POST['username'];

    // Validate input
    if (empty($title)) $errors[] = "Title is required";
    if (empty($name)) $errors[] = "Name is required";
    if (empty($area)) $errors[] = "Area is required";
    if (empty($contact)) $errors[] = "Contact is required";
    if (empty($username)) $errors[] = "Username is required";

    // Check for errors
    if (count($errors) == 0) {
        // Update assessor in the database
        $sql = "UPDATE assessors SET title = ?, name = ?, area = ?, contact = ?, username = ? WHERE id = ?";
        if ($stmt = $conn->prepare($sql)) {
            $stmt->bind_param("sssssi", $title, $name, $area, $contact, $username, $id);
            if ($stmt->execute()) {
                $success = "Assessor updated successfully!";
                header("Location: add_assessor.php?success=" . urlencode($success));
                exit();
            } else {
                $errors[] = "Error updating assessor: " . $stmt->error;
            }
            $stmt->close();
        } else {
            $errors[] = "Database error: Unable to prepare statement.";
        }
    }
} else {
    $errors[] = "Invalid request.";
}

$conn->close();
?>

<?php include('../../includes/header.php'); ?>
<div class="container mt-5 d-flex justify-content-center">
    <div class="card" style="width: 50%;">
        <div class="card-body">
            <h1 class="mb-4 text-center">Edit Assessor</h1>

            <?php if (!empty($errors)) : ?>
                <div class="alert alert-danger">
                    <?php foreach ($errors as $error) : ?>
                        <p><?php echo $error; ?></p>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <?php if (!empty($success)) : ?>
                <div class="alert alert-success">
                    <p><?php echo $success; ?></p>
                </div>
            <?php endif; ?>

            <form method="POST" action="edit_assessor.php?id=<?php echo htmlspecialchars($id); ?>">
                <input type="hidden" name="id" value="<?php echo htmlspecialchars($id); ?>">
                <div class="mb-3">
                    <label for="title" class="form-label">Title</label>
                    <select class="form-control" id="title" name="title">
                        <option value="Prof." <?php if($title == 'Prof.') echo 'selected'; ?>>Prof.</option>
                        <option value="Dr." <?php if($title == 'Dr.') echo 'selected'; ?>>Dr.</option>
                        <option value="Mr." <?php if($title == 'Mr.') echo 'selected'; ?>>Mr.</option>
                        <option value="Mrs." <?php if($title == 'Mrs.') echo 'selected'; ?>>Mrs.</option>
                        <option value="Ms." <?php if($title == 'Ms.') echo 'selected'; ?>>Ms.</option>
                        <option value="Miss." <?php if($title == 'Miss.') echo 'selected'; ?>>Miss.</option>
                        <option value="Rev." <?php if($title == 'Rev.') echo 'selected'; ?>>Rev.</option>
                        <option value="Esq." <?php if($title == 'Esq.') echo 'selected'; ?>>Esq.</option>
                    </select>
                </div>
                <div class="mb-3">
                    <label for="name" class="form-label">Name</label>
                    <input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($name); ?>">
                </div>
                <div class="mb-3">
                    <label for="area" class="form-label">Area</label>
                    <input type="text" class="form-control" id="area" name="area" value="<?php echo htmlspecialchars($area); ?>">
                </div>
                <div class="mb-3">
                    <label for="contact" class="form-label">Contact</label>
                    <input type="text" class="form-control" id="contact" name="contact" value="<?php echo htmlspecialchars($contact); ?>">
                </div>
                <div class="mb-3">
                    <label for="username" class="form-label">Username</label>
                    <input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($username); ?>">
                </div>
                <button type="submit" class="btn btn-black" style="background-color: #2c2b7c; color: #fff; border-radius: 0;">Update Assessor</button>
            </form>
        </div>
    </div>
</div>
<?php include('../../includes/footer.php'); ?>
