<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/config.php';require_once __DIR__ . '/../includes/db.php';require_once __DIR__ . '/../includes/functions.php';require_once __DIR__ . '/../includes/admin-auth.php';
$pdo=db();requireAdmin();$admin=currentAdmin($pdo);$id=(int)($_GET['id']??$_POST['id']??0);$student=$id>0?safeFetch($pdo,'SELECT * FROM students WHERE id=? LIMIT 1',[$id]):null;if(!$student){http_response_code(404);die('Student not found.');}$error='';
if(isPost()){requireCsrf();$name=trim((string)($_POST['full_name']??''));$code=trim((string)($_POST['student_code']??''));$card=trim((string)($_POST['student_card_number']??''));$email=trim((string)($_POST['email']??''));$phone=trim((string)($_POST['phone']??''));$country=trim((string)($_POST['country']??''));$city=trim((string)($_POST['city']??''));$gender=(string)($_POST['gender']??'male');$level=(string)($_POST['final_level']??'beginner');$status=(string)($_POST['status']??'pending');$coreStatus=(string)($_POST['core_status']??'not_enrolled');
if($name===''||$code===''||$card===''||$phone==='')$error='Name, access codes and phone are required.';elseif($email!==''&&!filter_var($email,FILTER_VALIDATE_EMAIL))$error='Enter a valid email.';elseif(!in_array($gender,['male','female'],true)||!in_array($level,['beginner','intermediate','advanced'],true)||!in_array($status,['pending','approved','rejected'],true)||!in_array($coreStatus,['not_enrolled','active','completed','suspended'],true))$error='One or more selected values are invalid.';else{try{$stmt=$pdo->prepare('UPDATE students SET full_name=?,student_code=?,student_card=?,student_card_number=?,email=?,phone=?,whatsapp=?,whatsapp_number=?,country=?,city=?,nationality=?,gender=?,selected_level=?,final_level=?,status=?,core_status=?,updated_at=NOW() WHERE id=?');$stmt->execute([$name,$code,$card,$card,$email?:null,$phone,$phone,$phone,$country?:null,$city?:null,$country?:null,$gender,$level,$level,$status,$coreStatus,$id]);logActivity($pdo,'admin',(int)($admin['id']??0),'Student updated','Updated student ID '.$id);flash('success','Student updated successfully.');redirect('admin/students.php');}catch(Throwable $e){error_log('[SMI edit student] '.$e->getMessage());$error=str_contains(strtolower($e->getMessage()),'duplicate')?'The code, card or phone belongs to another student.':'Student could not be updated.';}}
}
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Edit Student · SMI</title><style>:root{font-family:Inter,Arial,sans-serif;background:#f3f6fb;color:#0f172a}*{box-sizing:border-box}body{margin:0}.page{max-width:900px;margin:auto;padding:32px}.top{display:flex;justify-content:space-between;align-items:center}.top h1{font-size:44px;margin:0}.card{margin-top:24px;background:white;padding:28px;border-radius:28px;box-shadow:0 18px 50px rgba(15,23,42,.08)}.grid{display:grid;grid-template-columns:1fr 1fr;gap:14px}label{font-weight:800;font-size:13px}input,select{width:100%;padding:12px;border:1px solid #dbe3ef;border-radius:12px;margin-top:6px;font:inherit}.wide{grid-column:1/-1}.btn{border:0;background:#2563eb;color:white;padding:12px 17px;border-radius:13px;font-weight:850;text-decoration:none;cursor:pointer}.muted{background:#e2e8f0;color:#0f172a}.alert{background:#fee2e2;color:#991b1b;padding:14px;border-radius:13px;margin-top:18px}@media(max-width:650px){.grid{grid-template-columns:1fr}.page{padding:18px}.top h1{font-size:34px}}</style></head><body><main class="page"><div class="top"><div><h1>Edit student</h1><p><?=e($student['full_name'])?></p></div><a class="btn muted" href="students.php">Back</a></div><?php if($error):?><div class="alert"><?=e($error)?></div><?php endif;?><form class="card grid" method="post"><?=csrfField()?><input type="hidden" name="id" value="<?=$id?>"><label class="wide">Full name<input name="full_name" value="<?=e($student['full_name'])?>" required></label><label>Student code<input name="student_code" value="<?=e($student['student_code'])?>" required></label><label>Student card<input name="student_card_number" value="<?=e($student['student_card_number']?:$student['student_card'])?>" required></label><label>Email<input type="email" name="email" value="<?=e($student['email'])?>"></label><label>Phone / WhatsApp<input name="phone" value="<?=e($student['phone']?:$student['whatsapp'])?>" required></label><label>Country<input name="country" value="<?=e($student['country']?:$student['nationality'])?>"></label><label>City<input name="city" value="<?=e($student['city'])?>"></label><label>Gender<select name="gender"><option value="male" <?=selected($student['gender'],'male')?>>Male</option><option value="female" <?=selected($student['gender'],'female')?>>Female</option></select></label><label>Level<select name="final_level"><?php foreach(['beginner','intermediate','advanced'] as $v):?><option value="<?=$v?>" <?=selected($student['final_level']?:$student['selected_level'],$v)?>><?=ucfirst($v)?></option><?php endforeach;?></select></label><label>Account status<select name="status"><?php foreach(['pending','approved','rejected'] as $v):?><option value="<?=$v?>" <?=selected($student['status'],$v)?>><?=ucfirst($v)?></option><?php endforeach;?></select></label><label>Core status<select name="core_status"><?php foreach(['not_enrolled','active','completed','suspended'] as $v):?><option value="<?=$v?>" <?=selected($student['core_status'],$v)?>><?=ucwords(str_replace('_',' ',$v))?></option><?php endforeach;?></select></label><div class="wide"><button class="btn">Save changes</button></div></form></main></body></html>
