<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$date = $_GET['date'] ?? date('Y-m-d');

// Fetch daily summary
$sql = "SELECT payment_method, SUM(amount_paid) as total 
        FROM payments 
        WHERE DATE(payment_date) = ? 
        GROUP BY payment_method";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $date);
$stmt->execute();
$res = $stmt->get_result();

$summary = [];
$total_day = 0;
while ($row = $res->fetch_assoc()) {
    $summary[] = $row;
    $total_day += $row['total'];
}

// Fetch detailed list
$detail_sql = "SELECT pay.*, u.full_name as patient_name 
               FROM payments pay
               LEFT JOIN patients p ON pay.patient_id = p.patient_id
               LEFT JOIN users u ON p.user_id = u.user_id
               WHERE DATE(pay.payment_date) = ?
               ORDER BY pay.payment_id DESC";
$stmt_d = $conn->prepare($detail_sql);
$stmt_d->bind_param("s", $date);
$stmt_d->execute();
$detail_res = $stmt_d->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Daily Collections')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Daily Collections Report</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card">
                                <div class="card-body">
                                    <form method="GET" class="do-flex align-items-center gap-2 mb-4">
                                        <div class="input-group" style="max-width: 300px;">
                                            <span class="input-group-text">Date</span>
                                            <input type="date" name="date" class="form-control"
                                                value="<?php echo $date; ?>" onchange="this.form.submit()">
                                        </div>
                                    </form>

                                    <div class="row mb-4">
                                        <?php foreach ($summary as $s): ?>
                                            <div class="col-md-3">
                                                <div class="card border border-primary">
                                                    <div class="card-body text-center">
                                                        <h6 class="text-muted text-uppercase mb-2">
                                                            <?php echo $s['payment_method']; ?></h6>
                                                        <h3 class="mb-0 text-primary">KES
                                                            <?php echo number_format($s['total'], 2); ?></h3>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                        <div class="col-md-3">
                                            <div class="card bg-primary text-white">
                                                <div class="card-body text-center">
                                                    <h6 class="text-white-50 text-uppercase mb-2">Total Collected</h6>
                                                    <h3 class="mb-0 text-white">KES
                                                        <?php echo number_format($total_day, 2); ?></h3>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <h5 class="mb-3">Detailed Transactions</h5>
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-striped">
                                            <thead>
                                                <tr>
                                                    <th>Time</th>
                                                    <th>Patient</th>
                                                    <th>Method</th>
                                                    <th>Reference</th>
                                                    <th>Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php while ($row = $detail_res->fetch_assoc()): ?>
                                                    <tr>
                                                        <td><?php echo date('H:i', strtotime($row['created_at'])); // fallback if no time in payment_date ?>
                                                        </td>
                                                        <td><?php echo htmlspecialchars($row['patient_name']); ?></td>
                                                        <td><?php echo $row['payment_method']; ?></td>
                                                        <td><?php echo $row['transaction_ref']; ?></td>
                                                        <td class="text-end fw-bold">
                                                            <?php echo number_format($row['amount_paid'], 2); ?></td>
                                                    </tr>
                                                <?php endwhile; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>