<?php
include('db_connection.php');

$parent_id = $_GET['id'] ?? null;

if (!$parent_id) {
    die("Invalid Parent ID");
}

/* =========================
   FETCH PARENT DETAILS
========================= */
$query = "
SELECT parents.*, users.email, users.username 
FROM parents 
JOIN users ON parents.user_id = users.user_id 
WHERE parents.parent_id = ?
";

$stmt = $conn->prepare($query);
$stmt->bind_param("i", $parent_id);
$stmt->execute();
$result = $stmt->get_result();
$parent = $result->fetch_assoc();
$stmt->close();

if (!$parent) {
    die("Parent not found");
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Manage Parent | School ERP</title>

<style>
body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #eef2f7;
}

/* ===== HEADER ===== */
.header {
    background: #1e293b;
    color: white;
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header h2 {
    margin: 0;
    font-size: 18px;
}

.nav a {
    color: white;
    text-decoration: none;
    margin-left: 15px;
    font-size: 14px;
}

.nav a:hover {
    text-decoration: underline;
}

/* ===== CONTAINER ===== */
.container {
    width: 45%;
    margin: 30px auto;
}

/* ===== CARD ===== */
.card {
    background: white;
    padding: 25px;
    border-radius: 10px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.1);
}

.card h3 {
    margin-top: 0;
    text-align: center;
}

/* ===== FORM ===== */
label {
    font-weight: bold;
    display: block;
    margin-top: 10px;
    color: #333;
}

input {
    width: 100%;
    padding: 10px;
    margin-top: 5px;
    border: 1px solid #ccc;
    border-radius: 5px;
}

input:focus {
    border-color: #2563eb;
    outline: none;
}

/* ===== BUTTON ===== */
button {
    margin-top: 20px;
    width: 100%;
    padding: 12px;
    border: none;
    background: #16a34a;
    color: white;
    font-size: 15px;
    border-radius: 5px;
    cursor: pointer;
}

button:hover {
    background: #15803d;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
    .container {
        width: 90%;
    }

    .header {
        flex-direction: column;
        text-align: center;
    }

    .nav {
        margin-top: 10px;
    }
}
</style>
</head>

<body>

<!-- ===== HEADER ===== -->
<div class="header">
    <h2>AGID EDUCATIONAL INSTITUTE | ERP</h2>

    <div class="nav">
        <a href="admin_dashboard.php">Dashboard</a>
        <a href="manage_parents.php">Parents</a>
        <a href="manage_students.php">Students</a>
        <a href="manage_teachers.php">Teachers</a>
    </div>
</div>

<div class="container">
<div class="card">

<h3>Edit Parent Profile</h3>

<form action="process_edit_parent.php" method="post">

<input type="hidden" name="parent_id" value="<?= $parent['parent_id'] ?>">
<input type="hidden" name="user_id" value="<?= $parent['user_id'] ?>">

<label>Name</label>
<input type="text" name="name" value="<?= htmlspecialchars($parent['name']) ?>" required>

<label>Username</label>
<input type="text" name="username" value="<?= htmlspecialchars($parent['username']) ?>" required>

<label>Password (leave blank if not changing)</label>
<input type="password" name="password">

<label>Email</label>
<input type="email" name="email" value="<?= htmlspecialchars($parent['email']) ?>" required>

<label>Phone</label>
<input type="text" name="phone" value="<?= htmlspecialchars($parent['phone']) ?>" required>

<label>Occupation</label>
<input type="text" name="occupation" value="<?= htmlspecialchars($parent['occupation']) ?>" required>

<button type="submit">Update Parent</button>

</form>

</div>
</div>

</body>
</html>