<?php
// STEP 2D-1 ROTATION DYNAMIC
 require_once __DIR__ . '/auth.php';

if (!function_exists('app_setting')) {
    function app_setting(PDO $pdo, string $key, string $default = ''): string {
        try {
            $stmt = $pdo->prepare("SELECT setting_value FROM app_settings WHERE setting_key = ? LIMIT 1");
            $stmt->execute([$key]);
            $value = $stmt->fetchColumn();
            return ($value !== false && $value !== null && $value !== '') ? (string)$value : $default;
        } catch (Throwable $e) {
            return $default;
        }
    }
}

$rotationTitle = app_setting($pdo, 'menu_rotation', 'Prayer Rotation');
 $pageTitle = $rotationTitle; require_once __DIR__ . '/includes/header.php'; $month=trim($_GET['month'] ?? current_rotation_month()); $months=['April','May','June']; if(!in_array($month,$months,true)) $month='April'; $stmt=$pdo->prepare("SELECT b.identifier,b.name,b.phone,r.assigned_identifiers FROM brothers b LEFT JOIN rotations r ON r.owner_identifier=b.identifier AND r.month_name=? ORDER BY b.sort_order ASC,b.id ASC"); $stmt->execute([$month]); $rows=$stmt->fetchAll(); ?>
<main><section class="section"><div class="container"><div class="panel"><h1><?php echo h($rotationTitle); ?></h1><form><label>Month</label><select name="month" onchange="this.form.submit()"><?php foreach($months as $m): ?><option <?php echo $m===$month?'selected':''; ?>><?php echo h($m); ?></option><?php endforeach; ?></select></form><table class="module-table"><tr><th>Brother</th><th>Assigned Prayer</th></tr><?php foreach($rows as $r): ?><tr><td><strong><?php echo h($r['identifier'].' — '.$r['name']); ?></strong></td><td><?php foreach(resolve_assignment_names($pdo,$r['assigned_identifiers']??'') as $p): ?><div class="notice"><strong><?php echo h($p['identifier'].' — '.$p['name']); ?></strong><?php if($p['phone']): ?><br><a class="call-link" href="tel:<?php echo h(phone_digits($p['phone'])); ?>"><?php echo h($p['phone']); ?></a><?php endif; ?></div><?php endforeach; ?></td></tr><?php endforeach; ?></table></div></div></section></main><?php require_once __DIR__ . '/includes/footer.php'; ?>
