<?php
header("Content-Type: application/json");

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

require 'vendor/autoload.php';

// Required fields
$required = ["name", "phone", "email", "business", "industry", "nominationType", "description"];
foreach ($required as $field) {
    if (!isset($_POST[$field]) || empty(trim($_POST[$field]))) {
        echo json_encode(["status" => "error", "message" => "All fields are required."]);
        exit;
    }
}

// Sanitize input
$name = htmlspecialchars($_POST['name']);
$phone = htmlspecialchars($_POST['phone']);
$email = htmlspecialchars($_POST['email']);
$business = htmlspecialchars($_POST['business']);
$industry = htmlspecialchars($_POST['industry']);
$nominationType = htmlspecialchars($_POST['nominationType']);
$description = nl2br(htmlspecialchars($_POST['description']));

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host = 'newdedi.clouddreams.in';
    $mail->SMTPAuth = true;
    $mail->Username = 'tester@clouddreams.in';
    $mail->Password = 'OUHbP$W2$';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->setFrom("tester@clouddreams.in", "Ilampirai Awards Submission");
    $mail->addAddress("gokulnath@clouddreams.in", "Admin");

    // Attach PDF if uploaded
    if (!empty($_FILES["pdf"]["name"])) {
        $mail->addAttachment($_FILES["pdf"]["tmp_name"], $_FILES["pdf"]["name"]);
    }

    $mail->isHTML(true);
    $mail->Subject = "New Nomination - Ilampirai Awards";

 $mail->Body = "
<div style='font-family: Arial, sans-serif; background:#f4f4f4; padding:20px;'>
    <div style='max-width:600px; margin:auto; background:#ffffff; border-radius:12px; padding:30px; box-shadow:0 4px 20px rgba(0,0,0,0.1);'>

        <!-- Header -->
        <div style='text-align:center; margin-bottom:25px;'>
            <h1 style='font-size:24px; color:#002557; margin-bottom:5px;'>Ilampirai Awards Nomination</h1>
            <p style='font-size:14px; color:#666;'>Nomination details submitted by participant</p>
        </div>

        <!-- Participant Details Table -->
        <table style='width:100%; border-collapse:collapse;'>
            <tr>
                <td style='padding:10px; font-weight:bold; color:#002557; width:40%;'>Full Name:</td>
                <td style='padding:10px; color:#333;'>$name</td>
            </tr>
            <tr style='background:#f9f9f9;'>
                <td style='padding:10px; font-weight:bold; color:#002557;'>Phone:</td>
                <td style='padding:10px; color:#333;'>$phone</td>
            </tr>
            <tr>
                <td style='padding:10px; font-weight:bold; color:#002557;'>Email:</td>
                <td style='padding:10px; color:#333;'>$email</td>
            </tr>
            <tr style='background:#f9f9f9;'>
                <td style='padding:10px; font-weight:bold; color:#002557;'>Business Name:</td>
                <td style='padding:10px; color:#333;'>$business</td>
            </tr>
            <tr>
                <td style='padding:10px; font-weight:bold; color:#002557;'>Industry:</td>
                <td style='padding:10px; color:#333;'>$industry</td>
            </tr>
            <tr style='background:#f9f9f9;'>
                <td style='padding:10px; font-weight:bold; color:#002557;'>Nomination Type:</td>
                <td style='padding:10px; color:#333;'>$nominationType</td>
            </tr>
            <tr>
                <td style='padding:10px; font-weight:bold; color:#002557; vertical-align:top;'>Achievements / Description:</td>
                <td style='padding:10px; color:#333;'>$description</td>
            </tr>
        </table>

        <!-- Footer / Note -->
        <div style='margin-top:30px; text-align:center;'>
            <p style='font-size:12px; color:#888; line-height:1.5;'>
                This message was automatically generated from the Ilampirai Awards nomination form.<br>
                Please do not reply to this email.
            </p>
        </div>

        <!-- Gradient Divider -->
        <div style='height:4px; background: linear-gradient(90deg, #002557, #0059b3); border-radius:2px; margin-top:25px;'></div>
        
    </div>
</div>
";


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

    $mail->send();
    echo json_encode(["status" => "success", "message" => "Nomination submitted successfully!"]);
} catch (Exception $e) {
    echo json_encode(["status" => "error", "message" => "Mailer Error: " . $mail->ErrorInfo]);
}
