<?php

require_once '../../core/api_bootstrap.php';
require_once '../../core/employee_auth.php';

EmployeeAuth::check('manage_employees');

$data = Request::post();

try {

Validator::required($data,[
'id',
'name',
'email',
'role_id'
]);

$db = new Database();
$conn = $db->connect();

$stmt = $conn->prepare("

UPDATE employees
SET
name=:name,
email=:email,
phone=:phone,
role_id=:role,
status=:status
WHERE id=:id

");

$stmt->execute([

':id'=>$data['id'],
':name'=>$data['name'],
':email'=>$data['email'],
':phone'=>$data['phone'] ?? null,
':role'=>$data['role_id'],
':status'=>$data['status'] ?? 'active'

]);

Response::success([],"Employee updated");

}
catch(Exception $e){

Logger::log("system.log",$e->getMessage());

Response::error("Server error",500);

}
