<?php
/**
 * Aged & Invalids – list members in the Aged and Invalids special groups.
 * Members are included here only if the corresponding checkbox was set on add/edit.
 */

require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/fellowships.php';
requireAuth();

$pageTitle = 'Aged & Invalids';
$currentPage = 'aged-invalids';

// Check if columns exist (in case upgrade not run)
$hasColumns = false;
try {
    $pdo->query('SELECT is_aged, is_invalid FROM members LIMIT 1');
    $hasColumns = true;
} catch (PDOException $e) {
    // Columns may not exist
}

$aged = [];
$invalids = [];
if ($hasColumns) {
    $stmt = $pdo->query("
        SELECT id, membership_number, title, first_name, middle_name, last_name, phone, email, date_of_birth, is_aged, is_invalid
        FROM members
        WHERE is_aged = 1 OR is_invalid = 1
        ORDER BY last_name, first_name
    ");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        if (!empty($row['is_aged'])) {
            $aged[] = $row;
        }
        if (!empty($row['is_invalid'])) {
            $invalids[] = $row;
        }
    }
}

function memberName($m) {
    $parts = array_filter([$m['title'] ?? '', $m['first_name'] ?? '', $m['middle_name'] ?? '', $m['last_name'] ?? '']);
    return implode(' ', $parts);
}

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

<div class="header">
    <h1>Aged & Invalids</h1>
    <div class="header-actions">
        <a href="<?= BASE_PATH ?>/members" class="link-muted">← Members</a>
        <a href="<?= BASE_PATH ?>/logout">Logout</a>
    </div>
</div>

<?php if (!$hasColumns): ?>
<div class="card" style="padding: 1.5rem;">
    <p class="text-muted">Special groups (Aged / Invalids) are not available yet. Run the database upgrade to add the required columns.</p>
</div>
<?php else: ?>

<div class="card" style="margin-bottom: 1.5rem;">
    <h2 style="margin: 0 0 1rem 0; font-size: 1.125rem;">Aged</h2>
    <?php if (empty($aged)): ?>
        <p class="text-muted">No members in the Aged group. Check &quot;Aged&quot; on the member form when adding or editing a member.</p>
    <?php else: ?>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Member no.</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($aged as $m): ?>
                    <tr>
                        <td><?= htmlspecialchars(memberName($m)) ?></td>
                        <td><?= htmlspecialchars($m['membership_number'] ?? '—') ?></td>
                        <td><?= htmlspecialchars($m['phone'] ?? '—') ?></td>
                        <td><?= htmlspecialchars($m['email'] ?? '—') ?></td>
                        <td class="actions"><a href="<?= BASE_PATH ?>/members/view/<?= (int)$m['id'] ?>">View</a></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>

<div class="card">
    <h2 style="margin: 0 0 1rem 0; font-size: 1.125rem;">Invalids</h2>
    <?php if (empty($invalids)): ?>
        <p class="text-muted">No members in the Invalids group. Check &quot;Invalid&quot; on the member form when adding or editing a member.</p>
    <?php else: ?>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Member no.</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($invalids as $m): ?>
                    <tr>
                        <td><?= htmlspecialchars(memberName($m)) ?></td>
                        <td><?= htmlspecialchars($m['membership_number'] ?? '—') ?></td>
                        <td><?= htmlspecialchars($m['phone'] ?? '—') ?></td>
                        <td><?= htmlspecialchars($m['email'] ?? '—') ?></td>
                        <td class="actions"><a href="<?= BASE_PATH ?>/members/view/<?= (int)$m['id'] ?>">View</a></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>

<?php endif; ?>
