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

$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
$report = null;
$error = null;
$success = null;

// Fetch existing if ID provided
if ($id) {
    $stmt = $pdo->prepare("SELECT * FROM worship_reports WHERE id = ?");
    $stmt->execute([$id]);
    $report = $stmt->fetch();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = [
        'service_date' => clean_input($_POST['service_date']),
        
        // Section 1
        'ss_congregational_songs' => clean_input($_POST['ss_congregational_songs']),
        'ss_opening_prayer' => clean_input($_POST['ss_opening_prayer']),
        'ss_distribution_to_classes' => clean_input($_POST['ss_distribution_to_classes']),
        'ss_lesson_topic' => clean_input($_POST['ss_lesson_topic']),
        'ss_internet_teacher' => clean_input($_POST['ss_internet_teacher']),
        'ss_yoruba_teacher' => clean_input($_POST['ss_yoruba_teacher']),
        'ss_french_teacher' => clean_input($_POST['ss_french_teacher']),
        'ss_song_after_lesson' => clean_input($_POST['ss_song_after_lesson']),
        'ss_reviewer' => clean_input($_POST['ss_reviewer']),
        'ss_closing_song' => clean_input($_POST['ss_closing_song']),
        'ss_closing_prayer' => clean_input($_POST['ss_closing_prayer']),

        // Section 2
        'ds_prelude' => clean_input($_POST['ds_prelude']),
        'ds_welcome_address' => clean_input($_POST['ds_welcome_address']),
        'ds_congregational_song' => clean_input($_POST['ds_congregational_song']),
        'ds_congregational_prayer' => clean_input($_POST['ds_congregational_prayer']),
        'ds_announcements' => clean_input($_POST['ds_announcements']),
        'ds_newcomers' => clean_input($_POST['ds_newcomers']),
        'ds_baby_dedication' => clean_input($_POST['ds_baby_dedication']),
        'ds_anthem' => clean_input($_POST['ds_anthem']),
        'ds_scripture_reading' => clean_input($_POST['ds_scripture_reading']),
        'ds_bible_text' => clean_input($_POST['ds_bible_text']),
        'ds_special_song' => clean_input($_POST['ds_special_song']),
        'ds_sermon_preacher' => clean_input($_POST['ds_sermon_preacher']),
        'ds_sermon_title' => clean_input($_POST['ds_sermon_title']),
        'ds_bible_references' => clean_input($_POST['ds_bible_references']),
        'ds_summary_prayer_points' => clean_input($_POST['ds_summary_prayer_points']),
        'ds_invitation_song' => clean_input($_POST['ds_invitation_song']),
        'ds_benediction' => clean_input($_POST['ds_benediction']),
        'ds_altar_prayer' => clean_input($_POST['ds_altar_prayer']),

        // Section 3
        'ys_welcome_address' => clean_input($_POST['ys_welcome_address']),
        'ys_prelude' => clean_input($_POST['ys_prelude']),
        'ys_congregational_songs' => clean_input($_POST['ys_congregational_songs']),
        'ys_opening_prayer' => clean_input($_POST['ys_opening_prayer']),
        'ys_announcement' => clean_input($_POST['ys_announcement']),
        'ys_group_testifying' => clean_input($_POST['ys_group_testifying']),
        'ys_testimonies_choruses' => clean_input($_POST['ys_testimonies_choruses']),
        'ys_special_song' => clean_input($_POST['ys_special_song']),
        'ys_sermon_preacher' => clean_input($_POST['ys_sermon_preacher']),
        'ys_sermon_title' => clean_input($_POST['ys_sermon_title']),
        'ys_sermon_summary' => clean_input($_POST['ys_sermon_summary']),
        'ys_invitation_song' => clean_input($_POST['ys_invitation_song']),
        'ys_benediction' => clean_input($_POST['ys_benediction']),
        'ys_altar_prayer' => clean_input($_POST['ys_altar_prayer']),

        // Section 4
        'yps_prelude' => clean_input($_POST['yps_prelude']),
        'yps_songs_after_prelude' => clean_input($_POST['yps_songs_after_prelude']),
        'yps_congregational_songs' => clean_input($_POST['yps_congregational_songs']),
        'yps_opening_prayer' => clean_input($_POST['yps_opening_prayer']),
        'yps_announcement' => clean_input($_POST['yps_announcement']),
        'yps_songs_before_testimony' => clean_input($_POST['yps_songs_before_testimony']),
        'yps_choruses' => clean_input($_POST['yps_choruses']),
        'yps_testimonies_count' => clean_input($_POST['yps_testimonies_count']),
        'yps_special_song' => clean_input($_POST['yps_special_song']),
        'yps_sermon_preacher' => clean_input($_POST['yps_sermon_preacher']),
        'yps_sermon_title' => clean_input($_POST['yps_sermon_title']),
        'yps_bible_references' => clean_input($_POST['yps_bible_references']),
        'yps_notes' => clean_input($_POST['yps_notes']),
        'yps_invitation_closing_song' => clean_input($_POST['yps_invitation_closing_song']),
        'yps_benediction' => clean_input($_POST['yps_benediction']),
        'yps_altar_prayer' => clean_input($_POST['yps_altar_prayer']),

        // Section 5
        'att_morning_prayer_m' => (int)$_POST['att_morning_prayer_m'],
        'att_morning_prayer_f' => (int)$_POST['att_morning_prayer_f'],
        'att_morning_prayer_t' => (int)$_POST['att_morning_prayer_t'],
        'att_evening_prayer_m' => (int)$_POST['att_evening_prayer_m'],
        'att_evening_prayer_f' => (int)$_POST['att_evening_prayer_f'],
        'att_evening_prayer_t' => (int)$_POST['att_evening_prayer_t'],
        'att_sunday_school_m' => (int)$_POST['att_sunday_school_m'],
        'att_sunday_school_f' => (int)$_POST['att_sunday_school_f'],
        'att_sunday_school_t' => (int)$_POST['att_sunday_school_t'],
        'att_children_service_m' => (int)$_POST['att_children_service_m'],
        'att_children_service_f' => (int)$_POST['att_children_service_f'],
        'att_children_service_t' => (int)$_POST['att_children_service_t'],
        'att_devotional_m' => (int)$_POST['att_devotional_m'],
        'att_devotional_f' => (int)$_POST['att_devotional_f'],
        'att_devotional_t' => (int)$_POST['att_devotional_t'],
        'att_revival_m' => (int)$_POST['att_revival_m'],
        'att_revival_f' => (int)$_POST['att_revival_f'],
        'att_revival_t' => (int)$_POST['att_revival_t'],

        // Section 6
        'bless_salvation' => (int)$_POST['bless_salvation'],
        'bless_sanctification' => (int)$_POST['bless_sanctification'],
        'bless_holy_ghost' => (int)$_POST['bless_holy_ghost'],
        'bless_re_anointing' => (int)$_POST['bless_re_anointing'],
        'bless_healing' => (int)$_POST['bless_healing'],
        'bless_others' => (int)$_POST['bless_others'],
        'bless_total' => (int)$_POST['bless_total'],
    ];

    if ($id) {
        $fields = [];
        foreach ($data as $key => $val) { $fields[] = "$key = ?"; }
        $sql = "UPDATE worship_reports SET " . implode(", ", $fields) . " WHERE id = ?";
        $stmt = $pdo->prepare($sql);
        $execParams = array_values($data);
        $execParams[] = $id;
        if ($stmt->execute($execParams)) {
            $success = "Report updated successfully.";
        } else {
            $error = "Failed to update report.";
        }
    } else {
        $keys = array_keys($data);
        $keys[] = 'created_by';
        $placeholders = array_fill(0, count($keys), "?");
        $sql = "INSERT INTO worship_reports (" . implode(", ", $keys) . ") VALUES (" . implode(", ", $placeholders) . ")";
        $stmt = $pdo->prepare($sql);
        $execParams = array_values($data);
        $execParams[] = $_SESSION['user_id'] ?? null;
        if ($stmt->execute($execParams)) {
            $id = $pdo->lastInsertId();
            $success = "Report created successfully.";
        } else {
            $error = "Failed to create report.";
        }
    }
    
    if($id){
        $stmt = $pdo->prepare("SELECT * FROM worship_reports WHERE id = ?");
        $stmt->execute([$id]);
        $report = $stmt->fetch();
    }
}
?>

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
    <h1><?php echo $id ? 'Edit Worship Report' : 'New Worship Report'; ?></h1>
    <a href="index.php?page=worship_reports" class="btn" style="background: #666;">&laquo; Back to List</a>
