<?php

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

$sql = "SELECT
    d.id,
    d.phone,
    d.email,
    d.gender,
    d.license_type,
    d.national_number,
    d.name_arabic,
    d.issue_date,
    d.expiry_date,
    d.license_categories,
    d.address,
    d.status,
    d.birthdate,
    d.site,
    d.first_name,
    d.last_name,
    d.accountBank,
    d.bankCode,
    d.employmentType,
    d.maritalStatus,
    d.fullNameMaritial,
    d.expirationDate,
    d.created_at,
    d.updated_at,
    wdc.id AS welcomId,
    wdc.isCall,
    wdc.notes,
    wdc.createdAt AS welcomeCreatedAt
FROM
    driver d
LEFT JOIN welcomeDriverCall wdc ON
    wdc.driverId = d.id
WHERE
	d.status='actives' and
    d.created_at BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 DAY) AND CURDATE() + INTERVAL 2 DAY
ORDER BY
    d.created_at DESC;


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

if ($stmt->rowCount() > 0) {
  // Fetch the records
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// فك التشفير للحقول الحساسة
foreach ($rows as &$row) {
    $row['phone']           = $encryptionHelper->decryptData($row['phone']);
    $row['email']           = $encryptionHelper->decryptData($row['email']);
    $row['gender']          = $encryptionHelper->decryptData($row['gender']);
    $row['birthdate']       = $encryptionHelper->decryptData($row['birthdate']);
  	$row['national_number'] = $encryptionHelper->decryptData($row['national_number']);
    $row['site']            = $encryptionHelper->decryptData($row['site']);
    $row['first_name']      = $encryptionHelper->decryptData($row['first_name']);
    $row['last_name']       = $encryptionHelper->decryptData($row['last_name']);
   // $row['employmentType']  = $encryptionHelper->decryptData($row['employmentType']);
   // $row['maritalStatus']   = $encryptionHelper->decryptData($row['maritalStatus']);
}

jsonSuccess($rows);
} else {
  // Print a failure message
  jsonError($message = "No Phone verified yet found");
}

?>