<?php
require_once __DIR__.'/../includes/functions.php';
$admin=require_super_admin(); $adminTitle='Super Admin Center'; verify_csrf();
if($_SERVER['REQUEST_METHOD']==='POST'){
  $settings=['listing_approval_required','company_approval_required','auto_area_priority','allow_ip_area_detection','maintenance_mode','quiet_hours_start','quiet_hours_end'];
  foreach($settings as $k){ q('INSERT INTO mj_settings(setting_key,setting_value) VALUES(?,?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)',[$k,(string)($_POST[$k] ?? '0')]); }
  admin_log('update_super_admin_settings','mj_settings',0,$_POST); flash('success','Super Admin settings updated.'); redirect('super-admin.php');
}
$stats=['countries'=>safe_count('SELECT COUNT(*) c FROM mj_countries WHERE is_active=1'),'users'=>safe_count('SELECT COUNT(*) c FROM mj_users'),'admins'=>safe_count('SELECT COUNT(*) c FROM mj_admin_users'),'seed'=>safe_count('SELECT COUNT(*) c FROM mj_company_seed'),'reports'=>safe_count('SELECT COUNT(*) c FROM mj_reports WHERE status IN ("new","reviewing")'),'payments'=>0];
include __DIR__.'/_header.php';
?>
<div class="row"><?php foreach($stats as $k=>$v): ?><div class="col-lg-2 col-6"><div class="small-box bg-primary"><div class="inner"><h3><?=number_format($v)?></h3><p><?=e(ucwords(str_replace('_',' ',$k)))?></p></div><div class="icon"><i class="fas fa-crown"></i></div></div></div><?php endforeach; ?></div>
<div class="row"><div class="col-lg-8"><div class="card card-danger card-outline"><div class="card-header"><h3 class="card-title"><i class="fas fa-user-shield mr-2"></i>Global Control Settings</h3></div><form method="post"><div class="card-body"><?=csrf_input()?><div class="row"><div class="col-md-6"><div class="custom-control custom-switch mb-3"><input type="checkbox" class="custom-control-input" id="la" name="listing_approval_required" value="1" <?=setting('listing_approval_required','1')==='1'?'checked':''?>><label class="custom-control-label" for="la">Listing approval required</label></div><div class="custom-control custom-switch mb-3"><input type="checkbox" class="custom-control-input" id="ca" name="company_approval_required" value="1" <?=setting('company_approval_required','1')==='1'?'checked':''?>><label class="custom-control-label" for="ca">Company approval required</label></div><div class="custom-control custom-switch mb-3"><input type="checkbox" class="custom-control-input" id="ap" name="auto_area_priority" value="1" <?=setting('auto_area_priority','1')!=='0'?'checked':''?>><label class="custom-control-label" for="ap">Area priority sorting enabled</label></div></div><div class="col-md-6"><div class="custom-control custom-switch mb-3"><input type="checkbox" class="custom-control-input" id="ip" name="allow_ip_area_detection" value="1" <?=setting('allow_ip_area_detection','1')!=='0'?'checked':''?>><label class="custom-control-label" for="ip">Browser/IP area detection enabled</label></div><div class="custom-control custom-switch mb-3"><input type="checkbox" class="custom-control-input" id="mm" name="maintenance_mode" value="1" <?=setting('maintenance_mode','0')==='1'?'checked':''?>><label class="custom-control-label" for="mm">Maintenance mode</label></div><div class="form-row"><div class="form-group col"><label>Quiet hours start</label><input class="form-control" name="quiet_hours_start" value="<?=e(setting('quiet_hours_start','22:00'))?>"></div><div class="form-group col"><label>Quiet hours end</label><input class="form-control" name="quiet_hours_end" value="<?=e(setting('quiet_hours_end','08:00'))?>"></div></div></div></div></div><div class="card-footer"><button class="btn btn-danger"><i class="fas fa-save mr-1"></i>Save Global Settings</button></div></form></div></div>
<div class="col-lg-4"><div class="card card-warning card-outline"><div class="card-header"><h3 class="card-title">High-power Actions</h3></div><div class="card-body"><a class="btn btn-app" href="admin-users.php"><i class="fas fa-users-cog"></i>Admin Users</a><a class="btn btn-app" href="audit-logs.php"><i class="fas fa-history"></i>Audit Logs</a><a class="btn btn-app" href="backup.php"><i class="fas fa-heartbeat"></i>Health</a><a class="btn btn-app" href="import-center.php"><i class="fas fa-file-import"></i>Imports</a><a class="btn btn-app" href="modules.php"><i class="fas fa-puzzle-piece"></i>Modules</a><a class="btn btn-app" href="seo.php"><i class="fas fa-search"></i>SEO</a></div></div></div></div>
<div class="card"><div class="card-header"><h3 class="card-title">Recommended Super Admin Checklist</h3></div><div class="card-body"><div class="row"><div class="col-md-4"><div class="info-box"><span class="info-box-icon bg-info"><i class="fas fa-map-marker-alt"></i></span><div class="info-box-content"><span class="info-box-text">Area priority</span><span class="info-box-number">City → State → Country sorting</span></div></div></div><div class="col-md-4"><div class="info-box"><span class="info-box-icon bg-success"><i class="fas fa-user-secret"></i></span><div class="info-box-content"><span class="info-box-text">Login as user</span><span class="info-box-number">Support users safely</span></div></div></div><div class="col-md-4"><div class="info-box"><span class="info-box-icon bg-warning"><i class="fas fa-shield-alt"></i></span><div class="info-box-content"><span class="info-box-text">Fraud control</span><span class="info-box-number">Reports, keywords, IP/device logs</span></div></div></div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
