<?php



session_start();

if (!isset($_SESSION['user_id'])) {

    echo '<p class="text-danger">Unauthorized</p>';

    exit;

}



require_once '../config.php';

$conn = getDBConnection();



// Calculate current week

$today = new DateTime();

$week_start = clone $today;

$week_start->modify('monday this week');

$week_end = clone $week_start;

$week_end->modify('friday this week');



$week_start_str = $week_start->format('Y-m-d');

$week_end_str = $week_end->format('Y-m-d');

$week_display = $week_start->format('d M') . ' - ' . $week_end->format('d M Y');



// Get summary for current week

$query = "SELECT 

    s.id,

    s.student_id,

    s.first_name,

    s.last_name,

    s.class,

    s.parent_name,

    s.phone,

    COALESCE(wfp.amount_paid, 0) as amount_paid,

    wfp.sms_sent,

    wfp.sms_sent_date,

    CASE WHEN wfp.id IS NOT NULL THEN 'Paid' ELSE 'Not Paid' END as payment_status

FROM students s

LEFT JOIN weekly_feeding_payments wfp ON s.id = wfp.student_id 

    AND wfp.week_start_date = ?

WHERE s.status = 'active'

ORDER BY s.class, s.first_name";



$stmt = $conn->prepare($query);

$stmt->bind_param("s", $week_start_str);

$stmt->execute();

$result = $stmt->get_result();



$total_students = 0;

$paid_count = 0;

$total_amount = 0;



?>

<div class="container-fluid">

    <h6 class="mb-3">Week: <?php echo $week_display; ?></h6>

    

    <div class="row mb-3">

        <div class="col-md-4">

            <div class="card bg-success text-white">

                <div class="card-body">

                    <h5>Total Paid</h5>

                    <h3 id="totalPaid">₵0.00</h3>

                </div>

            </div>

        </div>

        <div class="col-md-4">

            <div class="card bg-info text-white">

                <div class="card-body">

                    <h5>Students Paid</h5>

                    <h3 id="studentsPaid">0</h3>

                </div>

            </div>

        </div>

        <div class="col-md-4">

            <div class="card bg-warning text-white">

                <div class="card-body">

                    <h5>Pending</h5>

                    <h3 id="studentsPending">0</h3>

                </div>

            </div>

        </div>

    </div>

    

    <div class="table-responsive">

        <table class="table table-sm table-hover">

            <thead>

                <tr>

                    <th>Student ID</th>

                    <th>Name</th>

                    <th>Class</th>

                    <th>Parent</th>

                    <th>Amount Paid</th>

                    <th>Status</th>

                    <th>SMS Sent</th>

                </tr>

            </thead>

            <tbody>

                <?php 

                while ($row = $result->fetch_assoc()): 

                    $total_students++;

                    if ($row['amount_paid'] > 0) {

                        $paid_count++;

                        $total_amount += $row['amount_paid'];

                    }

                ?>

                <tr>

                    <td><?php echo htmlspecialchars($row['student_id']); ?></td>

                    <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?></td>

                    <td><?php echo htmlspecialchars($row['class']); ?></td>

                    <td><?php echo htmlspecialchars($row['parent_name'] ?: '-'); ?></td>

                    <td><?php echo formatCurrency($row['amount_paid']); ?></td>

                    <td>

                        <span class="badge bg-<?php echo $row['amount_paid'] > 0 ? 'success' : 'warning'; ?>">

                            <?php echo $row['amount_paid'] > 0 ? 'Paid' : 'Pending'; ?>

                        </span>

                    </td>

                    <td>

                        <?php if ($row['sms_sent']): ?>

                            <span class="badge bg-success" title="<?php echo $row['sms_sent_date']; ?>">

                                Sent

                            </span>

                        <?php else: ?>

                            <span class="badge bg-secondary">Not Sent</span>

                        <?php endif; ?>

                    </td>

                </tr>

                <?php endwhile; ?>

            </tbody>

        </table>

    </div>

</div>



<script>

    document.getElementById('totalPaid').textContent = '<?php echo formatCurrency($total_amount); ?>';

    document.getElementById('studentsPaid').textContent = '<?php echo $paid_count; ?>';

    document.getElementById('studentsPending').textContent = '<?php echo $total_students - $paid_count; ?>';

</script>



<?php

$stmt->close();

$conn->close();

?>