<?php

session_start();

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

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

$page_title = 'View School';

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

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

/*
|--------------------------------------------------------------------------
| VALIDATE SCHOOL ID
|--------------------------------------------------------------------------
*/

$school_id = isset($_GET['id'])
    ? (int) $_GET['id']
    : 0;

if ($school_id <= 0) {

    die('Invalid school ID.');
}

/*
|--------------------------------------------------------------------------
| FETCH SCHOOL
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT *
    FROM schools
    WHERE id = ?
    LIMIT 1
");

$stmt->execute([$school_id]);

$school = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$school) {

    die('School not found.');
}

/*
|--------------------------------------------------------------------------
| FETCH SCHOOL SETTINGS
|--------------------------------------------------------------------------
*/

$settingsStmt = $pdo->prepare("
    SELECT *
    FROM school_settings
    WHERE school_id = ?
    LIMIT 1
");

$settingsStmt->execute([$school_id]);

$settings = $settingsStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH ADMIN USER
|--------------------------------------------------------------------------
*/

$userStmt = $pdo->prepare("
    SELECT
        id,
        username,
        name,
        role,
        status,
        created_at
    FROM users
    WHERE school_id = ?
    AND role = 'admin'
    LIMIT 1
");

$userStmt->execute([$school_id]);

$adminUser = $userStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| TOTAL USERS
|--------------------------------------------------------------------------
*/

$countStmt = $pdo->prepare("
    SELECT COUNT(*) AS total_users
    FROM users
    WHERE school_id = ?
");

$countStmt->execute([$school_id]);

$totalUsers = $countStmt->fetch(PDO::FETCH_ASSOC)['total_users'];

?>

<!-- Content Header -->

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

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

                <h1>

                    View School

                </h1>

            </div>

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

                <a
                    href="index.php"
                    class="btn btn-secondary"
                >

                    <i class="fas fa-arrow-left"></i>

                    Back

                </a>

            </div>

        </div>

    </div>

</section>

<!-- Main Content -->

<section class="content">

    <div class="container-fluid">

        <div class="row">

            <!-- School Info -->

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

                <div class="card card-primary">

                    <div class="card-header">

                        <h3 class="card-title">

                            School Information

                        </h3>

                    </div>

                    <div class="card-body">

                        <table class="table table-bordered">

                            <tr>

                                <th width="40%">

                                    School Name

                                </th>

                                <td>

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

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    Phone

                                </th>

                                <td>

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

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    Email

                                </th>

                                <td>

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

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    Status

                                </th>

                                <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>

                            </tr>

                            <tr>

                                <th>

                                    Subscription

                                </th>

                                <td>

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

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    Created At

                                </th>

                                <td>

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

                                </td>

                            </tr>

                        </table>

                    </div>

                </div>

            </div>

            <!-- School Settings -->

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

                <div class="card card-success">

                    <div class="card-header">

                        <h3 class="card-title">

                            School Settings

                        </h3>

                    </div>

                    <div class="card-body">

                        <table class="table table-bordered">

                            <tr>

                                <th width="40%">

                                    Address

                                </th>

                                <td>

                                    <?= htmlspecialchars($settings['school_address'] ?? '-'); ?>

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    Website

                                </th>

                                <td>

                                    <?= htmlspecialchars($settings['school_website'] ?? '-'); ?>

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    DISE Code

                                </th>

                                <td>

                                    <?= htmlspecialchars($settings['dise_code'] ?? '-'); ?>

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    Session

                                </th>

                                <td>

                                    <?= htmlspecialchars($settings['current_session'] ?? '-'); ?>

                                </td>

                            </tr>

                            <tr>

                                <th>

                                    Result Pattern

                                </th>

                                <td>

                                    <?= htmlspecialchars($settings['result_pattern'] ?? '-'); ?>

                                </td>

                            </tr>

                        </table>

                    </div>

                </div>

            </div>

        </div>

        <!-- Admin User -->

        <div class="card">

            <div class="card-header">

                <h3 class="card-title">

                    School Admin

                </h3>

            </div>

            <div class="card-body">

                <?php if ($adminUser): ?>

                    <table class="table table-bordered">

                        <tr>

                            <th width="20%">

                                Name

                            </th>

                            <td>

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

                            </td>

                        </tr>

                        <tr>

                            <th>

                                Username

                            </th>

                            <td>

                                <?= htmlspecialchars($adminUser['username']); ?>

                            </td>

                        </tr>

                        <tr>

                            <th>

                                Role

                            </th>

                            <td>

                                <?= htmlspecialchars($adminUser['role']); ?>

                            </td>

                        </tr>

                        <tr>

                            <th>

                                Status

                            </th>

                            <td>

                                <?= htmlspecialchars($adminUser['status']); ?>

                            </td>

                        </tr>

                        <tr>

                            <th>

                                Total Users

                            </th>

                            <td>

                                <?= (int) $totalUsers; ?>

                            </td>

                        </tr>

                    </table>

                <?php else: ?>

                    <div class="alert alert-danger">

                        Admin user not found.

                    </div>

                <?php endif; ?>

            </div>

        </div>

    </div>

</section>

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