<?php
// pages/worship_report_view.php
requireRole('any');

$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
if (!$id) {
    echo "Invalid Report ID";
    return;
}

$stmt = $pdo->prepare("SELECT * FROM worship_reports WHERE id = ?");
$stmt->execute([$id]);
$r = $stmt->fetch();

if (!$r) {
    echo "Report not found.";
    return;
}
?>

<div class="no-print" style="margin-bottom: 20px; display: flex; justify-content: space-between;">
    <a href="index.php?page=worship_reports" class="btn" style="background: #666;">&laquo; Back to List</a>
    <div style="display: flex; gap: 10px;">
        <a href="index.php?page=worship_report_form&id=<?php echo $r['id']; ?>" class="btn">Edit Report</a>
        <button onclick="window.print()" class="btn" style="background: #444;"><i class="fas fa-print"></i> Print PDF</button>
        <button onclick="exportReportJPG()" class="btn" style="background: var(--success-color); color: #000;"><i class="fas fa-image"></i> Export JPG</button>
    </div>
</div>

<div id="report-content" style="background: #fff; color: #000; padding: 40px; margin: 0 auto; max-width: 1000px; border: 1px solid #ddd; font-family: 'Times New Roman', Times, serif;">
    
    <!-- MAIN HEADER -->
    <div style="text-align: center; margin-bottom: 30px; border-bottom: 2px solid #000; padding-bottom: 10px;">
        <h1 style="margin: 0; font-size: 26px; text-transform: uppercase;">Apostolic Faith Church</h1>
        <h2 style="margin: 5px 0; font-size: 16px; text-transform: uppercase;">WECA HQTRS, ANTHONY, LAGOS</h2>
        <h3 style="margin: 15px 0 0; font-size: 22px; text-decoration: underline;">DAILY WORSHIP REPORT</h3>
        <p style="margin: 5px 0; font-size: 18px; font-weight: bold;"><?php echo date('l, F j, Y', strtotime($r['service_date'])); ?></p>
    </div>

    <!-- SECTION 1: SUNDAY SCHOOL -->
    <div class="report-section">
        <h4 class="section-banner">SECTION 1: SUNDAY SCHOOL</h4>
        <table class="data-table">
            <tr><td class="label">Congregational Songs:</td><td class="value"><?php echo nl2br(htmlspecialchars($r['ss_congregational_songs'])); ?></td></tr>
            <tr><td class="label">Opening Prayer:</td><td class="value"><?php echo htmlspecialchars($r['ss_opening_prayer']); ?></td></tr>
            <tr><td class="label">Distribution to Classes:</td><td class="value"><?php echo htmlspecialchars($r['ss_distribution_to_classes']); ?></td></tr>
            <tr><td class="label">Lesson Topic:</td><td class="value" style="font-weight: bold;"><?php echo htmlspecialchars($r['ss_lesson_topic']); ?></td></tr>
        </table>
        <table class="data-table" style="margin-top: 5px;">
            <tr>
                <td class="label" style="width: 25%;">Internet Teacher:</td><td class="value"><?php echo htmlspecialchars($r['ss_internet_teacher']); ?></td>
                <td class="label" style="width: 25%;">Yoruba Teacher:</td><td class="value"><?php echo htmlspecialchars($r['ss_yoruba_teacher']); ?></td>
            </tr>
            <tr>
                <td class="label">French Teacher:</td><td class="value"><?php echo htmlspecialchars($r['ss_french_teacher']); ?></td>
                <td class="label">Reviewer:</td><td class="value"><?php echo htmlspecialchars($r['ss_reviewer']); ?></td>
            </tr>
        </table>
        <table class="data-table" style="margin-top: 5px;">
             <tr><td class="label" style="width: 150px;">Song After Lesson:</td><td class="value"><?php echo htmlspecialchars($r['ss_song_after_lesson']); ?></td></tr>
             <tr><td class="label">Closing Song:</td><td class="value"><?php echo htmlspecialchars($r['ss_closing_song']); ?></td></tr>
             <tr><td class="label">Closing Prayer:</td><td class="value"><?php echo htmlspecialchars($r['ss_closing_prayer']); ?></td></tr>
        </table>
    </div>

    <!-- SECTION 2: DEVOTIONAL -->
    <div class="report-section" style="margin-top: 25px;">
        <h4 class="section-banner">SECTION 2: DEVOTIONAL SERVICE</h4>
        <table class="data-table">
            <tr>
                <td class="label" style="width: 15%;">Prelude:</td><td class="value"><?php echo htmlspecialchars($r['ds_prelude']); ?></td>
                <td class="label" style="width: 20%;">Welcome Address:</td><td class="value"><?php echo htmlspecialchars($r['ds_welcome_address']); ?></td>
            </tr>
            <tr><td class="label">Song:</td><td class="value"><?php echo htmlspecialchars($r['ds_congregational_song']); ?></td><td class="label">Prayer:</td><td class="value"><?php echo htmlspecialchars($r['ds_congregational_prayer']); ?></td></tr>
            <tr><td class="label">Announcements:</td><td class="value" colspan="3"><?php echo htmlspecialchars($r['ds_announcements']); ?></td></tr>
            <tr>
                <td class="label">Anthem:</td><td class="value"><?php echo htmlspecialchars($r['ds_anthem']); ?></td>
                <td class="label">Scripture:</td><td class="value"><?php echo htmlspecialchars($r['ds_scripture_reading']); ?></td>
            </tr>
            <tr><td class="label">Bible Text:</td><td class="value" colspan="3" style="font-weight: bold;"><?php echo htmlspecialchars($r['ds_bible_text']); ?></td></tr>
            <tr><td class="label">Special Song:</td><td class="value" colspan="3"><?php echo htmlspecialchars($r['ds_special_song']); ?></td></tr>
        </table>
        <div style="background: #f9f9f9; padding: 10px; border: 1px solid #ddd; margin-top: 5px;">
            <p style="margin: 0 0 5px;"><strong>Sermon:</strong> <?php echo htmlspecialchars($r['ds_sermon_title']); ?></p>
            <p style="margin: 0 0 5px;"><strong>Preacher:</strong> <?php echo htmlspecialchars($r['ds_sermon_preacher']); ?></p>
            <p style="margin: 0 0 5px;"><strong>References:</strong> <?php echo nl2br(htmlspecialchars($r['ds_bible_references'])); ?></p>
            <p style="margin: 0;"><strong>Summary:</strong> <?php echo nl2br(htmlspecialchars($r['ds_summary_prayer_points'])); ?></p>
        </div>
        <table class="data-table" style="margin-top: 5px;">
            <tr>
                <td class="label">Invitation:</td><td class="value"><?php echo htmlspecialchars($r['ds_invitation_song']); ?></td>
                <td class="label">Benediction:</td><td class="value"><?php echo htmlspecialchars($r['ds_benediction']); ?></td>
                <td class="label">Altar:</td><td class="value"><?php echo htmlspecialchars($r['ds_altar_prayer']); ?></td>
            </tr>
        </table>
    </div>

    <!-- DOUBLE COLUMN FOR YOUTH/YPS -->
    <div style="display: flex; gap: 20px; margin-top: 25px;">
        <!-- Youth Service -->
        <div style="flex: 1;">
            <h4 class="section-banner">SECTION 3: YOUTH SERVICE</h4>
            <div class="note-box">
                <p><strong>Songs:</strong> <?php echo htmlspecialchars($r['ys_congregational_songs']); ?></p>
                <p><strong>Preacher:</strong> <?php echo htmlspecialchars($r['ys_sermon_preacher']); ?></p>
                <p><strong>Topic:</strong> <?php echo htmlspecialchars($r['ys_sermon_title']); ?></p>
                <p><strong>Testimonies:</strong> <?php echo htmlspecialchars($r['ys_testimonies_choruses']); ?></p>
                <p><strong>Summary:</strong> <?php echo htmlspecialchars($r['ys_sermon_summary']); ?></p>
            </div>
        </div>
        <!-- YPS -->
        <div style="flex: 1;">
            <h4 class="section-banner">SECTION 4: YOUNG PEOPLE'S</h4>
            <div class="note-box">
                <p><strong>Songs:</strong> <?php echo htmlspecialchars($r['yps_congregational_songs']); ?></p>
                <p><strong>Preacher:</strong> <?php echo htmlspecialchars($r['yps_sermon_preacher']); ?></p>
                <p><strong>Topic:</strong> <?php echo htmlspecialchars($r['yps_sermon_title']); ?></p>
                <p><strong>Testimonies:</strong> <?php echo htmlspecialchars($r['yps_testimonies_count']); ?></p>
                <p><strong>Notes:</strong> <?php echo htmlspecialchars($r['yps_notes']); ?></p>
            </div>
        </div>
    </div>

    <!-- ATTENDANCE & BLESSINGS -->
    <div style="display: flex; gap: 20px; margin-top: 25px;">
        <div style="flex: 2;">
            <h4 class="section-banner">SECTION 5: ATTENDANCE FIGURES</h4>
            <table class="att-view-table">
                <thead>
                    <tr><th>Event</th><th>Male</th><th>Female</th><th>Total</th></tr>
                </thead>
                <tbody>
                    <?php 
                    $rows = [
                        'morning_prayer' => 'Prayer Room - Morning',
                        'evening_prayer' => 'Prayer Room - Evening',
                        'sunday_school' => 'Sunday School',
                        'children_service' => 'Children Service',
                        'devotional' => 'Devotional Service',
                        'revival' => 'Evening Revival / Evangelistic'
                    ];
                    foreach($rows as $k => $l): ?>
                    <tr>
                        <td><?php echo $l; ?></td>
                        <td align="center"><?php echo $r["att_{$k}_m"]; ?></td>
                        <td align="center"><?php echo $r["att_{$k}_f"]; ?></td>
                        <td align="center" style="font-weight: bold; background: #f0f0f0;"><?php echo $r["att_{$k}_t"]; ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div style="flex: 1;">
            <h4 class="section-banner">SECTION 6: BLESSINGS</h4>
            <table class="att-view-table">
                <tr><td>Salvation</td><td align="center"><?php echo $r['bless_salvation']; ?></td></tr>
                <tr><td>Sanctification</td><td align="center"><?php echo $r['bless_sanctification']; ?></td></tr>
                <tr><td>Holy Ghost</td><td align="center"><?php echo $r['bless_holy_ghost']; ?></td></tr>
                <tr><td>Re-anointing</td><td align="center"><?php echo $r['bless_re_anointing']; ?></td></tr>
                <tr><td>Healing</td><td align="center"><?php echo $r['bless_healing']; ?></td></tr>
                <tr><td>Others</td><td align="center"><?php echo $r['bless_others']; ?></td></tr>
                <tr style="font-weight: bold; font-size: 1.1em; background: #eee;">
                    <td>TOTAL</td><td align="center"><?php echo $r['bless_total']; ?></td>
                </tr>
            </table>
        </div>
    </div>

    <!-- FOOTER -->
    <div style="margin-top: 50px; border-top: 1px solid #000; padding-top: 10px; display: flex; justify-content: space-between; font-size: 14px;">
        <div>
            <p style="margin: 0;"><strong>Printed By:</strong> WCS Administrator System</p>
        </div>
        <div>
            <p style="margin: 0;"><strong>Date:</strong> <?php echo date('d/m/Y H:i'); ?></p>
        </div>
    </div>

