<?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 SUPPLIER
================================ */

$stmt = $pdo->prepare("
SELECT *
FROM suppliers
WHERE id = ?
AND company_id = ?
");

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

if(!$supplier){

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

}

/* ===============================
FETCH PURCHASE ORDERS
================================ */

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

$stmt->execute([$supplier['id'],$company_id]);
$purchase_orders = $stmt->fetchAll();

?>

<div class="main-content">

<div class="page-header">

<div class="dashboard-cards">

<div class="card-small">
<h4>Purchase Orders</h4>
<p><?php echo count($purchase_orders); ?></p>
</div>

<div class="card-small">
<h4>Invoices</h4>
<p>0</p>
</div>

<div class="card-small">
<h4>Payments</h4>
<p>0</p>
</div>

</div>

<h2><?php echo htmlspecialchars($supplier['supplier_name']); ?></h2>

<a href="edit.php?id=<?php echo $supplier['id']; ?>" class="btn btn-primary">
Edit Supplier
</a>

</div>


<div class="stat-card">

<table class="table">

<tr>
<th>Supplier Code</th>
<td><?php echo htmlspecialchars($supplier['supplier_code']); ?></td>
</tr>

<tr>
<th>Supplier Type</th>
<td><?php echo htmlspecialchars($supplier['supplier_type']); ?></td>
</tr>

<tr>
<th>VAT Number</th>
<td><?php echo htmlspecialchars($supplier['vat_number']); ?></td>
</tr>

<tr>
<th>Contact Person</th>
<td><?php echo htmlspecialchars($supplier['contact_person']); ?></td>
</tr>

<tr>
<th>Email</th>
<td><?php echo htmlspecialchars($supplier['email']); ?></td>
</tr>

<tr>
<th>Phone</th>
<td><?php echo htmlspecialchars($supplier['phone']); ?></td>
</tr>

<tr>
<th>Address</th>
<td><?php echo htmlspecialchars($supplier['address']); ?></td>
</tr>

<tr>
<th>City</th>
<td><?php echo htmlspecialchars($supplier['city']); ?></td>
</tr>

<tr>
<th>Country</th>
<td><?php echo htmlspecialchars($supplier['country']); ?></td>
</tr>

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

</table>

</div>


<div class="stat-card">

<h3>Purchase Orders</h3>

<table class="table">

<thead>
<tr>
<th>PO Number</th>
<th>Order Date</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>

<tbody>

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

<tr>
<td colspan="4" style="text-align:center;padding:15px;">
No purchase orders found for this supplier
</td>
</tr>

<?php } ?>

<?php foreach($purchase_orders as $po){ ?>

<tr>

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

<td><?php echo htmlspecialchars($po['order_date']); ?></td>

<td><?php echo htmlspecialchars($po['status']); ?></td>

<td>

<a href="../purchase_orders/view.php?id=<?php echo $po['id']; ?>" class="btn btn-sm btn-info">
View
</a>

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

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