<?php

session_start();

require_once __DIR__ . '/../../library/config.php';

require_once __DIR__ . '/../../library/super_admin_auth.php';

$page_title = 'Manage Schools';

require_once __DIR__ . '/../includes/header.php';

require_once __DIR__ . '/../includes/sidebar.php';

/*
|--------------------------------------------------------------------------
| FETCH SCHOOLS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        s.*,

        (
            SELECT COUNT(*)
            FROM users u
            WHERE u.school_id = s.id
        ) AS total_users

    FROM schools s

    ORDER BY s.id DESC
");

$stmt->execute();

$schools = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!-- Content Header -->

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

            <div class="col-sm-6">

                <h1>

                    Manage Schools

                </h1>

            </div>

            <div class="col-sm-6 text-right">

                <a
                    href="create.php"
                    class="btn btn-primary"
                >

                    <i class="fas fa-plus"></i>

                    Create School

                </a>

            </div>

        </div>

    </div>

</section>

<!-- Main Content -->

<section class="content">

    <div class="container-fluid">

        <div class="card">

            <div class="card-header">

                <h3 class="card-title">

                    School List

                </h3>

            </div>

            <div class="card-body table-responsive">

                <table class="table table-bordered table-hover">

                    <thead class="thead-dark">

                        <tr>

                            <th width="80">

                                ID

                            </th>

                            <th>

                                School Name

                            </th>

                            <th>

                                Phone

                            </th>

                            <th>

                                Email

                            </th>

                            <th width="120">

                                Users

                            </th>

                            <th width="140">

                                Status

                            </th>

                            <th width="240">

                                Actions

                            </th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php if (!empty($schools)): ?>

                            <?php foreach ($schools as $school): ?>

                                <tr>

                                    <td>

                                        <?= (int) $school['id']; ?>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($school['name']); ?>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($school['phone'] ?? '-'); ?>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($school['email'] ?? '-'); ?>

                                    </td>

                                    <td>

                                        <?= (int) $school['total_users']; ?>

                                    </td>

                                    <td>

                                        <?php if ($school['status'] === 'active'): ?>

                                            <span class="badge badge-success">

                                                Active

                                            </span>

                                        <?php else: ?>

                                            <span class="badge badge-danger">

                                                Inactive

                                            </span>

                                        <?php endif; ?>

                                    </td>

                                    <td>

                                        <a
													href="view.php?id=<?= (int)$school['id']; ?>"
													class="btn btn-sm btn-info">

													<i class="fas fa-eye"></i>

													View

										</a>

                                        <a
												href="edit.php?id=<?= (int)$school['id']; ?>"
												class="btn btn-sm btn-warning">

												<i class="fas fa-edit"></i>

												Edit

											</a>

										<a
												href="status.php?id=<?= (int)$school['id']; ?>"
												class="btn btn-sm btn-secondary"
												onclick="return confirm('Change school status?')">

												<i class="fas fa-toggle-on"></i>

												Status

											</a>
											<!-- school login from super admin -->
											<a href="../schools/login_as_school.php?id=<?= $school['id'] ?>"
												class="btn btn-sm btn-warning"
												target="_blank"
												onclick="return confirm('Open school dashboard in new tab?')"
											>

												<i class="fas fa-sign-in-alt"></i>

											</a>

                                    </td>
									

                                </tr>

                            <?php endforeach; ?>

                        <?php else: ?>

                            <tr>

                                <td
                                    colspan="7"
                                    class="text-center"
                                >

                                    No schools found.

                                </td>

                            </tr>

                        <?php endif; ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</section>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>