<?php

require_once '../config.php';
// require_once ('plugins/swiftmailer/vendor/autoload.php');

if(isset($_POST['submit'])){

    // $transport = (new Swift_SmtpTransport(EMAIL_HOST, EMAIL_PORT, EMAIL_ENCRIPTION))
    // ->setUsername(EMAIL_USER)
    // ->setPassword(EMAIL_PASS)
    // ;
    // $mailer = new Swift_Mailer($transport);

    // Message content
    $customer_info='<html><body>
    <table style="border-collapse: collapse; width: 50%;">
    <tr>
    <td colspan="2" style="color: #fff; background-color: #151823; text-align: center; font-size: 16px; padding: 5px 0;">Customer Details</td>
    </tr>
    ';
    
    if(isset($_POST['company'])){
    	$customer_info.='
    	<tr>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><strong>Company Name</strong></td>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><p>'.$_POST['company'].'</p></td>
    	</tr>';
    }
    if(isset($_POST['name'])){
    	$customer_info.='
    	<tr>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><strong>Customer Name</strong></td>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><p>'.$_POST['name'].'</p></td>
    	</tr>';
    }
    if(isset($_POST['email'])){
    	$customer_info.='
    	<tr>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><strong>Email Address</strong></td>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><p>'.$_POST['email'].'</p></td>
    	</tr>';
    }
    if(isset($_POST['tel'])){
    	$customer_info.='
    	<tr>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><strong>Telephone Number</strong></td>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><p>'.$_POST['tel'].'</p></td>
    	</tr>';
    }
    if(isset($_POST['sub'])){
    	$customer_info.='
    	<tr>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><strong>Subject</strong></td>
    	<td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><p>'.$_POST['sub'].'</p></td>
    	</tr>';
    }
    if(isset($_POST['msg'])){
        $customer_info.='
        <tr>
        <td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><strong>Customer Message</strong></td>
        <td style="border: 1px solid #f35444; color: #151823; font-size: 12px; padding-left: 5px;"><p>'.$_POST['msg'].'</p></td>
        </tr>';
    }
    
    $customer_info.='
    </table>
    </body>
    </html>
    ';

    // Create a message
    $message = new Swift_Message();
    
    $message->setSubject('Customer Details')
    ->setFrom([EMAIL_FROM])
    ->setTo([EMAIL_TO])
    ->setBody($customer_info,'text/html');    
    
    
    // Send the message
    try {
        $result = $mailer->send($message);
    }
    catch (Swift_TransportException $e) {
        echo $e->getMessage();
    }
    
    $bites = random_bytes(5);
    $notify = bin2hex($bites);
    
    if ($result) {

        header("Location:../index.php?notify=".$notify);

    }else{

        header("Location:../index.php?error=".$notify);

    }
}