<?php

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

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

$company_id = $_SESSION['company_id'] ?? 0;
$role_id    = $_SESSION['role_id'] ?? 0;

$search = $_GET['search'] ?? '';

/* ==============================
FETCH QUOTATIONS
============================== */

if ($role_id == 7) {

$stmt = $pdo->prepare("
SELECT q.*, c.company_name
FROM quotations q
LEFT JOIN companies c ON q.customer_id = c.id
WHERE (q.quote_number LIKE ? OR c.company_name LIKE ?)
ORDER BY q.created_at DESC
");

$stmt->execute(["%$search%","%$search%"]);

} else {

$stmt = $pdo->prepare("
SELECT q.*, c.company_name
FROM quotations q
LEFT JOIN companies c ON q.customer_id = c.id
WHERE q.company_id = ?
AND (q.quote_number LIKE ? OR c.company_name LIKE ?)
ORDER BY q.created_at DESC
");

$stmt->execute([$company_id,"%$search%","%$search%"]);

}

$quotes = $stmt->fetchAll(PDO::FETCH_ASSOC);


/* ==============================
LOAD LAYOUT
============================== */

$breadcrumb = "Quotations";
require_once __DIR__ . '/../../../includes/layout.php';

?>

<div class="container">

<div class="page-header">
<h2>Quotations</h2>
<p class="page-subtitle">
Manage quotations for customers.
</p>
</div>


<div class="topbar">

<a class="btn" href="create.php">+ Create Quotation</a>

<form method="GET" style="display:inline-block;margin-left:20px">

<input
type="text"
name="search"
placeholder="Search Quote # or Customer"
value="<?= htmlspecialchars($search) ?>"
style="padding:6px"
>

<button class="btn">Search</button>

</form>

</div>


<div class="table-wrap">

<table class="data-table">

<thead>

<tr>
<th>Quote #</th>
<th>Customer</th>
<th>Date</th>
<th>Status</th>
<th>Total</th>
<th>Actions</th>
</tr>

</thead>

<tbody>

<?php if(!$quotes): ?>

<tr>
<td colspan="6">No quotations found.</td>
</tr>

<?php else: ?>

<?php foreach($quotes as $q): ?>

<tr>

<td>
<strong><?= htmlspecialchars($q['quote_number']) ?></strong>
</td>

<td>
<?= htmlspecialchars($q['company_name'] ?? 'No Customer') ?>
</td>

<td>
<?= date("d M Y",strtotime($q['quote_date'])) ?>
</td>

<td>

<?php

$status = strtoupper($q['status'] ?? 'DRAFT');

$class = 'status-draft';

if($status == 'SENT') $class = 'status-sent';
if($status == 'CONVERTED') $class = 'status-converted';
if($status == 'CANCELLED') $class = 'status-cancelled';

?>

<span class="status-badge <?= $class ?>">
<?= htmlspecialchars($status) ?>
</span>

</td>

<td>
<strong>R <?= number_format($q['total'],2) ?></strong>
</td>

<td>

<a class="btn-small view"
href="view.php?id=<?= (int)$q['id'] ?>">
View
</a>

<a class="btn-small edit"
href="edit.php?id=<?= (int)$q['id'] ?>">
Edit
</a>

<a class="btn-small"
href="preview.php?id=<?= (int)$q['id'] ?>">
Preview
</a>

<a class="btn-small"
href="pdf.php?id=<?= (int)$q['id'] ?>">
PDF
</a>

</td>

</tr>

<?php endforeach; ?>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

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