</div>

<style>
    /* Force White Background and Black Text on Screen (for View and JPG Export) */
    #report-content, #report-content table, #report-content tr, #report-content td, #report-content th {
        background-color: #fff !important;
        color: #000 !important;
        border-color: #ddd !important;
    }
    #report-content .section-banner {
        background: #000 !important;
        color: #fff !important;
    }
    #report-content .data-table td.label {
        background: #f4f4f4 !important;
    }
    #report-content .att-view-table th {
        background: #eee !important;
    }
    #report-content .note-box {
        background: #fff !important;
        border: 1px solid #ddd !important;
        border-top: none !important;
    }

    .section-banner { background: #000; color: #fff; padding: 5px 10px; margin: 0; font-size: 14px; font-weight: bold; }
    .data-table { width: 100%; border-collapse: collapse; }
    .data-table td { padding: 4px 8px; border: 1px solid #ddd; font-size: 14px; }
    .data-table td.label { width: 180px; background: #f4f4f4; font-weight: bold; }
    .att-view-table { width: 100%; border-collapse: collapse; }
    .att-view-table th, .att-view-table td { border: 1px solid #000; padding: 5px; font-size: 13px; }
    .att-view-table th { background: #eee; }
    .note-box { border: 1px solid #ddd; padding: 10px; font-size: 13px; line-height: 1.4; border-top: none; }
    .note-box p { margin: 0 0 8px; }

    @media print {
        body { background: #fff !important; color: #000 !important; }
        .no-print { display: none !important; }
        #report-content { border: none !important; padding: 0 !important; width: 100% !important; max-width: none !important; background: #fff !important; color: #000 !important; }
        .section-banner { -webkit-print-color-adjust: exact; print-color-adjust: exact; background: #000 !important; color: #fff !important; }
        table, tr, td, th { background: transparent !important; color: #000 !important; border-color: #000 !important; }
        .data-table td.label { background: #f4f4f4 !important; -webkit-print-color-adjust: exact; }
        .att-view-table th { background: #eee !important; -webkit-print-color-adjust: exact; }
    }
</style>

<script>
async function exportReportJPG() {
    const area = document.getElementById('report-content');
    try {
        const canvas = await html2canvas(area, {
            scale: 2,
            backgroundColor: '#ffffff',
            useCORS: true
        });
        const link = document.createElement('a');
        link.download = `Worship-Report-V2-${"<?php echo $r['service_date']; ?>"}.jpg`;
        link.href = canvas.toDataURL('image/jpeg', 0.9);
        link.click();
    } catch (err) {
        console.error(err);
        alert('Image export failed.');
    }
}
</script>
