<?php require_once __DIR__.'/../includes/auth.php'; require_login(['admin']);
if($_SERVER['REQUEST_METHOD']==='POST'){ verify_csrf(); $token=bin2hex(random_bytes(24)); $hash=password_hash(bin2hex(random_bytes(10)), PASSWORD_DEFAULT); db()->prepare('INSERT INTO users(first_name,last_name,email,role,brand,department,position,status,invite_token,password_hash,site) VALUES(?,?,?,?,?,?,?,?,?,?,?)')->execute([$_POST['first_name'],$_POST['last_name'],$_POST['email'],$_POST['role'],$_POST['brand'],$_POST['department'],$_POST['position'],'invited',$token,$hash,$_POST['site']]); $newId=db()->lastInsertId();
$paths=$_POST['paths']??[]; foreach($paths as $pathId){ db()->prepare('INSERT IGNORE INTO user_learning_paths(user_id,learning_path_id) VALUES(?,?)')->execute([$newId,$pathId]); }
$invite=APP_URL.'/set-password.php?token='.$token; @mail($_POST['email'],'Welcome to Faithorn & Stag Academy',"Create your login: $invite",'From: '.MAIL_FROM); $notice="Invite created: $invite"; }
$paths=db()->query('SELECT * FROM learning_paths ORDER BY brand, level, name')->fetchAll(); $users=db()->query('SELECT * FROM users ORDER BY created_at DESC')->fetchAll(); require __DIR__.'/../includes/header.php'; ?>
<div class="card"><h1>Add employee / manager</h1><?php if(!empty($notice)): ?><p class="correct"><?= htmlspecialchars($notice) ?></p><?php endif; ?><form method="post"><input type="hidden" name="csrf" value="<?= csrf_token() ?>"><div class="grid"><input name="first_name" required placeholder="First name"><input name="last_name" required placeholder="Last name"><input type="email" name="email" required placeholder="Email"><input name="site" placeholder="Restaurant/site"><select name="role"><option value="staff">Staff</option><option value="manager">Manager</option><option value="admin">Admin</option></select><select name="brand"><option>Chuck & Blade</option><option>Bare Bones</option><option>Faithorn & Stag</option></select><select name="department"><option>FOH</option><option>BOH</option><option>Both</option><option>Management</option></select><input name="position" placeholder="Position, e.g. BOH Supervisor"></div><h3>Assign learning paths</h3><?php foreach($paths as $p): ?><label><input style="width:auto" type="checkbox" name="paths[]" value="<?= $p['id'] ?>"> <?= htmlspecialchars($p['brand'].' • '.$p['name']) ?></label><br><?php endforeach; ?><button>Add and invite</button></form></div>
<div class="card"><h2>Users</h2><table><tr><th>Name</th><th>Email</th><th>Role</th><th>Brand</th><th>Dept</th><th>Status</th></tr><?php foreach($users as $u): ?><tr><td><?= htmlspecialchars($u['first_name'].' '.$u['last_name']) ?></td><td><?= htmlspecialchars($u['email']) ?></td><td><?= $u['role'] ?></td><td><?= htmlspecialchars($u['brand']) ?></td><td><?= htmlspecialchars($u['department']) ?></td><td><?= $u['status'] ?></td></tr><?php endforeach; ?></table></div>
<?php require __DIR__.'/../includes/footer.php'; ?>
