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

// FETCH PARENTS
$parent_query = "
    SELECT 
        parents.parent_id,
        parents.name,
        parents.phone,
        parents.occupation,
        users.email,
        users.username
    FROM parents
    INNER JOIN users ON parents.user_id = users.user_id
    WHERE users.role = 'parent'
    ORDER BY parents.parent_id DESC
";

$parent_result = $conn->query($parent_query);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Parents Management | School ERP</title>

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

        /* HEADER */
        .header {
            background: #1e293b;
            color: white;
            padding: 15px 20px;
        }

        .header h2 {
            margin: 0;
        }

        .nav {
            margin-top: 8px;
        }

        .nav a {
            color: #fff;
            text-decoration: none;
            margin-right: 15px;
            font-size: 14px;
            opacity: 0.9;
        }

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

        /* CONTAINER */
        .container {
            width: 92%;
            margin: 20px auto;
        }

        /* CARD */
        .card {
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
        }

        h3 {
            margin-top: 0;
        }

        /* TABLE */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }

        th {
            background: #1e293b;
            color: white;
            padding: 12px;
            text-align: left;
        }

        td {
            padding: 12px;
            border-bottom: 1px solid #ddd;
        }

        tr:nth-child(even) {
            background: #f9fafb;
        }

        /* BUTTONS */
        .btn {
            padding: 6px 10px;
            border-radius: 5px;
            text-decoration: none;
            font-size: 13px;
            margin-right: 5px;
            display: inline-block;
        }

        .edit {
            background: #16a34a;
            color: white;
        }

        .delete {
            background: #dc2626;
            color: white;
        }

        .edit:hover { background: #15803d; }
        .delete:hover { background: #b91c1c; }

        /* EMPTY STATE */
        .empty {
            text-align: center;
            padding: 20px;
            color: #777;
        }
    </style>
</head>

<body>

<!-- HEADER -->
<div class="header">
    <h2>AGID EDUCATIONAL INSTITUTE</h2>
    <div class="nav">
        <a href="admin_dashboard.php">Dashboard</a>
        <a href="manage_teachers.php">Teachers</a>
        <a href="manage_students.php">Students</a>
        <a href="view_parents.php">Parents</a>
        <a href="manage_attendance.php">Attendance</a>
    </div>
</div>

<div class="container">

<div class="card">

    <h3>Parents List</h3>

    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Username</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Occupation</th>
            <th>Actions</th>
        </tr>

        <?php if ($parent_result && $parent_result->num_rows > 0): ?>
            <?php while ($row = $parent_result->fetch_assoc()): ?>
                <tr>
                    <td><?= $row['parent_id'] ?></td>
                    <td><?= htmlspecialchars($row['name']) ?></td>
                    <td><?= htmlspecialchars($row['username']) ?></td>
                    <td><?= htmlspecialchars($row['email']) ?></td>
                    <td><?= htmlspecialchars($row['phone']) ?></td>
                    <td><?= htmlspecialchars($row['occupation']) ?></td>
                    <td>
                        <a class="btn edit" href="edit_parent.php?id=<?= $row['parent_id'] ?>">Edit</a>
                        <a class="btn delete"
                           href="delete_parent.php?id=<?= $row['parent_id'] ?>"
                           onclick="return confirm('Are you sure you want to delete this parent?')">
                           Delete
                        </a>
                    </td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr>
                <td colspan="7" class="empty">No parents found</td>
            </tr>
        <?php endif; ?>

    </table>

</div>

</div>

</body>
</html>