<?php
require_once __DIR__.'/../includes/functions.php';
$admin=require_super_admin(); $adminTitle='Admin Users & Roles'; verify_csrf();
if($_SERVER['REQUEST_METHOD']==='POST'){
  $action=$_POST['action']??''; $id=(int)($_POST['id']??0);
  if($action==='add'){
    $pass=(string)($_POST['password']??'');
    if(trim($_POST['name']??'') && filter_var($_POST['email']??'',FILTER_VALIDATE_EMAIL) && strlen($pass)>=8){
      q('INSERT INTO mj_admin_users(name,email,password_hash,role,country_id,is_active,created_at) VALUES(?,?,?,?,?,?,NOW())',[trim($_POST['name']),trim($_POST['email']),password_hash($pass,PASSWORD_DEFAULT),$_POST['role']?:'country_admin',$_POST['country_id']?:null,1]);
      admin_log('create_admin','mj_admin_users',(int)db()->lastInsertId()); flash('success','Admin user created.');
    } else flash('error','Valid name, email and password required.');
  } elseif($id>0 && in_array($action,['activate','suspend'],true)){
    q('UPDATE mj_admin_users SET is_active=? WHERE id=?',[$action==='activate'?1:0,$id]); admin_log($action.'_admin','mj_admin_users',$id); flash('success','Admin status updated.');
  }
  redirect('admin-users.php');
}
$rows=all_rows('SELECT a.*,c.name country FROM mj_admin_users a LEFT JOIN mj_countries c ON c.id=a.country_id ORDER BY a.id DESC');
$countries=all_rows('SELECT id,name,flag_emoji FROM mj_countries WHERE is_active=1 ORDER BY name');
include __DIR__.'/_header.php';
?>
<div class="row"><div class="col-lg-4"><div class="card card-primary card-outline"><div class="card-header"><h3 class="card-title">Add Admin</h3></div><form method="post"><div class="card-body"><?=csrf_input()?><input type="hidden" name="action" value="add"><div class="form-group"><label>Name</label><input class="form-control" name="name" required></div><div class="form-group"><label>Email</label><input class="form-control" type="email" name="email" required></div><div class="form-group"><label>Role</label><select class="form-control" name="role"><option value="super_admin">Super Admin</option><option value="country_admin">Country Admin</option><option value="moderator">Moderator</option><option value="support">Support</option><option value="finance">Finance</option><option value="content_editor">Content Editor</option></select></div><div class="form-group"><label>Country limit</label><select class="form-control" name="country_id"><option value="">All / none</option><?php foreach($countries as $c): ?><option value="<?=e($c['id'])?>"><?=e($c['flag_emoji'].' '.$c['name'])?></option><?php endforeach; ?></select></div><div class="form-group"><label>Password</label><input class="form-control" type="password" name="password" minlength="8" required></div></div><div class="card-footer"><button class="btn btn-primary btn-block">Create Admin</button></div></form></div></div><div class="col-lg-8"><div class="card"><div class="card-header"><h3 class="card-title">Admin Users</h3></div><div class="card-body"><table class="table table-striped mj-data-table"><thead><tr><th>ID</th><th>Name</th><th>Email</th><th>Role</th><th>Country</th><th>Status</th><th>Action</th></tr></thead><tbody><?php foreach($rows as $r): ?><tr><td><?=e($r['id'])?></td><td><?=e($r['name'])?></td><td><?=e($r['email'])?></td><td><span class="badge badge-info"><?=e($r['role'])?></span></td><td><?=e($r['country'])?></td><td><?=$r['is_active']?'<span class="badge badge-success">Active</span>':'<span class="badge badge-danger">Suspended</span>'?></td><td><form method="post" class="btn-group btn-group-sm"><?=csrf_input()?><input type="hidden" name="id" value="<?=e($r['id'])?>"><button name="action" value="activate" class="btn btn-success">Active</button><button name="action" value="suspend" class="btn btn-danger">Suspend</button></form></td></tr><?php endforeach; ?></tbody></table></div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
