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

// Filters
$date_from = isset($_GET['date_from']) ? $_GET['date_from'] : date('Y-m-01'); // Default to start of month
$date_to = isset($_GET['date_to']) ? $_GET['date_to'] : date('Y-m-d');

// Build SQL
$sql = "SELECT id, service_date, ds_sermon_preacher, ds_sermon_title, att_devotional_t, bless_total 
        FROM worship_reports 
        WHERE service_date BETWEEN ? AND ? 
        ORDER BY service_date ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute([$date_from, $date_to]);
$reports = $stmt->fetchAll();

// Totals
$total_attendance = 0;
$total_blessings = 0;
foreach ($reports as $r) {
    $total_attendance += $r['att_devotional_t'];
    $total_blessings += $r['bless_total'];
}
?>

<div class="no-print" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
    <div>
        <a href="index.php?page=worship_reports" class="btn" style="background: #666;">&laquo; Back to Reports</a>
        <h1 style="margin: 10px 0 0;">Worship Review Summary</h1>
    </div>
    <div style="display: flex; gap: 10px; align-items: flex-end;">
        <form method="GET" style="display: flex; gap: 10px; background: #222; padding: 10px; border-radius: 4px;">
            <input type="hidden" name="page" value="worship_review">
            <div>
                <label style="display: block; font-size: 0.7rem; color: #888;">From</label>
                <input type="date" name="date_from" value="<?php echo $date_from; ?>" class="form-control" style="padding: 4px;">
            </div>
            <div>
                <label style="display: block; font-size: 0.7rem; color: #888;">To</label>
                <input type="date" name="date_to" value="<?php echo $date_to; ?>" class="form-control" style="padding: 4px;">
            </div>
            <button type="submit" class="btn" style="padding: 5px 15px;">Filter</button>
        </form>
        <button onclick="window.print()" class="btn" style="background: #fff; color: #000;"><i class="fas fa-print"></i> Print PDF</button>
    </div>
</div>

<!-- PRINT AREA -->
<div id="worship-review-print" class="report-container" style="background: #fff; color: #000; padding: 30px; border: 1px solid #ddd; min-height: 800px;">
    <div style="text-align: center; margin-bottom: 30px; border-bottom: 2px solid #000; padding-bottom: 10px;">
        <h1 style="margin: 0; font-size: 24px; text-transform: uppercase;">Apostolic Faith Church</h1>
        <h2 style="margin: 5px 0; font-size: 14px; text-transform: uppercase;">WECA HQTRS, ANTHONY, LAGOS</h2>
        <h3 style="margin: 15px 0 0; font-size: 20px; text-decoration: underline;">WORSHIP REPORTS REVIEW SUMMARY</h3>
        <p style="margin: 5px 0; font-size: 14px;"><strong>Period:</strong> <?php echo date('M j, Y', strtotime($date_from)); ?> to <?php echo date('M j, Y', strtotime($date_to)); ?></p>
    </div>

    <table style="width: 100%; border-collapse: collapse; margin-top: 20px;">
        <thead>
            <tr style="background: #f0f0f0;">
                <th style="border: 1px solid #000; padding: 10px; text-align: left;">S/N</th>
                <th style="border: 1px solid #000; padding: 10px; text-align: left;">Date</th>
                <th style="border: 1px solid #000; padding: 10px; text-align: left;">Service Topic</th>
                <th style="border: 1px solid #000; padding: 10px; text-align: left;">Preacher</th>
                <th style="border: 1px solid #000; padding: 10px; text-align: center;">Att.</th>
                <th style="border: 1px solid #000; padding: 10px; text-align: center;">Bless.</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($reports)): ?>
                <tr><td colspan="6" style="text-align: center; padding: 30px; border: 1px solid #000;">No records found for the selected period.</td></tr>
            <?php else: ?>
                <?php $sn = 1; foreach ($reports as $r): ?>
                <tr>
                    <td style="border: 1px solid #000; padding: 8px;"><?php echo $sn++; ?></td>
                    <td style="border: 1px solid #000; padding: 8px; white-space: nowrap;">
                        <?php echo date('d/m/Y', strtotime($r['service_date'])); ?>
                    </td>
                    <td style="border: 1px solid #000; padding: 8px;"><?php echo htmlspecialchars($r['ds_sermon_title'] ?: '-'); ?></td>
                    <td style="border: 1px solid #000; padding: 8px;"><?php echo htmlspecialchars($r['ds_sermon_preacher'] ?: '-'); ?></td>
                    <td style="border: 1px solid #000; padding: 8px; text-align: center;"><?php echo $r['att_devotional_t']; ?></td>
                    <td style="border: 1px solid #000; padding: 8px; text-align: center;"><?php echo $r['bless_total']; ?></td>
                </tr>
                <?php endforeach; ?>
                <!-- TOTALS ROW -->
                <tr class="totals-row" style="background: #eee; font-weight: bold;">
                    <td colspan="4" style="border: 1px solid #000; padding: 10px; text-align: right;">TOTALS:</td>
                    <td style="border: 1px solid #000; padding: 10px; text-align: center;"><?php echo $total_attendance; ?></td>
                    <td style="border: 1px solid #000; padding: 10px; text-align: center;"><?php echo $total_blessings; ?></td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

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

<style>
    /* Force white background for the specific report area on screen */
    #worship-review-print, 
    #worship-review-print table, 
    #worship-review-print tr, 
    #worship-review-print td, 
    #worship-review-print th {
        background-color: #ffffff !important;
        color: #000000 !important;
    }
    
    #worship-review-print thead tr th {
        background-color: #f0f0f0 !important;
    }
    
    #worship-review-print .totals-row td {
        background-color: #eeeeee !important;
    }

    @media print {
        header, nav, .no-print { display: none !important; }
        .container { width: 100% !important; max-width: none !important; margin: 0 !important; padding: 0 !important; }
        body { background: #fff !important; color: #000 !important; }
        #worship-review-print { border: none !important; padding: 0 !important; }
        table { font-size: 12px; }
        th { background: #f0f0f0 !important; -webkit-print-color-adjust: exact; }
        tr { page-break-inside: avoid; }
    }
</style>
