<?php
require_once __DIR__.'/includes/functions.php';
require_once __DIR__.'/includes/account-v281-ui.php';
mj_v281_ensure_schema();
$u=require_user(); $userId=(int)$u['id']; $pageTitle='Notifications';
$tabs=['inbox'=>'Inbox','unread'=>'Unread','preferences'=>'Preferences','rules'=>'Rules'];
$tab=(string)($_GET['tab']??'inbox'); if(!isset($tabs[$tab])){$tab='inbox';}
try{
  if($_SERVER['REQUEST_METHOD']==='POST'){
    verify_csrf();
    if((string)($_POST['action']??'')==='save_preferences'){
      mj_v281_save_user_preferences($userId,$_POST);
      flash('success','Notification preferences saved.'); redirect('notifications.php?tab=preferences');
    }
  }
}catch(Throwable $e){ flash('error',$e->getMessage()); }
$prefs=mj_v281_user_preferences($userId);
$where='user_id=?'; $params=[$userId]; if($tab==='unread'){$where.=' AND is_read=0';}
$notifs=table_exists('mj_notifications')?all_rows('SELECT * FROM mj_notifications WHERE '.$where.' ORDER BY is_read ASC, id DESC LIMIT 60',$params):[];
include __DIR__.'/includes/header.php';
?>
<div class="container py-5"><div class="sidebar-layout"><?php include __DIR__.'/includes/account-sidebar.php'; ?><section>
  <div class="mj-page-hero rounded-4 mb-4"><div class="container-fluid"><span class="mj-badge mb-2"><i class="bi bi-bell"></i> Notifications</span><h1 class="fw-bold mb-1">Notifications</h1><p class="text-muted mb-0">Listing expiry, quote received, offer received, payment approval, admin messages, missed leads and daily digest.</p></div></div>
  <?php mj_v281_tabs($tabs,$tab,'notifications.php'); ?>
  <?php if($tab==='preferences'): ?>
    <form method="post" class="card card-body border-0 rounded-4 shadow-sm"><?=csrf_input()?><input type="hidden" name="action" value="save_preferences"><h3 class="h5 fw-bold mb-3">Notification Preferences</h3><div class="row g-3"><?php foreach(['notify_inapp'=>'In-app Alerts','notify_email'=>'Email Alerts','notify_sms'=>'SMS Alerts','notify_whatsapp'=>'WhatsApp Alerts','daily_digest'=>'Daily Digest','important_alerts_only'=>'Important Alerts Only','do_not_disturb'=>'Do Not Disturb'] as $k=>$label): ?><div class="col-md-4"><div class="form-check form-switch"><input class="form-check-input" type="checkbox" name="<?=e($k)?>" value="1" <?=!empty($prefs[$k])?'checked':''?>><label class="form-check-label fw-bold"><?=e($label)?></label></div></div><?php endforeach; ?></div><button class="btn btn-primary rounded-pill mt-3">Save Preferences</button></form>
  <?php elseif($tab==='rules'): ?>
    <?php mj_v281_feature_grid([['bi-clock','Expiry Alerts','7 days, 3 days, 1 day and expired listing alerts.'],['bi-receipt','Quote & Offer Alerts','Quote received, offer received, accepted/rejected and chat updates.'],['bi-credit-card','Payment Alerts','Payment request, proof uploaded, approved, rejected and invoice overdue.'],['bi-people','CRM Alerts','Hot lead, missed lead, follow-up due and customer reply.'],['bi-shield-check','Verification Alerts','Verification approved, rejected or expiring.'],['bi-envelope-paper','Daily Digest','Group non-urgent alerts into one clean email/notification.']]); ?>
  <?php else: ?>
    <div class="card card-body border-0 rounded-4 shadow-sm"><?php if(!$notifs): mj_v281_empty($tab==='unread'?'No unread notifications':'No notifications yet','Important marketplace notifications will appear here.','bi-bell'); else: ?><div class="list-group list-group-flush"><?php foreach($notifs as $n): ?><a class="list-group-item list-group-item-action d-flex gap-3 py-3" href="<?=e($n['action_url'] ?: '#')?>"><i class="bi <?=e($n['icon'] ?: 'bi-bell')?> fs-4 text-primary"></i><div class="w-100"><div class="d-flex justify-content-between"><b><?=e($n['title'] ?? 'Notification')?></b><?=empty($n['is_read'])?'<span class="badge text-bg-danger">New</span>':''?></div><p class="text-muted mb-1"><?=e($n['message'] ?? '')?></p><small class="text-muted"><?=e($n['created_at'] ?? '')?> · <?=e($n['type'] ?? 'system')?></small></div></a><?php endforeach; ?></div><?php endif; ?></div>
  <?php endif; ?>
</section></div></div>
<?php include __DIR__.'/includes/footer.php'; ?>
