
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
// DB configuration
$host = 'localhost';
$dbname = 'balubaid_nissan_test_drive';
$user = 'balubaid_nissan_test_drive';
$pass = 'Vision@2050';

date_default_timezone_set('Asia/Riyadh');
$start_date = date('Y-m-d H:i:s'); // Time in Riyadh

// Connect to DB
$conn = new mysqli($host, $user, $pass, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Sanitize form inputs
$first_name = $conn->real_escape_string($_POST['first-name']);
$last_name = $conn->real_escape_string($_POST['last-name']);
$mobile = $conn->real_escape_string($_POST['mobile']);
$email = $conn->real_escape_string($_POST['email']);
$gender = $conn->real_escape_string($_POST['gender']);
$vehicle = $conn->real_escape_string($_POST['vehicle']);
$natid = $conn->real_escape_string($_POST['natid']);
$status = 'Registered';
$agegroup = $conn->real_escape_string($_POST['agegroup']);

// Check for duplicate natid
$check_stmt = $conn->prepare("SELECT id FROM nissan_registrations WHERE mobile = ? and vehicle = ?");
$check_stmt->bind_param("ss", $mobile,$vehicle);
$check_stmt->execute();
$check_stmt->store_result();

if ($check_stmt->num_rows > 0) {
    echo "<script>alert('You have already registered using with the same mobile and vehicle.'); window.history.back();</script>";
    $check_stmt->close();
    $conn->close();
    exit;
}
$check_stmt->close();

// Handle license file upload
$upload_dir = 'uploads/';
if (!is_dir($upload_dir)) {
    mkdir($upload_dir, 0755, true);
}

$license_file = $_FILES['license'];
$extension = strtolower(pathinfo($license_file['name'], PATHINFO_EXTENSION));
$unique_filename = 'license_' . time() . '_' . uniqid() . '.jpg'; // Save all as JPEG
$upload_path = $upload_dir . $unique_filename;

// Compress and move file
list($width, $height) = getimagesize($license_file['tmp_name']);
$source_image = null;

// Load image based on MIME type
switch ($extension) {
    case 'jpeg':
    case 'jpg':
        $source_image = imagecreatefromjpeg($license_file['tmp_name']);
        break;
    case 'png':
        $source_image = imagecreatefrompng($license_file['tmp_name']);
        break;
    case 'gif':
        $source_image = imagecreatefromgif($license_file['tmp_name']);
        break;
    default:
        echo "Unsupported image format.";
        exit;
}

// Resize (optional, or use original size)
$max_width = 800; // reduce size if needed
if ($width > $max_width) {
    $new_width = $max_width;
    $new_height = intval(($height / $width) * $new_width);
    $resized_image = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($resized_image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
} else {
    $resized_image = $source_image;
}

// Compress and save as JPEG (quality = 60)
if (imagejpeg($resized_image, $upload_path, 60)) {
    // Insert into database
    $stmt = $conn->prepare("INSERT INTO nissan_registrations (first_name, last_name, natid, mobile, email, gender, vehicle, license_path, status, agegroup,submitted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssssssss", $first_name, $last_name, $natid, $mobile, $email, $gender, $vehicle, $upload_path, $status,$agegroup, $start_date);

    if ($stmt->execute()) {
        echo "<script>alert('Thank you for registering. Your submission was successful.'); window.location.href = 'thanks.php';</script>";
    } else {
        echo "Error inserting data: " . $stmt->error;
    }

    $stmt->close();
} else {
    echo "Failed to compress and save the license image.";
}

if ($source_image) imagedestroy($source_image);
if ($resized_image) imagedestroy($resized_image);

$conn->close();
?>