<?php
include 'db_connect.php';

if(!isset($_GET['id'])){
	echo '<div class="alert alert-danger">Invalid debt ID</div>';
	exit;
}

$debt_id = intval($_GET['id']);

// Get debt info
$debt = $conn->query("SELECT d.*, s.ref_no, c.name as customer_name
	FROM debt_list d
	INNER JOIN sales_list s ON s.id = d.sales_id
	INNER JOIN customer_list c ON c.id = d.customer_id
	WHERE d.id = ".$debt_id)->fetch_assoc();

if(!$debt){
	echo '<div class="alert alert-danger">Debt record not found</div>';
	exit;
}
?>

<div class="mb-4 p-3" style="background: var(--gray-100); border-radius: 8px;">
	<div class="row">
		<div class="col-md-6">
			<p class="mb-1"><strong>Customer:</strong> <?php echo htmlspecialchars($debt['customer_name']); ?></p>
			<p class="mb-1"><strong>Sale Reference:</strong> <code><?php echo htmlspecialchars($debt['ref_no']); ?></code></p>
			<p class="mb-0"><strong>Date Created:</strong> <?php echo date('M d, Y h:i A', strtotime($debt['date_created'])); ?></p>
		</div>
		<div class="col-md-6">
			<p class="mb-1"><strong>Total Amount:</strong> <span class="text-dark">TZS <?php echo number_format($debt['total_amount'], 0); ?></span></p>
			<p class="mb-1"><strong>Amount Paid:</strong> <span class="text-success">TZS <?php echo number_format($debt['amount_paid'], 0); ?></span></p>
			<p class="mb-0"><strong>Balance:</strong> <span class="text-danger font-weight-bold">TZS <?php echo number_format($debt['balance'], 0); ?></span></p>
		</div>
	</div>
</div>

<div class="row mb-4">
	<div class="col-md-4">
		<div class="text-center p-3" style="background: #e3f2fd; border-radius: 8px;">
			<h4 class="mb-0 text-primary">TZS <?php echo number_format($debt['total_amount'], 0); ?></h4>
			<small class="text-muted">Total Amount</small>
		</div>
	</div>
	<div class="col-md-4">
		<div class="text-center p-3" style="background: #e8f5e9; border-radius: 8px;">
			<h4 class="mb-0 text-success">TZS <?php echo number_format($debt['amount_paid'], 0); ?></h4>
			<small class="text-muted">Total Paid</small>
		</div>
	</div>
	<div class="col-md-4">
		<div class="text-center p-3" style="background: #ffebee; border-radius: 8px;">
			<h4 class="mb-0 text-danger">TZS <?php echo number_format($debt['balance'], 0); ?></h4>
			<small class="text-muted">Outstanding</small>
		</div>
	</div>
</div>

<h6 class="mb-3"><i class="fas fa-list mr-2"></i>Payment History</h6>
<div class="table-responsive">
	<table class="table table-sm table-bordered">
		<thead style="background: var(--gray-100);">
			<tr>
				<th>Date</th>
				<th>Amount</th>
				<th>Method</th>
				<th>Remarks</th>
			</tr>
		</thead>
		<tbody>
			<?php
			$payments = $conn->query("SELECT * FROM debt_payments WHERE debt_id = ".$debt_id." ORDER BY date_created DESC");

			if($payments && $payments->num_rows > 0):
				while($p = $payments->fetch_assoc()):
			?>
			<tr>
				<td><?php echo date('M d, Y h:i A', strtotime($p['date_created'])); ?></td>
				<td class="text-success font-weight-bold">+ TZS <?php echo number_format($p['amount'], 0); ?></td>
				<td>
					<span class="badge badge-light">
						<?php
						$method_icons = [
							'cash' => 'money-bill-wave',
							'bank_transfer' => 'university',
							'mobile_money' => 'mobile-alt'
						];
						$icon = $method_icons[$p['payment_method']] ?? 'wallet';
						?>
						<i class="fas fa-<?php echo $icon; ?> mr-1"></i>
						<?php echo ucwords(str_replace('_', ' ', $p['payment_method'])); ?>
					</span>
				</td>
				<td><?php echo htmlspecialchars($p['remarks'] ?? '-'); ?></td>
			</tr>
			<?php
				endwhile;
			else:
			?>
			<tr>
				<td colspan="4" class="text-center text-muted py-3">No payment records found</td>
			</tr>
			<?php endif; ?>
		</tbody>
	</table>
</div>

<?php if($debt['status'] == 'paid'): ?>
<div class="alert alert-success mt-3 mb-0">
	<i class="fas fa-check-circle mr-2"></i>
	<strong>Fully Paid!</strong> This debt has been completely settled.
</div>
<?php endif; ?>
