<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="wrapper">
        <div class="content"></div>
                <br>
                <main>
                    <section>
                        <form action="<?= site_url('administrator/userSearch') ?>" method="get" class="form-inline mb-4">
                            <div class="form-group">
                                <input type="text" name="query" class="form-control"
                                    value="<?= htmlspecialchars($query, ENT_QUOTES, 'UTF-8'); ?>"
                                    placeholder="Enter search term" required>
                                <button type="submit" class="btn btn-primary ms-2">Search</button>
                            </div>
                        </form>

                        <h2>Search Results for "<?= htmlspecialchars($query, ENT_QUOTES, 'UTF-8'); ?>"</h2>

                        <?php if (empty($results) || array_filter($results, fn($group) => empty($group)) === $results): ?>
                            <p>No results found.</p>
                        <?php else: ?>

                            <!-- Users Table -->
                            <?php if (!empty($results['users'])): ?>
                                <h4>Users</h4>
                                <table class="table table-bordered table-sm">
                                    <thead>
                                        <tr>
                                            <th>School ID</th>
                                            <th>First Name</th>
                                            <th>Last Name</th>
                                            <th>Email</th>
                                            <th>Phone</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($results['users'] as $user): ?>
                                            <tr>
                                                <td><?= esc($user['school_id'] ?? 'N/A') ?></td>
                                                <td><?= esc($user['firstname'] ?? 'N/A') ?></td>
                                                <td><?= esc($user['lastname'] ?? 'N/A') ?></td>
                                                <td><?= esc($user['email'] ?? 'N/A') ?></td>
                                                <td><?= esc($user['cellphone'] ?? 'N/A') ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>

                            <?php endif; ?>

                            <!-- Students Table -->
                            <?php if (!empty($results['students'])): ?>
                                <h4>Students</h4>
                                <table class="table table-bordered table-sm">
                                    <thead>
                                        <tr>
                                            <th>School ID</th>
                                            <th>First Name</th>
                                            <th>Last Name</th>
                                            <th>Date of Birth</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($results['students'] as $student): ?>
                                            <tr>
                                                <td><?= esc($student['school_id'] ?? 'N/A') ?></td>
                                                <td><?= esc($student['firstname'] ?? 'N/A') ?></td>
                                                <td><?= esc($student['lastname'] ?? 'N/A') ?></td>
                                                <td><?= esc($student['dob'] ?? 'N/A') ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>

                            <?php endif; ?>

                            <!-- Parents Table -->
                            <?php if (!empty($results['parents'])): ?>
                                <h4>Parents</h4>
                                <table class="table table-bordered table-sm">
                                    <thead>
                                        <tr>
                                            <th>First Parent ID</th>
                                            <th>Second Parent</th>
                                            <th>Email</th>
                                            <th>Phone</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($results['parents'] as $parent): ?>
                                            <tr>
                                                <td><?= esc($parent['firstparent_id'] ?? 'N/A') ?></td>
                                                <td><?= esc(trim(($parent['secondparent_firstname'] ?? '') . ' ' . ($parent['secondparent_lastname'] ?? ''))) ?></td>
                                                <td><?= esc($parent['secondparent_email'] ?? 'N/A') ?></td>
                                                <td><?= esc($parent['secondparent_phone'] ?? 'N/A') ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            <?php endif; ?>

                            <!-- Staff Table -->
                            <?php if (!empty($results['staff'])): ?>
                                <h4>Staff</h4>
                                <table class="table table-bordered table-sm">
                                    <thead>
                                        <tr>
                                            <th>Role</th>
                                            <th>Name</th>
                                            <th>Email</th>
                                            <th>Phone</th>
                                            <th>Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($results['staff'] as $staff): ?>
                                            <tr>
                                                <td><?= esc($staff['role_name'] ?? 'N/A') ?></td>
                                                <td><?= esc(trim(($staff['firstname'] ?? '') . ' ' . ($staff['lastname'] ?? ''))) ?></td>
                                                <td><?= esc($staff['email'] ?? 'N/A') ?></td>
                                                <td><?= esc($staff['phone'] ?? 'N/A') ?></td>
                                                <td><?= esc($staff['active_role'] ?? 'N/A') ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            <?php endif; ?>

                            <!-- Emergency Contacts Table -->
                            <?php if (!empty($results['emergency_contacts'])): ?>
                                <h4>Emergency Contacts</h4>
                                <table class="table table-bordered table-sm">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Name</th>
                                            <th>Relation</th>
                                            <th>Phone</th>
                                            <th>Email</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($results['emergency_contacts'] as $contact): ?>
                                            <tr>
                                                <td><?= esc($contact['id'] ?? 'N/A') ?></td>
                                                <td><?= esc($contact['emergency_contact_name'] ?? 'N/A') ?></td>
                                                <td><?= esc($contact['relation'] ?? 'N/A') ?></td>
                                                <td><?= esc($contact['cellphone'] ?? 'N/A') ?></td>
                                                <td><?= esc($contact['email'] ?? 'N/A') ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            <?php endif; ?>

                        <?php endif; ?>
                    </section>
                </main>
            </div>
        </div>
    </div>
<?= $this->endSection() ?>
