<?php

include "../../config/master.php";

/* GET CATEGORY ID (IF PROVIDED) */

$category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;

/* FETCH WORK DETAILS */

$work = null;

if($category_id > 0)
{
$work = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT * FROM donation_categories
WHERE id='$category_id'
"));
}

/* FETCH DONORS */

if($category_id > 0)
{
$donors = mysqli_query($conn,"
SELECT * FROM donors
WHERE category_id='$category_id'
ORDER BY id DESC
");
}
else
{
$donors = mysqli_query($conn,"
SELECT * FROM donors
ORDER BY id DESC
");
}

include "../layout/header.php";
include "../layout/sidebar.php";

?>

<div class="main">

<h2>Donors List</h2>

<p><b>Work:</b> <?php echo $work ? htmlspecialchars($work['work_name']) : "All Donation Works"; ?></p>

<a href="add_donor.php" class="btn-save">Add New Donor</a>

<br><br>

<table border="1" width="100%" cellpadding="8" cellspacing="0">

<tr style="background:#eee">

<th>ID</th>
<th>Name</th>
<th>Father Name</th>
<th>Assigned Amount</th>
<th>Received</th>
<th>Balance</th>
<th>Next Date</th>
<th>Action</th>

</tr>

<?php

while($row=mysqli_fetch_assoc($donors))
{

$balance = $row['assigned_amount'] - $row['received_amount'];

?>

<tr>

<td><?php echo $row['id']; ?></td>

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

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

<td>₹<?php echo number_format($row['assigned_amount']); ?></td>

<td>₹<?php echo number_format($row['received_amount']); ?></td>

<td>

<?php

if($balance > 0)
{
echo "<span style='color:red;'>₹".number_format($balance)."</span>";
}
else
{
echo "<span style='color:green;'>Paid</span>";
}

?>

</td>

<td><?php echo $row['next_date']; ?></td>

<td>

<?php if($balance > 0){ ?>

<a href="receive_payment.php?donor_id=<?php echo $row['id']; ?>&category_id=<?php echo $row['category_id']; ?>">
Receive Balance
</a>

<?php } ?>

</td>

</tr>

<?php } ?>

</table>

</div>

<?php include "../layout/footer.php"; ?>