<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .loader {
        position: fixed;
        z-index: 10;
        width: 100vw;
        height: 100vh;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        font-size: 2rem;
    }

    .lds-roller {
        display: inline-block;
        position: relative;
        width: 80px;
        height: 80px;
    }

    .lds-roller div {
        animation: lds-roller 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
        transform-origin: 40px 40px;
    }

    .lds-roller div:after {
        content: " ";
        display: block;
        position: absolute;
        width: 7px;
        height: 7px;
        border-radius: 50%;
        background: #000;
        margin: -4px 0 0 -4px;
    }

    .lds-roller div:nth-child(1) {
        animation-delay: -0.036s;
    }

    .lds-roller div:nth-child(1):after {
        top: 63px;
        left: 63px;
    }

    .lds-roller div:nth-child(2) {
        animation-delay: -0.072s;
    }

    .lds-roller div:nth-child(2):after {
        top: 68px;
        left: 56px;
    }

    .lds-roller div:nth-child(3) {
        animation-delay: -0.108s;
    }

    .lds-roller div:nth-child(3):after {
        top: 71px;
        left: 48px;
    }

    .lds-roller div:nth-child(4) {
        animation-delay: -0.144s;
    }

    .lds-roller div:nth-child(4):after {
        top: 72px;
        left: 40px;
    }

    .lds-roller div:nth-child(5) {
        animation-delay: -0.18s;
    }

    .lds-roller div:nth-child(5):after {
        top: 71px;
        left: 32px;
    }

    .lds-roller div:nth-child(6) {
        animation-delay: -0.216s;
    }

    .lds-roller div:nth-child(6):after {
        top: 68px;
        left: 24px;
    }

    .lds-roller div:nth-child(7) {
        animation-delay: -0.252s;
    }

    .lds-roller div:nth-child(7):after {
        top: 63px;
        left: 17px;
    }

    .lds-roller div:nth-child(8) {
        animation-delay: -0.288s;
    }

    .lds-roller div:nth-child(8):after {
        top: 56px;
        left: 12px;
    }

    @keyframes lds-roller {
        0% {
            transform: rotate(0deg);
        }

        100% {
            transform: rotate(360deg);
        }
    }
</style>

<body>


    <div class="loader">
        <div class="lds-roller">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
        <h2>Loading</h2>
        <p>Please Don't Close the Browser</p>
    </div>

    <?php

    // Mail
    require "./vendor/autoload.php";

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    // Connection Variable
    $servername = "localhost";
    $username = "gainsk5_root";
    $password = "SkillsPass@123";
    $dbname = "gainsk5_messages_backup";

    // Date Stamp Variable
    date_default_timezone_set('Asia/Kolkata');
    $time = date('Y-m-d H:i:s');

    // Condition Submitting
    if (isset($_POST['contact-btn'])) {

        // Form Data Variable
        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $officialEmail = $_POST['email'];
        $mobileNumber = $_POST['mobileNumber'];
        $companyName = $_POST['companyName'];
        $designation = $_POST['designation'];
        $city = $_POST['city'];
        $fullName = $firstName . " " . $lastName;

        // Mysql
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        // prepare and bind
        $stmt = $conn->prepare("INSERT INTO `contact_us` (First_Name, Last_Name, Official_Email, Mobile_Number, Company_Name, Designation, City, Date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssssss", $firstName, $lastName, $officialEmail, $mobileNumber, $companyName, $designation, $city, $time);
        $stmt->execute();

        // echo "New records created successfully";

        $stmt->close();
        $conn->close();

        $mailer = new PHPMailer(true);

        try {
            $mailer->SMTPDebug = 0;
            $mailer->isSMTP();

            $mailer->SMTPOptions = [
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true,
                ]
            ];

            $mailer->Host = 'mail.gainskillsmedia.com';
            $mailer->SMTPAuth = true;
            $mailer->Username = 'info@gainskillsmedia.com';
            $mailer->Password = 'Info@122022';
            $mailer->SMTPSecure = "tls";
            $mailer->SMTPAutoTLS = false;
            $mailer->Port = 587;

            $mailer->setFrom('info@gainskillsmedia.com', 'Gainskills Business Media');
            // $mailer->addAddress('web@gainskillsmedia.com', 'Harsh Yadav');
            $mailer->addAddress('sumeetk@gainskillsmedia.com', 'Sumeet Khatri');
            $mailer->addCC('Adil@gainskillsmedia.com', 'Adil Z Najeeb');
            $mailer->addCC('aaliyah@gainskillsmedia.com', 'Aaliyah Khatri');

            $mailer->isHTML(true);
            $mailer->Subject = "Contact From Main Website Page";
            $mailer->Body = "
            <html>
            <head>
            <title>Contact From Main Website Page</title>
            </head>
            <body>
            <h2>Contact From Main Website Page</h2>
            <table style='width=100%'>
                <tr>
                <th style='text-align:start;padding:5px;'>Full Name</th>
                <td style='text-align:start;padding:5px;'>$fullName</td>
                </tr>
                <tr>
                <th style='text-align:start;padding:5px;'>Official Email</th>
                <td style='text-align:start;padding:5px;'>$officialEmail</td>
                </tr>
                <tr>
                <th style='text-align:start;padding:5px;'>Mobile Number</th>
                <td style='text-align:start;padding:5px;'>$mobileNumber</td>
                </tr>
                <tr>
                <th style='text-align:start;padding:5px;'>Company Name</th>
                <td style='text-align:start;padding:5px;'>$companyName</td>
                </tr>
                <tr>
                <th style='text-align:start;padding:5px;'>Designation</th>
                <td style='text-align:start;padding:5px;'>$designation</td>
                </tr>
                <tr>
                <th style='text-align:start;padding:5px;'>City</th>
                <td style='text-align:start;padding:5px;'>$city</td>
                </tr>
            </table>
            </body>
            </html>
            ";


            if (!($mailer->send())) {
                $mailer->ClearAllRecipients();
                ?><script>window.location.href = '../fail.html';</script><?php
            } else {
                $mailer->ClearAllRecipients();
                ?><script>window.location.href = `<?php echo "./contact-confirmation.php?officialEmail=$officialEmail&fullName=$fullName"; ?>`;</script><?php
            }
        } catch (Exception $e) {
            echo "EMAIL SENDING FAILED. INFO: " . $mailer->ErrorInfo;
        }
    }
    else{
        ?><script>window.location.href = `../fail.html`;</script><?php
    }
?>

</body>

</html>