<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

require_once __DIR__.'/../../includes/auth.php';
requireLogin();

require_once __DIR__.'/../../config/database.php';

require_once __DIR__.'/../../includes/header.php';
require_once __DIR__.'/../../includes/sidebar.php';

$company_id = $_SESSION['company_id'];

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

/* ===============================
FETCH INVOICE
================================ */

$stmt = $pdo->prepare("
SELECT si.*, s.supplier_name, po.po_number
FROM supplier_invoices si
LEFT JOIN suppliers s ON si.supplier_id = s.id
LEFT JOIN purchase_orders po ON si.purchase_order_id = po.id
WHERE si.id = ?
AND si.company_id = ?
");

$stmt->execute([$id,$company_id]);
$invoice = $stmt->fetch();

if(!$invoice){

echo "<div class='main-content'><div class='stat-card'>Invoice not found.</div></div>";
require_once __DIR__.'/../../includes/footer.php';
exit;

}

/* ===============================
ADD PAYMENT
================================ */

if(isset($_POST['add_payment'])){

$amount = $_POST['amount'];
$method = $_POST['payment_method'];
$reference = $_POST['reference'];

$stmt = $pdo->prepare("
INSERT INTO supplier_payments
(company_id,supplier_invoice_id,payment_date,amount,payment_method,reference)
VALUES (?,?,?,?,?,?)
");

$stmt->execute([
$company_id,
$id,
date('Y-m-d'),
$amount,
$method,
$reference
]);

/* update invoice paid amount */

$stmt = $pdo->prepare("
UPDATE supplier_invoices
SET paid_amount = paid_amount + ?
WHERE id = ?
AND company_id = ?
");

$stmt->execute([$amount,$id,$company_id]);

/* update status */

$stmt = $pdo->prepare("
SELECT total_amount,paid_amount
FROM supplier_invoices
WHERE id = ?
");

$stmt->execute([$id]);
$row = $stmt->fetch();

$status = "partial";

if($row['paid_amount'] >= $row['total_amount']){

$status = "paid";

}

$stmt = $pdo->prepare("
UPDATE supplier_invoices
SET status = ?
WHERE id = ?
");

$stmt->execute([$status,$id]);

header("Location: view.php?id=".$id);
exit;

}

/* ===============================
FETCH PAYMENTS
================================ */

$stmt = $pdo->prepare("
SELECT *
FROM supplier_payments
WHERE supplier_invoice_id = ?
AND company_id = ?
ORDER BY created_at DESC
");

$stmt->execute([$id,$company_id]);
$payments = $stmt->fetchAll();

?>

<div class="main-content">

<div class="page-header">

<h2><?php echo htmlspecialchars($po['po_number']); ?></h2>

<div>

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

<a href="../supplier_invoices/create.php?po_id=<?php echo $po['id']; ?>" 
class="btn btn-primary">
Create Supplier Invoice
</a>

</div>

</div>

<div class="stat-card">

<table class="table">

<tr>
<th>Invoice</th>
<td><?php echo htmlspecialchars($invoice['invoice_number']); ?></td>
</tr>

<tr>
<th>Supplier</th>
<td><?php echo htmlspecialchars($invoice['supplier_name']); ?></td>
</tr>

<tr>
<th>PO</th>
<td><?php echo htmlspecialchars($invoice['po_number']); ?></td>
</tr>

<tr>
<th>Total</th>
<td>R <?php echo number_format($invoice['total_amount'],2); ?></td>
</tr>

<tr>
<th>Paid</th>
<td>R <?php echo number_format($invoice['paid_amount'],2); ?></td>
</tr>

<tr>
<th>Status</th>
<td><?php echo htmlspecialchars($invoice['status']); ?></td>
</tr>

</table>

</div>

<div class="stat-card">

<h3>Record Payment</h3>

<form method="POST">

<input type="hidden" name="add_payment" value="1">

<div class="form-row">

<div class="form-group">

<label>Amount</label>
<input type="number" step="0.01" name="amount" required>

</div>

<div class="form-group">

<label>Payment Method</label>

<select name="payment_method">

<option>Bank Transfer</option>
<option>Cash</option>
<option>Card</option>

</select>

</div>

<div class="form-group">

<label>Reference</label>
<input type="text" name="reference">

</div>

</div>

<button class="btn btn-primary">
Record Payment
</button>

</form>

</div>

<div class="stat-card">

<h3>Payments</h3>

<div class="table-responsive">

<table class="table table-striped">

<thead>

<tr>
<th>Date</th>
<th>Amount</th>
<th>Method</th>
<th>Reference</th>
</tr>

</thead>

<tbody>

<?php if(count($payments)==0){ ?>

<tr>
<td colspan="4" style="text-align:center;">
No payments yet
</td>
</tr>

<?php } ?>

<?php foreach($payments as $p){ ?>

<tr>

<td><?php echo $p['payment_date']; ?></td>

<td>R <?php echo number_format($p['amount'],2); ?></td>

<td><?php echo htmlspecialchars($p['payment_method']); ?></td>

<td><?php echo htmlspecialchars($p['reference']); ?></td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

<?php require_once __DIR__.'/../../includes/footer.php'; ?>