</div>

<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>

<style>
    .form-tabs { display: flex; gap: 5px; margin-bottom: 20px; border-bottom: 1px solid #444; padding-bottom: 10px; overflow-x: auto; }
    .tab-btn { padding: 10px 20px; background: #333; border: none; color: #888; cursor: pointer; border-radius: 4px 4px 0 0; white-space: nowrap; transition: 0.3s; }
    .tab-btn.active { background: var(--primary-color); color: #fff; }
    .tab-content { display: none; }
    .tab-content.active { display: block; animation: fadeIn 0.3s; }
    @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
    .att-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
    .att-table th { background: #1a1a1a; padding: 10px; text-align: left; border: 1px solid #333; }
    .att-table td { padding: 5px; border: 1px solid #333; }
    .att-table input { width: 100%; background: #222; border: 1px solid #444; color: #fff; padding: 8px; border-radius: 4px; text-align: center; }
    .att-table input[readonly] { background: #111; border: none; font-weight: bold; }
    .section-title-form { font-size: 1.2rem; margin: 20px 0 10px; color: var(--primary-color); border-left: 4px solid var(--primary-color); padding-left: 10px; }
</style>

<form method="POST" class="card" id="reportForm">
    <div class="form-group" style="max-width: 300px; margin-bottom: 30px;">
        <label>Service Date *</label>
        <input type="date" name="service_date" class="form-control" required value="<?php echo $report ? $report['service_date'] : date('Y-m-d'); ?>">
    </div>

    <!-- TABS -->
    <div class="form-tabs">
        <button type="button" class="tab-btn active" onclick="showTab('section_ss')">1. Sunday School</button>
        <button type="button" class="tab-btn" onclick="showTab('section_ds')">2. Devotional</button>
        <button type="button" class="tab-btn" onclick="showTab('section_ys')">3. Youth Service</button>
        <button type="button" class="tab-btn" onclick="showTab('section_yps')">4. YPS</button>
        <button type="button" class="tab-btn" onclick="showTab('section_att')">5. Attendance</button>
        <button type="button" class="tab-btn" onclick="showTab('section_bless')">6. Blessings</button>
    </div>

    <!-- Section 1: Sunday School -->
    <div id="section_ss" class="tab-content active">
        <div class="dashboard-grid">
            <div class="form-group" style="grid-column: span 2;">
                <label>Congregational Songs</label>
                <textarea name="ss_congregational_songs" class="form-control" rows="2"><?php echo $report ? $report['ss_congregational_songs'] : ''; ?></textarea>
            </div>
            <div class="form-group">
                <label>Opening Prayer</label>
                <input type="text" name="ss_opening_prayer" class="form-control" value="<?php echo $report ? $report['ss_opening_prayer'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>Distribution to Classes</label>
                <input type="text" name="ss_distribution_to_classes" class="form-control" value="<?php echo $report ? $report['ss_distribution_to_classes'] : ''; ?>">
            </div>
            <div class="form-group" style="grid-column: span 2;">
                <label>Sunday School Lesson Topic</label>
                <input type="text" name="ss_lesson_topic" class="form-control" value="<?php echo $report ? $report['ss_lesson_topic'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>Internet Class Teacher</label>
                <input type="text" name="ss_internet_teacher" class="form-control" value="<?php echo $report ? $report['ss_internet_teacher'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>Yoruba Internet Class Teacher</label>
                <input type="text" name="ss_yoruba_teacher" class="form-control" value="<?php echo $report ? $report['ss_yoruba_teacher'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>French Internet Class Teacher</label>
                <input type="text" name="ss_french_teacher" class="form-control" value="<?php echo $report ? $report['ss_french_teacher'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>Song After Lesson</label>
                <input type="text" name="ss_song_after_lesson" class="form-control" value="<?php echo $report ? $report['ss_song_after_lesson'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>Reviewer</label>
                <input type="text" name="ss_reviewer" class="form-control" value="<?php echo $report ? $report['ss_reviewer'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>Closing Song</label>
                <input type="text" name="ss_closing_song" class="form-control" value="<?php echo $report ? $report['ss_closing_song'] : ''; ?>">
            </div>
            <div class="form-group">
                <label>Closing Prayer</label>
                <input type="text" name="ss_closing_prayer" class="form-control" value="<?php echo $report ? $report['ss_closing_prayer'] : ''; ?>">
            </div>
        </div>
    </div>

    <!-- Section 2: Devotional -->
    <div id="section_ds" class="tab-content">
        <div class="dashboard-grid">
            <div class="form-group"><label>Prelude</label><input type="text" name="ds_prelude" class="form-control" value="<?php echo $report?$report['ds_prelude']:'';?>"></div>
            <div class="form-group"><label>Welcome Address</label><input type="text" name="ds_welcome_address" class="form-control" value="<?php echo $report?$report['ds_welcome_address']:'';?>"></div>
            <div class="form-group"><label>Congregational Song</label><input type="text" name="ds_congregational_song" class="form-control" value="<?php echo $report?$report['ds_congregational_song']:'';?>"></div>
            <div class="form-group"><label>Congregational Prayer</label><input type="text" name="ds_congregational_prayer" class="form-control" value="<?php echo $report?$report['ds_congregational_prayer']:'';?>"></div>
            <div class="form-group" style="grid-column: span 2;"><label>Announcements</label><input type="text" name="ds_announcements" class="form-control" value="<?php echo $report?$report['ds_announcements']:'';?>"></div>
            <div class="form-group"><label>Newcomers</label><input type="text" name="ds_newcomers" class="form-control" value="<?php echo $report?$report['ds_newcomers']:'';?>"></div>
            <div class="form-group"><label>Baby Dedication</label><input type="text" name="ds_baby_dedication" class="form-control" value="<?php echo $report?$report['ds_baby_dedication']:'';?>"></div>
            <div class="form-group"><label>Anthem</label><input type="text" name="ds_anthem" class="form-control" value="<?php echo $report?$report['ds_anthem']:'';?>"></div>
            <div class="form-group"><label>Scripture Reading</label><input type="text" name="ds_scripture_reading" class="form-control" value="<?php echo $report?$report['ds_scripture_reading']:'';?>"></div>
            <div class="form-group"><label>Bible Text</label><input type="text" name="ds_bible_text" class="form-control" value="<?php echo $report?$report['ds_bible_text']:'';?>"></div>
            <div class="form-group"><label>Special Song</label><input type="text" name="ds_special_song" class="form-control" value="<?php echo $report?$report['ds_special_song']:'';?>"></div>
            <div class="form-group"><label>Preacher</label><input type="text" name="ds_sermon_preacher" class="form-control" value="<?php echo $report?$report['ds_sermon_preacher']:'';?>"></div>
            <div class="form-group"><label>Sermon Title</label><input type="text" name="ds_sermon_title" class="form-control" value="<?php echo $report?$report['ds_sermon_title']:'';?>"></div>
            <div class="form-group" style="grid-column: span 2;">
                <label>Bible References</label>
                <textarea name="ds_bible_references" class="form-control" rows="2"><?php echo $report ? $report['ds_bible_references'] : ''; ?></textarea>
            </div>
            <div class="form-group" style="grid-column: span 2;">
                <label>Summary / Prayer Points</label>
                <textarea name="ds_summary_prayer_points" class="form-control" rows="3"><?php echo $report ? $report['ds_summary_prayer_points'] : ''; ?></textarea>
            </div>
            <div class="form-group"><label>Invitation Song</label><input type="text" name="ds_invitation_song" class="form-control" value="<?php echo $report?$report['ds_invitation_song']:'';?>"></div>
            <div class="form-group"><label>Benediction</label><input type="text" name="ds_benediction" class="form-control" value="<?php echo $report?$report['ds_benediction']:'';?>"></div>
            <div class="form-group"><label>Altar Prayer</label><input type="text" name="ds_altar_prayer" class="form-control" value="<?php echo $report?$report['ds_altar_prayer']:'';?>"></div>
        </div>
    </div>

    <!-- Section 3: Youth Service -->
    <div id="section_ys" class="tab-content">
        <div class="dashboard-grid">
            <div class="form-group"><label>Welcome Address</label><input type="text" name="ys_welcome_address" class="form-control" value="<?php echo $report?$report['ys_welcome_address']:'';?>"></div>
            <div class="form-group"><label>Prelude</label><input type="text" name="ys_prelude" class="form-control" value="<?php echo $report?$report['ys_prelude']:'';?>"></div>
            <div class="form-group" style="grid-column: span 2;"><label>Congregational Songs</label><textarea name="ys_congregational_songs" class="form-control"><?php echo $report?$report['ys_congregational_songs']:'';?></textarea></div>
            <div class="form-group"><label>Opening Prayer</label><input type="text" name="ys_opening_prayer" class="form-control" value="<?php echo $report?$report['ys_opening_prayer']:'';?>"></div>
            <div class="form-group"><label>Announcement</label><textarea name="ys_announcement" class="form-control"><?php echo $report?$report['ys_announcement']:'';?></textarea></div>
            <div class="form-group"><label>Group Testifying</label><input type="text" name="ys_group_testifying" class="form-control" value="<?php echo $report?$report['ys_group_testifying']:'';?>"></div>
            <div class="form-group"><label>No of Testimonies & Choruses</label><input type="text" name="ys_testimonies_choruses" class="form-control" value="<?php echo $report?$report['ys_testimonies_choruses']:'';?>"></div>
            <div class="form-group"><label>Special Song</label><input type="text" name="ys_special_song" class="form-control" value="<?php echo $report?$report['ys_special_song']:'';?>"></div>
            <div class="form-group"><label>Preacher</label><input type="text" name="ys_sermon_preacher" class="form-control" value="<?php echo $report?$report['ys_sermon_preacher']:'';?>"></div>
            <div class="form-group"><label>Sermon Title</label><input type="text" name="ys_sermon_title" class="form-control" value="<?php echo $report?$report['ys_sermon_title']:'';?>"></div>
            <div class="form-group" style="grid-column: span 2;"><label>Summary</label><textarea name="ys_sermon_summary" class="form-control"><?php echo $report?$report['ys_sermon_summary']:'';?></textarea></div>
            <div class="form-group"><label>Invitation Song</label><input type="text" name="ys_invitation_song" class="form-control" value="<?php echo $report?$report['ys_invitation_song']:'';?>"></div>
            <div class="form-group"><label>Benediction</label><input type="text" name="ys_benediction" class="form-control" value="<?php echo $report?$report['ys_benediction']:'';?>"></div>
            <div class="form-group"><label>Altar Prayer</label><input type="text" name="ys_altar_prayer" class="form-control" value="<?php echo $report?$report['ys_altar_prayer']:'';?>"></div>
        </div>
    </div>

    <!-- Section 4: Young People's Service -->
    <div id="section_yps" class="tab-content">
        <div class="dashboard-grid">
            <div class="form-group"><label>Prelude</label><input type="text" name="yps_prelude" class="form-control" value="<?php echo $report?$report['yps_prelude']:'';?>"></div>
            <div class="form-group"><label>Songs After Prelude</label><textarea name="yps_songs_after_prelude" class="form-control"><?php echo $report?$report['yps_songs_after_prelude']:'';?></textarea></div>
            <div class="form-group" style="grid-column: span 2;"><label>Congregational Songs</label><textarea name="yps_congregational_songs" class="form-control"><?php echo $report?$report['yps_congregational_songs']:'';?></textarea></div>
            <div class="form-group"><label>Opening Prayer</label><input type="text" name="yps_opening_prayer" class="form-control" value="<?php echo $report?$report['yps_opening_prayer']:'';?>"></div>
            <div class="form-group"><label>Announcement</label><textarea name="yps_announcement" class="form-control"><?php echo $report?$report['yps_announcement']:'';?></textarea></div>
            <div class="form-group"><label>Songs Before Testimony</label><textarea name="yps_songs_before_testimony" class="form-control"><?php echo $report?$report['yps_songs_before_testimony']:'';?></textarea></div>
            <div class="form-group"><label>Choruses</label><input type="text" name="yps_choruses" class="form-control" value="<?php echo $report?$report['yps_choruses']:'';?>"></div>
            <div class="form-group"><label>No of Testimonies</label><input type="text" name="yps_testimonies_count" class="form-control" value="<?php echo $report?$report['yps_testimonies_count']:'';?>"></div>
            <div class="form-group"><label>Special Song</label><input type="text" name="yps_special_song" class="form-control" value="<?php echo $report?$report['yps_special_song']:'';?>"></div>
            <div class="form-group"><label>Preacher</label><input type="text" name="yps_sermon_preacher" class="form-control" value="<?php echo $report?$report['yps_sermon_preacher']:'';?>"></div>
            <div class="form-group"><label>Sermon Title</label><input type="text" name="yps_sermon_title" class="form-control" value="<?php echo $report?$report['yps_sermon_title']:'';?>"></div>
            <div class="form-group"><label>Bible References</label><textarea name="yps_bible_references" class="form-control"><?php echo $report?$report['yps_bible_references']:'';?></textarea></div>
            <div class="form-group" style="grid-column: span 2;"><label>Notes</label><textarea name="yps_notes" class="form-control"><?php echo $report?$report['yps_notes']:'';?></textarea></div>
            <div class="form-group"><label>Invitation / Closing Song</label><input type="text" name="yps_invitation_closing_song" class="form-control" value="<?php echo $report?$report['yps_invitation_closing_song']:'';?>"></div>
            <div class="form-group"><label>Benediction</label><input type="text" name="yps_benediction" class="form-control" value="<?php echo $report?$report['yps_benediction']:'';?>"></div>
            <div class="form-group"><label>Altar Prayer</label><input type="text" name="yps_altar_prayer" class="form-control" value="<?php echo $report?$report['yps_altar_prayer']:'';?>"></div>
        </div>
    </div>

    <!-- Section 5: Attendance -->
    <div id="section_att" class="tab-content">
        <h3 class="section-title-form">Attendance Figures</h3>
        <table class="att-table">
            <thead>
                <tr>
                    <th>Service / Event</th>
                    <th style="width: 100px;">Male</th>
                    <th style="width: 100px;">Female</th>
                    <th style="width: 120px;">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $att_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 ($att_rows as $key => $label):
                    $m = $report ? $report["att_{$key}_m"] : 0;
                    $f = $report ? $report["att_{$key}_f"] : 0;
                    $t = $report ? $report["att_{$key}_t"] : 0;
                ?>
                <tr>
                    <td><?php echo $label; ?></td>
                    <td><input type="number" name="att_<?php echo $key; ?>_m" id="att_<?php echo $key; ?>_m" value="<?php echo $m; ?>" oninput="calcAtt('<?php echo $key; ?>')"></td>
                    <td><input type="number" name="att_<?php echo $key; ?>_f" id="att_<?php echo $key; ?>_f" value="<?php echo $f; ?>" oninput="calcAtt('<?php echo $key; ?>')"></td>
                    <td><input type="number" name="att_<?php echo $key; ?>_t" id="att_<?php echo $key; ?>_t" value="<?php echo $t; ?>" readonly></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- Section 6: Blessings -->
    <div id="section_bless" class="tab-content">
        <h3 class="section-title-form">Blessings</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 15px;">
            <div class="form-group"><label>Salvation</label><input type="number" name="bless_salvation" class="form-control bless-input" value="<?php echo $report?$report['bless_salvation']:0;?>"></div>
            <div class="form-group"><label>Sanctification</label><input type="number" name="bless_sanctification" class="form-control bless-input" value="<?php echo $report?$report['bless_sanctification']:0;?>"></div>
            <div class="form-group"><label>Holy Ghost Baptism</label><input type="number" name="bless_holy_ghost" class="form-control bless-input" value="<?php echo $report?$report['bless_holy_ghost']:0;?>"></div>
            <div class="form-group"><label>Re-anointing</label><input type="number" name="bless_re_anointing" class="form-control bless-input" value="<?php echo $report?$report['bless_re_anointing']:0;?>"></div>
            <div class="form-group"><label>Healing</label><input type="number" name="bless_healing" class="form-control bless-input" value="<?php echo $report?$report['bless_healing']:0;?>"></div>
            <div class="form-group"><label>Others</label><input type="number" name="bless_others" class="form-control bless-input" value="<?php echo $report?$report['bless_others']:0;?>"></div>
            <div class="form-group"><label><strong>Total</strong></label><input type="number" name="bless_total" id="bless_total" class="form-control" readonly style="background: #111; font-weight: bold;" value="<?php echo $report?$report['bless_total']:0;?>"></div>
        </div>
    </div>

    <div style="margin-top: 40px; text-align: right; border-top: 1px solid #444; padding-top: 20px;">
        <button type="submit" class="btn" style="padding: 12px 40px; font-size: 1.1rem;"><?php echo $id ? 'Update Daily Report' : 'Save Daily Report'; ?></button>
    </div>
</form>

<script>
function showTab(tabId) {
    document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
    document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
    document.getElementById(tabId).classList.add('active');
    event.currentTarget.classList.add('active');
}

function calcAtt(key) {
    const m = parseInt(document.getElementById('att_' + key + '_m').value) || 0;
    const f = parseInt(document.getElementById('att_' + key + '_f').value) || 0;
    document.getElementById('att_' + key + '_t').value = m + f;
}

// Global total for blessings
document.querySelectorAll('.bless-input').forEach(input => {
    input.addEventListener('input', () => {
        let total = 0;
        document.querySelectorAll('.bless-input').forEach(i => {
            total += parseInt(i.value) || 0;
        });
        document.getElementById('bless_total').value = total;
    });
});
</script>
