<?php require dirname(__DIR__).'/app/core.php';require_roles(['admin','standard']);csrf();$id=(int)($_POST['id']??0);$s=norm_serial($_POST['serial_number']??'');if(!valid_serial($s))exit('Invalid serial');$c=$_POST['asset_condition'];$d=$c==='new'?'For sale':($c==='scrap'?null:$_POST['designation']);$p=[$s,$_POST['owned_by'],$_POST['model'],$c,$d,$_POST['current_location'],user()['username'],$_POST['notes']];if($id){$old=$pdo->query('SELECT * FROM afguards WHERE id='.(int)$id)->fetch();$p[]=$id;$pdo->prepare('UPDATE afguards SET serial_number=?,owned_by=?,model=?,asset_condition=?,designation=?,current_location=?,record_updated_by=?,notes=? WHERE id=?')->execute($p);audit('asset','ASSET_UPDATED','success','afguard',$id,$s,$old,$_POST);}else{$pdo->prepare('INSERT INTO afguards(serial_number,owned_by,model,asset_condition,designation,current_location,record_updated_by,notes) VALUES(?,?,?,?,?,?,?,?)')->execute($p);$id=(int)$pdo->lastInsertId();audit('asset','ASSET_CREATED','success','afguard',$id,$s,null,$_POST);}queue_event('ASSET_CHANGED','AFGTrac sensor changed',$s,'afguard',$id);flash('Sensor saved.');go('assets.php');