<?php include 'db.php'; ?>
<?php include 'layout.php'; ?>

<body>
<div class="main-wrapper">
	<div class="page-wrapper">
		<div class="content">
			<div class="page-header">
				<div class="page-title">
					<h4>Customer Payments</h4>
					<h6>Enter payments per invoice or as total</h6>
				</div>
			</div>

			<!-- Filter/Search -->
			<form method="GET" class="row mb-3">
				<div class="col-md-6">
					<select name="customer_id" class="form-control select2" required>
						<option value="">-- Select Customer --</option>
						<?php
						$cust = $conn->query("SELECT id, name FROM customers WHERE active = 1 ORDER BY name ASC");
						while ($c = $cust->fetch_assoc()) {
							$selected = ($_GET['customer_id'] ?? '') == $c['id'] ? 'selected' : '';
							echo "<option value='{$c['id']}' $selected>{$c['name']}</option>";
						}
						?>
					</select>
				</div>
				<div class="col-md-2">
					<button type="submit" class="btn btn-primary">Search</button>
				</div>
			</form>

			<?php if (!empty($_GET['customer_id'])): ?>
			<?php
			$cid = intval($_GET['customer_id']);
			$customer = $conn->query("SELECT * FROM customers WHERE id = $cid")->fetch_assoc();
			$outstanding = $conn->query("SELECT * FROM cus_outstanding WHERE customer_id = $cid AND status != 'paid' ORDER BY sale_id ASC");

			$rows = [];
			$total_outstanding = 0;
			while ($row = $outstanding->fetch_assoc()) {
				$rows[] = $row;
				$total_outstanding += $row['amount'];
			}
			?>

			<div class="card">
				<div class="card-header">
					<h5>Outstanding for: <?php echo $customer['name']; ?></h5>
					<p>Total Outstanding: <strong>Rs <?php echo number_format($total_outstanding, 2); ?></strong></p>
				</div>
				<div class="card-body">
					<?php if (count($rows)): ?>
					<form method="POST" action="make_group_payment.php">
						<input type="hidden" name="customer_id" value="<?php echo $cid; ?>">

						<table class="table table-bordered">
							<thead>
								<tr>
									<th>Invoice</th>
									<th>Outstanding</th>
									<th>Status</th>
									<th>Enter Payment (Optional)</th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ($rows as $row): ?>
								<tr>
									<td>#<?php echo $row['sale_id']; ?></td>
									<td>Rs <?php echo number_format($row['amount'], 2); ?></td>
									<td><?php echo ucfirst($row['status']); ?></td>
									<td>
										<input type="hidden" name="sale_ids[]" value="<?php echo $row['sale_id']; ?>">
										<input type="hidden" name="sale_amounts[<?php echo $row['sale_id']; ?>]" value="<?php echo $row['amount']; ?>">
										<input type="number" step="0.01" name="invoice_payments[<?php echo $row['sale_id']; ?>]" class="form-control" placeholder="Rs">
									</td>
								</tr>
								<?php endforeach; ?>
							</tbody>
						</table>

						<div class="row">
							<div class="col-md-4 mb-3">
								<label>Total Amount (Optional)</label>
								<input type="number" step="0.01" name="amount_paid" class="form-control" placeholder="If not entered above">
							</div>
							<div class="col-md-8 mb-3">
								<label>Remarks</label>
								<input type="text" name="remarks" class="form-control" placeholder="Payment remarks">
							</div>
						</div>

						<button type="submit" class="btn btn-success">Submit Payment</button>
					</form>
					<?php else: ?>
						<p>No outstanding dues for this customer.</p>
					<?php endif; ?>
				</div>
			</div>
			<?php endif; ?>
		</div>
	</div>
</div>


<?php if (isset($_GET['success'])): ?>
<script>
Swal.fire({
	title: 'Payment Successful',
	text: 'Customer payments recorded.',
	icon: 'success',
	timer: 2500,
	showConfirmButton: false
});
</script>
<?php endif; ?>


<script>
$(document).ready(function () {
	$('.select2').select2();
});
</script>
</body>
</html>
