<!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">
    <meta name="description" content="Conferences & Awards - GainSkills Business Media Pvt Ltd" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    <?php require '../../inc/header.php'; ?>
</head>
<body>
<?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['carrier-submit'])){
        
        // Form Data Variable
        $firstName = $_POST['first-name'];
        $lastName = $_POST['last-name'];
        $phone = $_POST['phone'];
        $officialEmail = $_POST['email'];
        $joinUsAs = $_POST['join-us-as'];
        $fileName = $_FILES["file"]["name"];
        $fileSize = $_FILES["file"]["size"];
        $fileTmp = $_FILES["file"]["tmp_name"];
        $newFileName = $officialEmail . '-' . $time . "-" . $fileName;
        $fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
        $FileName = str_replace(' ', '', $newFileName);
        $uploads_dir = './resumes/' . basename($FileName);
        $fileLink = 'https://gainskillsmedia.com/pages/mail/resumes/'.$FileName;
        
        if($fileType == 'pdf' || $fileType == 'doc' || $fileType == 'docx' || $fileType == 'docs'){
            
            if($fileTpe > 5000){
                echo "<span style='color: #dc3545;font-size: 24px;'>Error: File size should be under 5MB</span>";
                return false;
            }
            if(move_uploaded_file($fileTmp, $uploads_dir)){
                // 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 `resume` (First_Name, Last_Name, Phone, Official_Email, Join_As, Link, Date) VALUES (?, ?, ?, ?, ?, ?, ?)");
                $stmt->bind_param("sssssss", $firstName, $lastName, $phone, $officialEmail, $joinUsAs, $fileLink, $time);
                $stmt->execute();
                $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('hr@gainskillsmedia.com', 'Jinita Rawal');
                    // $mailer->addCC('sumeetk@gainskillsmedia.com', 'Sumeet Khatri');
                
                    $mailer->isHTML(true);
                    $mailer->Subject = 'New Candidate Form Submission';
                    $mailer->Body = "
                    <html>
                    <head>
                      <title>New Resume Submission</title>
                    </head>
                    <body>
                      <h2>New Resume Submission</h2>
                      <table style='width=100%'>
                        <tr>
                          <th style='text-align:start;padding:5px;'>Full Name</th>
                          <td style='text-align:start;padding:5px;'>$firstName $lastName</td>
                        </tr>
                        <tr>
                          <th style='text-align:start;padding:5px;'>Phone Number</th>
                          <td style='text-align:start;padding:5px;'>$phone</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;'>Applied For</th>
                          <td style='text-align:start;padding:5px;'>$joinUsAs</td>
                        </tr>
                        <tr>
                          <th style='text-align:start;padding:5px;'>Resume Link</th>
                          <td style='text-align:start;padding:5px;'><a href='$fileLink' target='_blank' download>$fileLink</a></td>
                        </tr>
                      </table>
                    </body>
                    </html>
                    ";
                    
                    if(!($mailer->send())){
                        $mailer->ClearAllRecipients();
                        echo "<span style='color: #dc3545;font-size: 24px;'>Error: Something Went Wrong</span>";
                    }
                    else{    
                        $mailer->ClearAllRecipients();
                        echo "<span style='color: #198754;font-size: 24px;'>Form Subitted, We Will Contact You Soon..</span>";
                    }
            
                } catch (Exception $e) {
                    echo "Error: " . $mailer->ErrorInfo;
                }
            }
        }
        else{
            echo "<span style='color: #dc3545;font-size: 24px;'>File should be in pdf or docs format</span>";
            return false;
        }
    }
?>
</body>

</html>