<?php
// STEP 2D-3B ASSIGNMENTS TITLE FIX
 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;
        }
    }
}

$assignmentsTitle = app_setting($pdo, 'menu_assignments', 'Assignments');
 $pageTitle = $assignmentsTitle; require_once __DIR__ . '/includes/header.php'; $items=$pdo->query("SELECT * FROM assignments WHERE COALESCE(active,1)=1 ORDER BY sort_order ASC,id DESC")->fetchAll(); ?>
<main><section class="section"><div class="container"><div class="panel"><h1><?php echo h($assignmentsTitle); ?></h1><p class="muted"><?php echo h(app_setting($pdo,'assignments_intro','Weekly assignments and spiritual growth resources.')); ?></p><?php if(!$items): ?><div class="notice"><?php echo h(app_setting($pdo,'assignments_empty_message','No assignments are available yet.')); ?></div><?php endif; ?><div class="grid-2"><?php foreach($items as $item): ?><article class="card assignment-card"><h2><?php echo h($item['title']); ?></h2><?php $description=$item['description']??($item['content']??''); $video=$item['video_url']??''; $audio=$item['audio_url']??''; $pdf=$item['pdf_url']??''; ?><?php if($description): ?><p class="muted"><?php echo nl2br(h($description)); ?></p><?php endif; ?><?php if($video): ?><div class="media-section"><h3><?php echo h(app_setting($pdo,'media_video_label','Video')); ?></h3><?php $embed=youtube_embed_url($video); if($embed): ?><div class="video-frame"><iframe src="<?php echo h($embed); ?>" allowfullscreen></iframe></div><?php elseif(is_video_file($video)): ?><video controls playsinline style="width:100%;border-radius:16px;"><source src="<?php echo h($video); ?>"></video><?php else: ?><a class="btn btn-secondary" href="<?php echo h($video); ?>" target="_blank">Open Video</a><?php endif; ?></div><?php endif; ?><?php if($audio): ?><div class="media-section"><h3><?php echo h(app_setting($pdo,'media_audio_label','Audio')); ?></h3><?php if(is_audio_file($audio)): ?><audio controls style="width:100%;"><source src="<?php echo h($audio); ?>"></audio><?php else: ?><a class="btn btn-secondary" href="<?php echo h($audio); ?>" target="_blank">Open Audio</a><?php endif; ?></div><?php endif; ?><?php if($pdf): ?><div class="media-section"><h3><?php echo h(app_setting($pdo,'media_pdf_label','PDF')); ?></h3><a class="btn btn-secondary" href="<?php echo h($pdf); ?>" target="_blank">Open PDF</a></div><?php endif; ?></article><?php endforeach; ?></div></div></div></section></main><?php require_once __DIR__ . '/includes/footer.php'; ?>
