<?php

include "db.class.php";
include "email.class.php";


$db = new Database();
$email_class = new email();



function postEncryptedRequest($url, $postData)
{
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);

    // Enable SSL verification 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

    $response = curl_exec($curl);
    curl_close($curl);

    // Process the response
    $statusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
    if ($statusCode == 200) {
        $responseData = $response;
        // Perform further processing of the encrypted string
        return $responseData;
    } else {
        throw new Exception("Failed to complete request: {$statusCode}");
    }
}

$postData = 'client_code=LVNT';

try {
    $encryptionKey = postEncryptedRequest('https://encryption.elegantwork.co.za', $postData);
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}
@$name = openssl_encrypt($_POST['name'], 'aes-256-cbc', $encryptionKey);
@$surname = openssl_encrypt($_POST['surname'], 'aes-256-cbc', $encryptionKey);
@$phone = openssl_encrypt($_POST['phone'], 'aes-256-cbc', $encryptionKey);
@$email = openssl_encrypt($_POST['email'], 'aes-256-cbc', $encryptionKey);
@$company = openssl_encrypt($_POST['company'], 'aes-256-cbc', $encryptionKey);

$first = substr($_POST['name'], 0, 2);
$second = substr($_POST['email'], 0, 2);
$third = substr($_POST['phone'], 4, 2);
$forth = substr($_POST['company'], 0, 2);

$otp = strtoupper($first . $second . $third . $forth . rand(9, 55));

if ($db->query("INSERT INTO `contact`(`name`, `surname`, `phone`, `email`,`company`,`otp`) VALUES ('$name','$surname','$phone','$email','$company','$otp')")) {
    $htmlString = '
    
    <style> 
        body {
            font-family: "Roboto Slab", serif;
            margin: 0;
            display: flex;
            padding: 0;
            background-color: none;
            color: #3C6E59;
            flex-direction: column;
            align-content: center;
            justify-content: center;
            align-items: center;
        }

        header {
            text-align: center;
            margin-bottom: 20px;
            padding: 20px;
            border-bottom: 1px solid #ccc;
            background-color: #f5f5f5;
        }

        section {
            max-width: 500px;
            margin: 0 auto;
            border: 5px solid #3C6E59;
            display: flex;
            padding: 20px;
            flex-direction: column;
            flex-wrap: nowrap;
            align-content: center;
            background-color: whitesmoke;
            justify-content: flex-start;
            align-items: center;
        }

        form {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        label {
            font-weight: bold;
            margin-bottom: 5px;
        }


        input {
            width: 100%;
            font-size: 1em;
            padding: 10px;
            border: 0px;
            border-bottom: 1px solid #87553c;
            /* border-radius: 5px; */
            box-sizing: border-box;
            background-color: #fff;
        }

        button {
            background-color: #87553c;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        /* Mobile styles */
        @media screen and (max-width: 768px) {
            section {
                padding: 10px;
            }

            form label {
                font-size: smaller;
            }

            input[type="date"],
            input[type="datetime-local"] {
                width: 100%;
                padding: 8px;
                margin-bottom: 10px;
            }
        }

        #loadingPopup {
            position: fixed;
            z-index: 100;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border: 3px solid #3C6E59;
            padding: 20px;
            border-radius: 8px;
            opacity: 0;
            transition: opacity 0.3s ease-in-out;
        }

        #loadingPopup.show {
            opacity: 1;
        }
    </style>

    <section>
        <h1>THANK YOU FOR YOUR RESERVATION, ' . $_POST['name'] . ' ' . $_POST['surname'] . '</h1>
        <h2 style="font-weight: 900;">Date of Event : <span style="color: #3C6E59">16</span> October <span style="color: #3C6E59">2025</span></h2>
        <h2>Your Information</h2>
        <h1>OTP: ' . $otp . '</h1>
            <div class="form-group">
                <label for="company">Company Name:</label>
                <span style="color: #3C6E59"><input type="text" id="company" name="company" required value="' . $_POST['company'] . '" /></span>
            </div>
            <div class="form-group">
                <label for="phone">Phone Number:</label>
                <span style="color: #3C6E59"><input type="tel" id="phone" name="phone" required value="' . $_POST['phone'] . '" /></span>
            </div>
            <div class="form-group">
                <label for="email">Email Address:</label>
                <span style="color: #3C6E59"><input type="email" id="email" name="email" value="' . $_POST['email'] . '" /></span>
            </div>
            <h2>CONTACT US:</h2>
            <span style="color: #3C6E59"><h3>Lowveld Nuts (Pty) Ltd.</h3></span>
            <h3>Lowveld Nuts (Pty) Ltd, White River, Brondal, 1240</h3>
            <span style="color: #3C6E59"><h3>info@lowveldnuts.co.za</h3></span>
            <h3>013 762 3103/4</h3>
           <span style="color: #3C6E59"> <h3>064 743 0348</h3></span>

           <p>We look forward to see you there!</p>
           <p>Hosted and Developed by Elegant Work Group (Pty) Ltd.</p>
        </section>
    ';
    if ($email_class->send_mail([$_POST['email']], [$_POST['name']], $htmlString, "LOWVELD NUTS RESERVATION")) {
        echo "success";
    } else {
        echo "failed to send email";
    }

} else {
    echo "ERROR";
}