<?php
require_once "../config/auth.php";
require_once "../config/db.php";
require_once "../config/helpers.php";

require_once "../includes/header.php";
require_once "../includes/sidebar.php";
require_once "../includes/topbar.php";
$pageTitle = "Outstanding";
$result = $conn->query("
SELECT 
customers.id,
customers.name,
customers.phone,
SUM(invoices.total) as outstanding
FROM invoices
JOIN customers ON invoices.customer_id = customers.id
WHERE invoices.payment_status='pending'
GROUP BY customers.id
ORDER BY outstanding DESC
");
?>

<div class="main-content">
<div class="col-lg-12">

<div class="topbar">
    <h5>Customer Outstanding</h5>
</div>

<div class="p-4">

<table class="table table-bordered">

<thead>
<tr>
<th>Customer</th>
<th>Phone</th>
<th>Outstanding</th>
<th>Action</th>
</tr>
</thead>

<tbody>

<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?= e($row['name']) ?></td>
<td><?= $row['phone'] ?></td>
<td><b>₹<?= $row['outstanding'] ?></b></td>

<td>
<a href="../actions/whatsapp_customer_due.php?id=<?= $row['id'] ?>" 
class="btn btn-success btn-sm" target="_blank">
Remind
</a>
</td>
</tr>
<?php } ?>

</tbody>

</table>

</div>
</div>
</div>

<?php require_once "../includes/footer.php"; ?>
