<?php
session_start();
$page_title = 'Notifications';
$active_nav = 'notifications';
include("includes/header.php");

// Mark all read on page visit
$ma = $conn->prepare("UPDATE notifications SET is_read=1 WHERE restaurant_id=?");
$ma->bind_param("i", $__rid); $ma->execute();

$type_filter = $_GET['type'] ?? 'all';

$q  = "SELECT n.* FROM notifications n WHERE n.restaurant_id=?";
$pa = [$__rid]; $ty = "i";
if ($type_filter !== 'all') { $q .= " AND n.type=?"; $pa[] = $type_filter; $ty .= "s"; }
$q .= " ORDER BY n.created_at DESC LIMIT 150";
$stmt = $conn->prepare($q); $stmt->bind_param($ty,...$pa); $stmt->execute();
$notifs = $stmt->get_result();

$type_icons = [
    'qr_scan'          => ['color'=>'var(--primary)',  'bg'=>'var(--primary-bg)',  'icon'=>'M3 7V5a2 2 0 012-2h2M17 3h2a2 2 0 012 2v2M21 17v2a2 2 0 01-2 2h-2M7 21H5a2 2 0 01-2-2v-2 M9 9h6v6H9z'],
    'new_order'        => ['color'=>'var(--warning)',  'bg'=>'var(--warning-bg)',  'icon'=>'M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2 M9 3h6v4H9z'],
    'payment_received' => ['color'=>'var(--success)',  'bg'=>'var(--success-bg)',  'icon'=>'M12 2v20M17 5H9.5a3.5 3.5 0 000 7h5a3.5 3.5 0 010 7H6'],
    'order_ready'      => ['color'=>'var(--success)',  'bg'=>'var(--success-bg)',  'icon'=>'M22 11.08V12a10 10 0 11-5.93-9.14 M22 4 12 14.01 9 11.01'],
    'low_stock'        => ['color'=>'var(--error)',    'bg'=>'var(--error-bg)',    'icon'=>'M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z M12 9v4M12 17h.01'],
];

$counts = [];
foreach (['all','qr_scan','new_order','payment_received','order_ready'] as $t) {
    $cx = $conn->prepare("SELECT COUNT(*) FROM notifications WHERE restaurant_id=?" . ($t==='all'?'':" AND type='$t'"));
    $cx->bind_param("i",$__rid); $cx->execute();
    $counts[$t] = $cx->get_result()->fetch_row()[0];
}
?>

<div style="display:flex;align-items:flex-start;justify-content:space-between;margin-bottom:24px;flex-wrap:wrap;gap:12px">
  <div>
    <h1 style="font-size:1.4rem;font-weight:800;letter-spacing:-.03em;margin-bottom:3px">Notifications</h1>
    <p style="color:var(--text-muted);font-size:.875rem">All activity for <?php echo htmlspecialchars($__rest_name); ?></p>
  </div>
</div>

<div style="display:flex;gap:8px;margin-bottom:20px;flex-wrap:wrap">
  <?php foreach (['all'=>'All','qr_scan'=>'QR Scans','new_order'=>'New Orders','payment_received'=>'Payments','order_ready'=>'Order Ready'] as $k=>$l): ?>
  <a href="?type=<?php echo $k; ?>" class="btn btn-sm <?php echo $type_filter===$k?'btn-primary':'btn-secondary'; ?>">
    <?php echo $l; ?> <span style="opacity:.7">(<?php echo $counts[$k] ?? 0; ?>)</span>
  </a>
  <?php endforeach; ?>
</div>

<div class="card">
  <?php if ($notifs->num_rows === 0): ?>
    <div style="padding:60px;text-align:center;color:var(--text-muted)">
      <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="var(--border)" stroke-width="1.5" stroke-linecap="round" style="margin:0 auto 14px;display:block"><path d="M18 8A6 6 0 006 8c0 7-3 9-3 9h18s-3-2-3-9M13.73 21a2 2 0 01-3.46 0"/></svg>
      <p style="font-weight:700">No notifications yet</p>
    </div>
  <?php else: ?>
    <?php while ($n = $notifs->fetch_assoc()):
      $ti = $type_icons[$n['type']] ?? $type_icons['new_order'];
    ?>
    <div style="display:flex;gap:14px;padding:16px 22px;border-bottom:1px solid var(--border)">
      <div style="width:42px;height:42px;border-radius:12px;background:<?php echo $ti['bg']; ?>;display:flex;align-items:center;justify-content:center;flex-shrink:0;margin-top:2px">
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="<?php echo $ti['color']; ?>" stroke-width="2" stroke-linecap="round"><path d="<?php echo $ti['icon']; ?>"/></svg>
      </div>
      <div style="flex:1">
        <div style="display:flex;align-items:flex-start;justify-content:space-between;gap:12px;flex-wrap:wrap">
          <div>
            <div style="font-weight:700;font-size:.875rem;margin-bottom:3px"><?php echo htmlspecialchars($n['title']); ?></div>
            <div style="font-size:.8rem;color:var(--text-muted)"><?php echo htmlspecialchars($n['message'] ?? ''); ?></div>
          </div>
          <div style="font-size:.72rem;color:var(--text-muted);white-space:nowrap;flex-shrink:0"><?php echo date('M j, Y g:i A', strtotime($n['created_at'])); ?></div>
        </div>
        <?php if ($n['type']==='new_order' && $n['related_id']): ?>
        <a href="orders.php?id=<?php echo $n['related_id']; ?>" class="btn btn-primary btn-sm" style="margin-top:10px">View Order</a>
        <?php elseif ($n['type']==='qr_scan'): ?>
        <a href="qr_scans.php" class="btn btn-secondary btn-sm" style="margin-top:10px">View Scans</a>
        <?php endif; ?>
      </div>
    </div>
    <?php endwhile; ?>
  <?php endif; ?>
</div>

<?php include("includes/footer.php"); ?>
