<?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'];

/* ===============================
FETCH SUPPLIERS
================================ */

$stmt = $pdo->prepare("
SELECT *
FROM suppliers
WHERE company_id = ?
ORDER BY supplier_name ASC
");

$stmt->execute([$company_id]);
$suppliers = $stmt->fetchAll();

?>

<div class="main-content">

<div class="page-header">
<h2>Suppliers</h2>

<a href="create.php" class="btn btn-primary">+ Add Supplier</a>

</div>

<div class="stat-card">

<table class="table">

<thead>

<tr>
<th>Code</th>
<th>Name</th>
<th>Type</th>
<th>VAT</th>
<th>Phone</th>
<th>Status</th>
<th>Actions</th>
</tr>

</thead>

<tbody>

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

<tr>
<td colspan="7" style="text-align:center;padding:20px;">
No suppliers found. Click "Add Supplier" to create one.
</td>
</tr>

<?php } else { ?>

<?php foreach($suppliers as $supplier){ ?>

<tr>

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

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

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

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

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

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

<td>

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

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

</td>

</tr>

<?php } ?>

<?php } ?>

</tbody>

</table>

</div>

</div>

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