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

$id = (int)($_GET['id'] ?? 0);

if(!$id){
    die("Invalid invoice");
}

// ================= INVOICE =================
$stmt = $conn->prepare("
SELECT i.*, c.name, c.phone, c.email
FROM invoices i
JOIN customers c ON i.customer_id = c.id
WHERE i.id=?
");
$stmt->bind_param("i", $id);
$stmt->execute();
$invoice = $stmt->get_result()->fetch_assoc();

if(!$invoice){
    die("Invoice not found");
}

// ================= ITEMS =================
$items = $conn->query("
SELECT it.*, s.service_name
FROM invoice_items it
LEFT JOIN services s ON s.id = it.service_id
WHERE it.invoice_id = $id
");

// ================= PAYMENTS =================
$payments = $conn->query("
SELECT * FROM payments WHERE invoice_id=$id ORDER BY payment_date ASC
");

// ================= TOTAL PAID =================
$paid = $conn->query("
SELECT IFNULL(SUM(amount),0) as total FROM payments WHERE invoice_id=$id
")->fetch_assoc()['total'];

$pending = $invoice['total'] - $paid;
?>

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

<div class="container mt-4">

<h4>Invoice #<?= $id ?></h4>

<!-- CUSTOMER -->
<div class="card p-3 mb-3">
    <h6>Customer</h6>
    <p><?= e($invoice['name']) ?></p>
    <p><?= $invoice['phone'] ?></p>
    <p><?= $invoice['email'] ?></p>
</div>

<!-- INFO -->
<div class="card p-3 mb-3">
    <p><b>Date:</b> <?= $invoice['invoice_date'] ?></p>
    <p><b>Due Date:</b> <?= $invoice['due_date'] ?></p>
</div>

<!-- ITEMS -->
<div class="card p-3 mb-3">
<h6>Items</h6>

<table class="table table-bordered">
<tr>
<th>Service</th>
<th>Amount</th>
</tr>

<?php while($row = $items->fetch_assoc()){ ?>
<tr>
<td>
<?= e($row['service_name'] ?? $row['description']) ?>
</td>
<td>₹<?= number_format($row['amount'],2) ?></td>
</tr>
<?php } ?>

<tr>
<th>Total</th>
<th>₹<?= number_format($invoice['total'],2) ?></th>
</tr>

</table>
</div>

<!-- PAYMENTS -->
<div class="card p-3 mb-3">
<h6>Payments</h6>

<table class="table table-bordered">
<tr>
<th>Date</th>
<th>Method</th>
<th>Amount</th>
</tr>

<?php if($payments->num_rows > 0){ ?>
<?php while($p = $payments->fetch_assoc()){ ?>
<tr>
<td><?= $p['payment_date'] ?></td>
<td><?= $p['method'] ?></td>
<td class="text-success">₹<?= number_format($p['amount'],2) ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr><td colspan="3" class="text-center">No payments</td></tr>
<?php } ?>

</table>
</div>

<!-- SUMMARY -->
<div class="card p-3">

<div class="row text-center">

<div class="col-md-4">
<h6>Total</h6>
<h5>₹<?= number_format($invoice['total'],2) ?></h5>
</div>

<div class="col-md-4">
<h6>Paid</h6>
<h5 class="text-success">₹<?= number_format($paid,2) ?></h5>
</div>

<div class="col-md-4">
<h6>Pending</h6>
<h5 class="text-danger">₹<?= number_format($pending,2) ?></h5>
</div>

</div>

</div>

<br>

<a href="invoices.php" class="btn btn-secondary">Back</a>

<?php if($pending > 0){ ?>
<a href="add_payment.php?id=<?= $id ?>" class="btn btn-success">
Add Payment
</a>
<?php } ?>

</div>

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