<?php
require_once __DIR__ . '/../../models/Database.php';
use Models\Database;

$pdo = Database::getInstance()->getConnection();

$id = $_GET['id'] ?? null;

if (!$id) {
    echo "ID manquant.";
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $pdo->prepare("
        UPDATE employees SET
            first_name = ?, last_name = ?, contract_start = ?, contract_end = ?, matricule = ?,
            date_entree = ?, taux_impot = ?, indice = ?, date_sortie = ?, date_anciennete = ?,
            statut_fiscal = ?, profession = ?, gross_salary = ?, heures_semaine = ?, heures_mois = ?,
            iban = ?, bic_code = ?, bank = ?, status = ?
        WHERE id = ?
    ");

    $stmt->execute([
        $_POST['first_name'], $_POST['last_name'], $_POST['contract_start'], $_POST['contract_end'] ?: null,
        $_POST['matricule'], $_POST['date_entree'], $_POST['taux_impot'], $_POST['indice'] ?: null,
        $_POST['date_sortie'] ?: null, $_POST['date_anciennete'] ?: null, $_POST['statut_fiscal'] ?: null,
        $_POST['profession'] ?: null, $_POST['gross_salary'], $_POST['heures_semaine'] ?: null,
        $_POST['heures_mois'] ?: null, $_POST['iban'] ?: null, $_POST['bic_code'] ?: null,
        $_POST['bank'] ?: null, $_POST['status'] ?? 'actif', $id
    ]);

    header("Location: /employees");
    exit;
} else {
    echo "Méthode non autorisée.";
}
