<?php
$isEdit=is_array($patient); $form=$form??[];
$value=static function(string $key,string $default='') use($form,$patient):string { if(array_key_exists($key,$form)) return (string)$form[$key]; return is_array($patient)?(string)($patient[$key]??$default):$default; };
$e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');
?>
<section class="page-header"><div><p class="eyebrow">Clinical / Patients</p><h1><?=$isEdit?'Edit Patient':'Add Patient'?></h1><p><?=$isEdit?'Update the patient demographic record.':'Create a new patient record and generate a unique reference.'?></p></div><a class="btn btn-secondary" href="/crm/public/patients">Back to Patients</a></section>
<?php if(!empty($error)): ?><div class="alert alert-danger"><?=$e($error)?></div><?php endif; ?>
<?php if(!empty($message)): ?><div class="alert alert-success"><?=$e($message)?></div><?php endif; ?>
<section class="card form-card section-gap"><form method="post" action="<?=$isEdit?'/crm/public/patients/'.(int)$patient['id'].'/edit':'/crm/public/patients/create'?>" class="form-stack"><input type="hidden" name="_csrf" value="<?=$e($csrf)?>">
<?php if($isEdit): ?><div class="info-grid"><div><span>Patient reference</span><strong><?=$e($patient['patient_reference'])?></strong></div><div><span>Created</span><strong><?=$e($patient['created_at'])?></strong></div></div><?php endif; ?>
<div class="grid grid-2"><label class="form-field"><span>First name *</span><input required maxlength="100" name="first_name" value="<?=$e($value('first_name'))?>"></label><label class="form-field"><span>Last name *</span><input required maxlength="100" name="last_name" value="<?=$e($value('last_name'))?>"></label></div>
<div class="grid grid-2"><label class="form-field"><span>Date of birth</span><input type="date" name="date_of_birth" value="<?=$e($value('date_of_birth'))?>"></label><label class="form-field"><span>Gender</span><select name="gender"><option value="">Prefer not to say</option><?php foreach(['Female','Male','Other'] as $g): ?><option value="<?=$e($g)?>" <?=$value('gender')===$g?'selected':''?>><?=$e($g)?></option><?php endforeach; ?></select></label></div>
<div class="grid grid-2"><label class="form-field"><span>Phone</span><input maxlength="50" name="phone" value="<?=$e($value('phone'))?>"></label><label class="form-field"><span>Email</span><input type="email" maxlength="190" name="email" value="<?=$e($value('email'))?>"></label></div>
<label class="form-field"><span>Address</span><textarea name="address" rows="3" maxlength="5000"><?=$e($value('address'))?></textarea></label>
<div class="grid grid-2"><label class="form-field"><span>Parent / Guardian</span><input maxlength="190" name="parent_guardian" value="<?=$e($value('parent_guardian'))?>"></label><label class="form-field"><span>Emergency contact</span><input maxlength="190" name="emergency_contact" value="<?=$e($value('emergency_contact'))?>"></label></div>
<label class="form-field"><span>Status</span><select name="status"><option value="active" <?=$value('status','active')==='active'?'selected':''?>>Active</option><option value="inactive" <?=$value('status')==='inactive'?'selected':''?>>Inactive</option><option value="discharged" <?=$value('status')==='discharged'?'selected':''?>>Discharged</option></select></label>
<div class="form-actions"><a class="btn btn-secondary" href="/crm/public/patients">Cancel</a><button class="btn btn-primary" type="submit"><?=$isEdit?'Save Changes':'Create Patient'?></button></div>
</form></section>
