<?php

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/vendor/PHPMailer/src/Exception.php';

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/vendor/PHPMailer/src/PHPMailer.php';

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/vendor/PHPMailer/src/SMTP.php';

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/config/mail.php';

/* =========================================
   PAYMENT EMAIL
========================================= */

function sendPaymentEmail(
    $email,
    $name,
    $event,
    $amount,
    $trx
){

    try{

        /* =========================================
           SETUP MAIL
        ========================================= */

        $mail = setupMail();

        /* =========================================
           RECEIVER
        ========================================= */

        $mail->addAddress(
            $email,
            $name
        );

        /* =========================================
           SUBJECT
        ========================================= */

        $mail->Subject =
        "Payment Successful - ".
        $event['title'];

        /* =========================================
           BODY
        ========================================= */
$mail->Body = "

<div style='
    background:#f1f5f9;
    padding:40px 20px;
    font-family:Segoe UI,sans-serif;
'>

    <div style='
        max-width:720px;
        margin:auto;
        background:#ffffff;
        border-radius:24px;
        overflow:hidden;
        box-shadow:0 10px 35px rgba(15,23,42,0.08);
        border:1px solid #e2e8f0;
    '>

        <!-- TOP HEADER -->

        <div style='
            background:linear-gradient(
                135deg,
                #2563eb,
                #3b82f6
            );
            padding:38px;
            color:white;
        '>

            <div style='
                font-size:30px;
                font-weight:700;
                letter-spacing:-0.5px;
            '>

                Payment Successful

            </div>

            <div style='
                margin-top:10px;
                font-size:15px;
                opacity:0.92;
                line-height:1.7;
            '>

                Your payment has been confirmed successfully.

            </div>

        </div>

        <!-- MAIN CONTENT -->

        <div style='padding:35px;'>

            <!-- USER -->

            <div style='
                font-size:15px;
                color:#334155;
                line-height:1.9;
                margin-bottom:28px;
            '>

                Hello
                <b>{$name}</b>,

                <br><br>

                Thank you for your payment.
                Below is your payment receipt details.

            </div>

            <!-- CARD -->

            <div style='
                background:#f8fafc;
                border:1px solid #e2e8f0;
                border-radius:20px;
                padding:24px;
            '>

                <div style='
                    font-size:18px;
                    font-weight:700;
                    color:#0f172a;
                    margin-bottom:18px;
                '>

                    Payment Details

                </div>

                <table
                width='100%'
                cellpadding='0'
                cellspacing='0'
                style='
                    border-collapse:collapse;
                    font-size:14px;
                    color:#334155;
                '>

                    <tr>

                        <td style='
                            padding:14px 0;
                            color:#64748b;
                            width:220px;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            Event Name

                        </td>

                        <td style='
                            padding:14px 0;
                            font-weight:600;
                            color:#0f172a;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            {$event['title']}

                        </td>

                    </tr>

                    <tr>

                        <td style='
                            padding:14px 0;
                            color:#64748b;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            Amount Paid

                        </td>

                        <td style='
                            padding:14px 0;
                            font-weight:700;
                            color:#16a34a;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            RM ".number_format($amount,2)."

                        </td>

                    </tr>

                    <tr>

                        <td style='
                            padding:14px 0;
                            color:#64748b;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            Transaction ID

                        </td>

                        <td style='
                            padding:14px 0;
                            font-weight:600;
                            color:#0f172a;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            {$trx}

                        </td>

                    </tr>

                    <tr>

                        <td style='
                            padding:14px 0;
                            color:#64748b;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            Payment Timestamp

                        </td>

                        <td style='
                            padding:14px 0;
                            font-weight:600;
                            color:#0f172a;
                            border-bottom:1px solid #e2e8f0;
                        '>

                            ".date("d M Y h:i A")."

                        </td>

                    </tr>

                    <tr>

                        <td style='
                            padding:14px 0;
                            color:#64748b;
                        '>

                            Account Holder

                        </td>

                        <td style='
                            padding:14px 0;
                            font-weight:600;
                            color:#0f172a;
                        '>

                            {$name}

                        </td>

                    </tr>

                </table>

            </div>

            <!-- FOOTER -->

            <div style='
                margin-top:30px;
                padding-top:22px;
                border-top:1px solid #e2e8f0;
                font-size:13px;
                color:#64748b;
                line-height:1.8;
            '>

                Please keep this email as your official payment receipt.

                <br><br>

                Thank you for using
                <b>SEAT Event System</b>.

            </div>

        </div>

    </div>

</div>
";

  

        /* =========================================
           SEND
        ========================================= */

        $mail->send();

        return true;

    }catch(\Exception $e){

        error_log(
            "Payment Email Error : ".
            $e->getMessage()
        );

        return false;
    }
}
?>