<?php
require_once __DIR__ . '/../../includes/bootstrap.php';
require_login();
require_once __DIR__ . '/quotation_helpers.php';

$pageTitle = 'Quotations';
$status = trim((string)($_GET['status'] ?? ''));
$search = trim((string)($_GET['q'] ?? ''));

$where = [];
$params = [];
if ($status !== '') { $where[] = 'q.status = :status'; $params[':status'] = $status; }
if ($search !== '') {
    $where[] = '(q.quote_no LIKE :s OR q.guest_name LIKE :s OR CAST(q.id AS CHAR) LIKE :s OR p.name LIKE :s)';
    $params[':s'] = '%' . $search . '%';
}
$sqlWhere = $where ? ('WHERE ' . implode(' AND ', $where)) : '';

$stmt = $pdo->prepare("SELECT q.*, p.name AS property_name, p.code AS property_code, CONCAT(COALESCE(a.firstname,''),' ',COALESCE(a.lastname,'')) AS agent_name FROM reservation_quotes q LEFT JOIN properties p ON p.id=q.property_id LEFT JOIN agents a ON a.id=q.agent_id {$sqlWhere} ORDER BY q.created_at DESC, q.id DESC");
$stmt->execute($params);
$quotes = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];

$counts = ['draft'=>0,'sent'=>0,'accepted'=>0,'converted'=>0,'cancelled'=>0,'expired'=>0];
try { foreach($pdo->query("SELECT status, COUNT(*) c FROM reservation_quotes GROUP BY status") as $r){ $counts[$r['status']] = (int)$r['c']; } } catch(Throwable $e) {}

require_once __DIR__ . '/../../includes/header.php';
?>
<div class="quote-hero mb-2">
  <div>
    <div class="page-eyebrow"><i class="bi bi-file-earmark-text"></i> Reservations</div>
    <h4 class="mb-0 fw-bold">Quotations</h4>
    <div class="text-muted small">Create flexible cost proposals before converting them into confirmed bookings.</div>
  </div>
  <a href="quotation_form.php" class="btn btn-primary btn-sm"><i class="bi bi-plus-circle me-1"></i>New Quote</a>
</div>

<div class="quote-stats mb-2">
  <?php foreach($counts as $k=>$v): ?>
    <a class="quote-stat" href="quotation_list.php?status=<?php echo qh($k); ?>"><span><?php echo qh(ucfirst($k)); ?></span><strong><?php echo (int)$v; ?></strong></a>
  <?php endforeach; ?>
</div>

<div class="card panel-card mb-2"><div class="card-body p-2">
  <form class="row g-2 align-items-end" method="get">
    <div class="col-md-3"><label class="form-label">Status</label><select name="status" class="form-select"><option value="">All</option><?php foreach(array_keys($counts) as $s): ?><option value="<?php echo qh($s); ?>" <?php echo $status===$s?'selected':''; ?>><?php echo qh(ucfirst($s)); ?></option><?php endforeach; ?></select></div>
    <div class="col-md-7"><label class="form-label">Search</label><input name="q" class="form-control" value="<?php echo qh($search); ?>" placeholder="Quote no, guest, property..."></div>
    <div class="col-md-2 d-grid"><button class="btn btn-primary">Search</button></div>
  </form>
</div></div>

<div class="card panel-card"><div class="table-responsive quote-table-wrap">
<table class="table table-modern align-middle mb-0">
<thead><tr><th>Quote</th><th>Guest / Agent</th><th>Property</th><th>Stay</th><th class="text-center">Rooms</th><th class="text-end">Total</th><th>Status</th><th class="text-end">Actions</th></tr></thead>
<tbody>
<?php if(!$quotes): ?><tr><td colspan="8" class="text-center text-muted py-5">No quotations found.</td></tr><?php endif; ?>
<?php foreach($quotes as $q): ?>
<tr>
<td><strong><?php echo qh($q['quote_no']); ?></strong><div class="small text-muted">Created <?php echo qh($q['created_at']); ?></div></td>
<td><div class="fw-semibold"><?php echo qh($q['guest_name']); ?></div><div class="small text-muted"><?php echo qh(trim($q['agent_name']) ?: 'Direct'); ?></div></td>
<td><?php echo qh(trim(($q['property_name']??'') . (!empty($q['property_code']) ? ' ('.$q['property_code'].')' : '')) ?: 'Not set'); ?></td>
<td><?php echo qh($q['check_in']); ?><br><span class="small text-muted"><?php echo qh($q['check_out']); ?></span></td>
<td class="text-center"><?php echo (int)$q['totalrooms']; ?></td>
<td class="text-end fw-bold"><?php echo qh($q['currency']); ?> <?php echo quote_money($q['totalamount']); ?></td>
<td><span class="badge text-bg-<?php echo $q['status']==='converted'?'success':($q['status']==='cancelled'?'danger':($q['status']==='accepted'?'primary':'secondary')); ?>"><?php echo qh(ucfirst($q['status'])); ?></span></td>
<td class="text-end"><div class="btn-group btn-group-sm"><a class="btn btn-light" href="quotation_form.php?id=<?php echo (int)$q['id']; ?>">Edit</a><a class="btn btn-outline-secondary" target="_blank" href="quotation_pdf.php?id=<?php echo (int)$q['id']; ?>">Preview</a><?php if($q['status']!=='converted'): ?><a class="btn btn-primary" href="quotation_convert.php?id=<?php echo (int)$q['id']; ?>" onclick="return confirm('Convert this quote into a booking?');">Confirm</a><?php endif; ?></div></td>
</tr>
<?php endforeach; ?>
</tbody></table>
</div></div>
<style>
.quote-hero{display:flex;justify-content:space-between;align-items:center;gap:10px;background:#fff;border:1px solid #e7edf5;border-radius:16px;padding:10px 12px;box-shadow:0 8px 22px rgba(15,23,42,.04)}
.quote-stats{display:grid;grid-template-columns:repeat(6,minmax(100px,1fr));gap:8px}.quote-stat{text-decoration:none;color:#0f172a;background:#fff;border:1px solid #e7edf5;border-radius:14px;padding:8px 10px;display:flex;justify-content:space-between}.quote-stat span{font-size:.75rem;color:#64748b;font-weight:800}.quote-stat strong{font-size:1rem}.quote-table-wrap thead th{position:sticky;top:0;background:#fff;z-index:2}@media(max-width:900px){.quote-stats{grid-template-columns:repeat(2,1fr)}}
</style>
<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
