

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

// $driverID = filterRequest("driver_id");
// $imageName = filterRequest("image_name");
// $link = filterRequest("link");

// // Check if the driverID exists in the table
// $checkSQL = "SELECT * FROM `card_images` WHERE `driver_id` = '$driverID'";
// $checkStmt = $con->prepare($checkSQL);
// $checkStmt->execute();

// if ($checkStmt->rowCount() > 0) {
//     // Driver ID found, update the upload_date
//     $uploadDate = date("Y-m-d H:i:s");
    
//     $updateSQL = "UPDATE `card_images` SET `upload_date` = '$uploadDate' WHERE `driver_id` = '$driverID'";
//     $updateStmt = $con->prepare($updateSQL);
//     $updateStmt->execute();
    
//     if ($updateStmt->rowCount() > 0) {
//         // Print a success message for update
//         jsonSuccess($message = "Record updated successfully");
//     } else {
//         // Print a failure message for update
//         jsonError($message = "Failed to update record");
//     }
// } else {
//     // Driver ID not found, insert a new record
//     $sql = "INSERT INTO `card_images` (`id`, `driver_id`, `image_name`, `link`) 
//             VALUES (SHA2(UUID(), 256), '$driverID', '$imageName', '$link')";
    
//     $stmt = $con->prepare($sql);
//     $stmt->execute();
    
//     if ($stmt->rowCount() > 0) {
//         // Print a success message for insert
//         jsonSuccess($message = "Record inserted successfully");
//     } else {
//         // Print a failure message for insert
//         jsonError($message = "Failed to insert record");
//     }
// }





<?php

require_once __DIR__ . '/connect.php';

// Get the image file from the request.
$image_file = $_FILES['image'];
$driverID = filterRequest("driver_id");

// Check if the image file was uploaded successfully.
if ($image_file['error'] != UPLOAD_ERR_OK) {
  echo json_encode(array('status' => 'The image file was not uploaded successfully.'));
  exit;
}

// Get the file name of the image file.
$image_name = $image_file['name'];

// Get the file extension of the image file.
$image_extension = pathinfo($image_name, PATHINFO_EXTENSION);

// Check if the image file is a valid image file.
if (!in_array($image_extension, array('jpg', 'jpeg', 'png'))) {
  echo json_encode(array('status' => 'The image file is not a valid image file.'));
  exit;
}

// Generate a new filename using the driver ID.
$new_filename = $driverID . '.' . $image_extension;

// Move the image file to the uploads directory with the new filename.
$target_dir = "card_image/";
$target_file = $target_dir . $new_filename;
move_uploaded_file($image_file['tmp_name'], $target_file);

// Update the image name variable with the new filename.
$image_name = $new_filename;

// Check if the driverID already exists in the database.
$sql = "SELECT * FROM card_images WHERE driver_id = '$driverID'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
  // The driverID already exists in the database, so update the upload_date
  $uploadDate = date("Y-m-d H:i:s");
  $linlImage='https://ride.mobile-app.store/card_image/'.$image_name;
  $updateSQL = "UPDATE card_images SET upload_date = '$uploadDate' WHERE driver_id = '$driverID'";
  mysqli_query($conn, $updateSQL);
  
  if (mysqli_affected_rows($conn) > 0) {
    // Print a success message for update
    echo json_encode(array('status' => 'Record updated successfully'));
  } else {
    // Print a failure message for update
    echo json_encode(array('status' => 'Failed to update record'));
  }
} else {
  // The driverID does not exist in the database, so insert a new row.
  $insertSQL = "INSERT INTO card_images (id, driver_id, image_name, `link`) 
                VALUES (SHA2(UUID(), 256), '$driverID', '$image_name',)";
  mysqli_query($conn, $insertSQL);
  
  if (mysqli_affected_rows($conn) > 0) {
    // Print a success message for insert
    echo json_encode(array('status' => 'Record inserted successfully'));
  } else {
    // Print a failure message for insert
    echo json_encode(array('status' => 'Failed to insert record'));
  }
}

?>