<?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();
}

$res = $db->query("SELECT * FROM contact WHERE otp = '" . strtoupper($_POST['otp']) . "' AND date_time_attended IS NULL");
if ($res = $res->fetchAll()) {
    if ($res) {
        $db->query("UPDATE contact SET date_time_attended = NOW() WHERE otp = '{$_POST['otp']}'");
        $name = openssl_decrypt($res[0][1], 'aes-256-cbc', $encryptionKey);
        $email = openssl_decrypt($res[0][4], 'aes-256-cbc', $encryptionKey);
        $htmlString = '
        <style> 
            body {
                font-family: "Monsterrat", sans-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 #87553c;
                padding: 20px;
                border-radius: 8px;
                opacity: 0;
                transition: opacity 0.3s ease-in-out;
            }
    
            #loadingPopup.show {
                opacity: 1;
            }
        </style>
    
        <section style="text-align: center">
            <h1>THANK YOU,<span style="color: #3C6E59">' . $name . '</span> for attending to our event!</h1>
                <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([$email], [$name], $htmlString, "LOWVELD NUTS (Pty) Ltd")) {
            echo "success";
        } else {
            echo "failed to send email";
        }
    }
} else {
    echo "OTP NOT FOUND OR ALREADY IN ATTENDANCE";
}