<?php
require_once __DIR__ . '/../../connect.php';

$id = filterRequest("id");
$columnValues = [];

// الحقول التي تحتاج تشفير
$fieldsToEncrypt = [
  "phone", "email", "gender", "birthdate", "site",
  "first_name", "last_name", "accountBank", "education",
  "employmentType", "maritalStatus"
];

// الحقول غير المشفرة
$plainFields = ["status", "bankCode", "updated_at"];

foreach ($_POST as $key => $value) {
    $filtered = filterRequest($key);

    if ($key === "password") {
        // هاش لكلمة المرور
        $hashed = password_hash($filtered, PASSWORD_DEFAULT);
        $columnValues[] = "`password` = '$hashed'";
    } elseif (in_array($key, $fieldsToEncrypt)) {
        $encrypted = $encryptionHelper->encryptData($filtered);
        $columnValues[] = "`$key` = '$encrypted'";
    } elseif (in_array($key, $plainFields)) {
        $columnValues[] = "`$key` = '$filtered'";
    }
}

// بناء جملة التحديث
$setClause = implode(", ", $columnValues);
$sql = "UPDATE `driver` SET $setClause WHERE `id` = '$id'";

$stmt = $con->prepare($sql);
$stmt->execute();

if ($stmt->rowCount() > 0) {
    jsonSuccess(null, "Driver data updated successfully");
} else {
    jsonError("Failed to update driver data");
}
?>