<?php
require_once __DIR__.'/includes/functions.php'; $u=require_user(); verify_csrf(); mj263_schema(); $pageTitle='Notification Settings';
$types=['listing'=>'Listing updates','offer'=>'Offers','chat'=>'Chat messages','renewal'=>'Renewals','payment'=>'Payments','deals'=>'Deals and promotions','saved_search'=>'Saved search alerts'];
if($_SERVER['REQUEST_METHOD']==='POST'){
  foreach($types as $key=>$label){ $in=isset($_POST['in_app'][$key])?1:0; $em=isset($_POST['email'][$key])?1:0; $pop=isset($_POST['popup'][$key])?1:0; q('INSERT INTO mj_user_notification_preferences(user_id,notification_type,in_app_enabled,email_enabled,popup_enabled) VALUES(?,?,?,?,?) ON DUPLICATE KEY UPDATE in_app_enabled=VALUES(in_app_enabled),email_enabled=VALUES(email_enabled),popup_enabled=VALUES(popup_enabled)',[$u['id'],$key,$in,$em,$pop]); }
  flash('success','Notification preferences saved.'); redirect('notification-settings.php');
}
$prefs=[]; if(table_exists('mj_user_notification_preferences')) foreach(all_rows('SELECT * FROM mj_user_notification_preferences WHERE user_id=?',[$u['id']]) as $p){$prefs[$p['notification_type']]=$p;}
include __DIR__.'/includes/header.php'; ?>
<section class="mj-page-hero"><div class="container"><h1 class="fw-bold">Notification Settings</h1><p class="text-muted mb-0">Control popups, in-app alerts and email notifications.</p></div></section><section class="mj-section"><div class="container"><form method="post" class="mj-card p-4"><?=csrf_input()?><?php show_flash(); ?><div class="table-responsive"><table class="table"><thead><tr><th>Type</th><th>In-app</th><th>Email</th><th>Live popup</th></tr></thead><tbody><?php foreach($types as $k=>$label): $p=$prefs[$k]??['in_app_enabled'=>1,'email_enabled'=>1,'popup_enabled'=>1]; ?><tr><td><b><?=e($label)?></b></td><td><input type="checkbox" name="in_app[<?=$k?>]" <?=$p['in_app_enabled']?'checked':''?>></td><td><input type="checkbox" name="email[<?=$k?>]" <?=$p['email_enabled']?'checked':''?>></td><td><input type="checkbox" name="popup[<?=$k?>]" <?=$p['popup_enabled']?'checked':''?>></td></tr><?php endforeach; ?></tbody></table></div><button class="btn btn-primary">Save Preferences</button></form></div></section><?php include __DIR__.'/includes/footer.php'; ?>
