<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Attachment;

use Illuminate\Support\Facades\File; 
use Illuminate\Support\Str;
use Carbon\Carbon;
use PDF;

class confirmationMail extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
        
    }

    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */

    public function build()
    {
 
        $nationality = Str::upper($this->data["nationality"]);
        $discount = Str::upper($this->data["discount"]);
        $discount_message = "";

        $age = Carbon::parse($this->data['birthdate'])->diff(Carbon::now())->format('%y');

        if($age < 15) {
            if ($nationality == "PHILIPPINES" && $discount == "Y") {
                $package='US Pedia - Less than 15 years old | PWD';
                $discount = "20%";
                $discount_message = "
                 <tr>
                    <td style='color:red;padding:0 10px 0 10px'>
                        <strong>Note!</strong>
                        <p style='font-size: 13px; font-style: italic'>You are eligible for the person with disability discount. Please bring your PWD ID to your scheduled medical exam and present it at the reception or to the cashier to verify your discount.
                    </td>
                </tr>";
            } else {
                $package="US Pedia - Less than 15 years old";
                $discount = "0%";
            }
            
        } elseif($age > 14) {
            if ($age > 59) {
                if ($nationality == "PHILIPPINES") {
                    $package = "US Adult - Senior Citizen";
                    $discount = "20%";
                    $discount_message = "
                    <tr>
                        <td style='color:red;padding:0 10px 0 10px'>
                            <strong>Note!</strong>
                            <p style='font-size: 13px; font-style: italic'>You are eligible for the senior citizen discount. Please bring your senior citizen ID to your scheduled medical exam and present it at the reception or to the cashier to verify your discount.</p>
                        </td>
                    </tr>";
                } else {
                    $package = "US Adult - Senior Citizen";
                    $discount = "0%";
                }
            } else {
                if ($nationality == "PHILIPPINES" && $discount == "Y") {
                    $package='US Adult - 15 years old and above | PWD';
                    $discount = "20%";
                    $discount_message = "
                     <tr>
                        <td style='color:red;padding:0 10px 0 10px'>
                            <strong>Note!</strong>
                            <p style='font-size: 13px; font-style: italic'>You are eligible for the person with disability discount. Please bring your PWD ID to your scheduled medical exam and present it at the reception or to the cashier to verify your discount.
                        </td>
                    </tr>";
                } else {
                    $package='US Adult - 15 years old and above';
                    $discount = "0%";
                }
            }
            
        }

        if($age == '00' || $age == '01') {
           $yr_old = 'year old';
        } else {
            $yr_old = 'years old';
        }

            $medical_date = date("l, F d, Y", strtotime($this->data['sched_date']));
            // $medical_time = str_replace(" ", ":00 ", $this->data['sched_time']);
            $medical_time = $this->data['sched_time'];

            $medical_date_reschedule = date('F d, Y', strtotime($this->data['sched_date']. ' - 2 day'));

            $medical_fee = number_format($this->data['medical_fee'], 2);
            $payment_status = $this->data['payment_status'];
            
            $PayCode = $this->data['paycode'];
            $applicant_name = strtoupper($this->data['name']);

            $path = public_path('/img/slec-email-logo.png');
            $type = pathinfo($path, PATHINFO_EXTENSION);
            $data = file_get_contents($path);
            $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);


            $html ="
                <html>
                <head>
                    <title>$PayCode</title>
                </head>
                <style>
                    @import url(\'https://fonts.googleapis.com/css?family=Consolas\');
                    @font-face {
                        font-family: C39T36L;
                        src: url(https://slec.ph/font/C39T36L.woff);
                    }

                    @font-face {
                        font-family: consola;
                        src: url(https://slec.ph/font/consola.ttf);
                    }
                    body {
                        padding: 0;
                        margin: 0;
                    }
                    p {
                        font-size: 11pt
                    }
                    li {
                        font-size: 11pt
                    }
                </style>
                <body>";
            $html .= "
                    <table align='center' border='0' width='100%'>
                        <tr><td><center><img src='$base64'/></center><br></td></tr>
                        <tr>
                            <td align='center'><span style='font-size: 8pt; font-family: Arial, Verdana;'> 1177 JORGE BOCOBO ST. ERMITA, MANILA 1000 &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;(02) 8521-0020&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;https://slec.ph&nbsp;&nbsp;&nbsp;<hr/></td>
                        </tr>
                    </table>
                    <table  border='0'>
                    <tr>
                        <td align='center' class=''>
                            <h1 style='font-family: Arial, Sans Serif; margin: 0;'>$medical_date</h1>
                            <h2 style='margin: 5px 0px 20px;'>
                                <u style='font-family:Arial, Sans Serif;'>Time: $medical_time</u>
                            </h2>
                        </td>
                    </tr>
                    <tr>
                        <td align='center' style='padding:0; margin:0;'>
                            <p style='font-family: consola; font-size: 25pt; padding:0; margin:0;'>$PayCode</p>
                            <p style='font-family: C39T36L; font-size: 70pt; padding:0; margin:0;'>*$PayCode*</p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <p style='font-size: 14px'>Dear Maam/Sir:</p>
                    
                            <p style='font-size: 14px'>
                                This confirms the medical examination appointment of U.S. visa applicant
                                <strong> $applicant_name </strong> at St. Luke s Medical Center Extension Clinic
                                (SLEC), on <strong>$medical_date</strong> at
                                <strong> $medical_time </strong
                                >. Please be at the clinic at least 30 minutes before your scheduled
                                appointment.
                            </p>
                    
                            <p style='font-size: 14px'>
                                SLEC strictly observes the schedule for the visa medical examination. Visa
                                applicants who arrive on time shall be given priority over those who arrive
                                late and miss their appointment.
                            </p>

                            <p style='font-size: 14px'>Sudden changes in quarantine restrictions may affect clinic schedules.  Please provide an email address and phone number that is working and accessible to you so we can contact you when needed. <b>Visit our website regularly for important announcements.</b></p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <p style='font-size: 14px; text-align:center;'><b><u>IMPORTANT REMINDERS:</u></b></p>

                            <p>Prepare the following required documents inside a short brown envelope in this exact order:</p>
                            <ul>
                                <li>Medical Examination Appointment Confirmation Email from SLEC (printed or digital copy)</li>
                                <li>Please print a copy of the attached Process Flow and bring it with you on the day of your medical examination.</li>
                                <li>Passport (<i>must be valid for at least 6 months</i>) – original and one (1) photocopy of the biographic data page (actual size).</li>
                            </ul>
                       
                            <p>Minors (i.e., ages 17 years and below) who will not be accompanied by their parents during the medical examination must also present the following:</p>
                            
                            
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <ul>
                                <li>Letter of Authorization or Special power of attorney stating that the companion is authorized to sign on behalf of the parent (companion must be knowledgeable of the minor\'s medical history)</li>
                                <li>Parent's valid government signature bearing ID – two (2) photocopies</li>
                                <li>Authorized companion's valid government signature bearing ID – original and two (2) photocopies</li>
                            </ul>
                            <p>Bring the following documents:</p>
                            <ul>
                                <li>Visa Interview Appointment Letter – one (1) photocopy</li>
                                <li>Letter with Case Number / K1FTP Letter (K Visa) – one (1) photocopy</li>
                                <li>DS 260 Confirmation for Regular Immigrants OR DS 160 Confirmation for K Visa applicants</li>
                            </ul>
                            <p><b>Each person who will undergo a visa medical examination must have one envelope that contains his/her documents. Please do not staple the documents.</b></p>
                            <ul>
                                <li><b>In the event that the US Embassy or NVC gives you a visa interview date that is <u>earlier</u> than your scheduled medical examination appointment date, please send us an email at <u>inquiry@slec.ph</u> so we can reschedule your medical examination accordingly.</b></li>
                                <li>For health and safety reasons, only visa applicants will be allowed to enter clinic premises. Only one companion will be allowed in the case of senior citizens, PWDs, or minors (i.e., ages 17 years and below), provided that the minor is not accompanied by an adult visa applicant.</li>
                                <li>Always wear your face mask within clinic premises on the day of your visa medical examination</li>
                                <li>Applicants who wear contact lenses should bring their container and soaking liquid in case they are asked to remove the lenses during the medical exam. </li>
                            </ul>
                        </td>
                    </tr>
                    
                    <tr>
                        <td>
                            <strong style='font-size: 16px'><u>RESCHEDULING</u></strong>
                            <p>You may reschedule your appointment for visa medical examination not later than <strong>$medical_date_reschedule</strong> by clicking on <strong>https://account.slec.ph/</strong>. 
                            Rescheduling is subject to availability, and may affect the medical fee in case your age changes.  Any difference in amount shall be settled at the Clinic 
                            on the day of your medical examination.</p>

                            <p>If your visa interview date is earlier than the date of your visa medical examination appointment date, please send us an email at <strong>inquiry@slec.ph</strong> 
                            so that we can reschedule your appointment.</p>
                        </td>
                    </tr>
                    <tr>
                        <td><hr /></td>
                    </tr>
                    <tr>
                        <td class='pl-3'>
                        <h2>ERMITA, MANILA</h2>
                        <h4>CONTACT DETAILS</h4>
                            <p style='font-size: 14px'>
                            <strong>Clinic Address:</strong><br />
                            1177 Jorge Bocobo St., Ermita, Manila
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td class='pl-3'>
                            <p style='font-size: 14px'>
                            <strong>Contact Numbers:</strong><br />
                            (02) 8521-0020
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td class='pl-3'>
                            <p style='font-size: 14px'>
                            <strong>Clinic Hours:</strong><br />
                            Mondays to Fridays from 7:00AM to 4:30PM (We are closed on declared Philippine
                            holidays)
                            </p>
                        </td>
                    </tr>";

                    if($this->data['payment_status']==0){
                        $html .= "
                            <tr style='page-break-before: always; break-before: always;'>
                                <td>
                                <h3>Mode of Payment:</h3>
                                <ul style='font-size: 14px' class='listed_items_1'>
                                    <li>Cash payment at the Clinic Cashier on the day of your medical exam.</li>
                                    <li>Credit Card (Visa and Mastercard only)</li>
                                    <ul class='listed_items_2'>
                                    <li>Credit card must only be used and signed by the owner of the card.</li>
                                    <li>Authorization to use credit card is not accepted.</li>
                                    <li>Debit card is not accepted.</li>
                                    <li>
                                        Credit card downtime may happen due to system update/maintenance by the
                                        provider.
                                    </li>
                                    </ul>
                                    <li>Online Payment (BDO)</li>
                                    <ul class='listed_items_2'>
                                    <li>Visa, Mastercard, Amex, JCB, and Diners are available for our online payment.</li>
                                    <li>Please make sure that you are signed in to your account, as you can only make the payment there.</li>
                                    <li>Please make sure to strictly follow the step-by-step instructions in the attached online payment guide.</li>
                                    <li>Please be reminded that the GCash Visa card will not be accepted for our online payments and may cause issues with your online registration.</li>
                                    </ul>
                                    <li>Security Bank.</li>
                                    <ul class='listed_items_2'>
                                    <li>Over the counter at any Security Bank Branch</li>
                                    <li>
                                        Security Bank online payment (with existing SBC online bank account)
                                    </li>
                                    <li>Security Bank mobile app (with existing SBC online bank account)</li>
                                    <li>See attached payment guide for Security Bank Bills Payment.</li>
                                    </ul>
                                    
                                </ul>
                                </td>
                            </tr>
                            <tr>
                                <td style='color:red;padding:0 10px 0 10px;text-align: center;'>
                                    <p style='font-size: 15px; font-style: bold'>
                                        Payments made to St. Luke's Medical Center - Quezon City or Global City will not be credited to your medical examination package. Such payment discrepancies may cause significant delays to your appointment.
                                    </p>
                                </td>
                            </tr>
                            </table>
                            <table style='border: 2px solid black; margin-top:2rem;' width='100%' height='auto'>
                                <tr>
                                <td style='border-bottom: 2px solid black;' class='p-2' align='center'>
                                    <h3 class='fw-bold'>
                                    PLEASE PRINT AND PRESENT THIS FORM WHEN PAYING AT THE BANK
                                    </h3>
                                </td>
                                </tr>
                                <tr>
                                <td class='p-2' align='center'>
                                    <h4 class='fw-bold'><u>PAYMENT INSTRUCTIONS</u></h4>
                                </td>
                                </tr>
                                <tr>
                                <td class='p-3'>
                                    <ol style='font-size: 14px; line-height: 25px' class='listed_items_1'>
                                    <li>
                                        Only the <strong>initial medical examination</strong> may be paid via bank
                                        transaction. Additional tests must be paid at the clinic cashier.
                                    </li>
                                    <li>
                                        <strong
                                        >Should there be an adjustment in medical fees prior to medical
                                        examination, the new medical fee will prevail. The applicant must pay
                                        the difference between the old and new fees at the clinic cashier on the
                                        day of medical exam.</strong
                                        >
                                    </li>
                                    <li>
                                        <strong
                                        >For Over-the-Counter Payments, the Universal transaction slip shall
                                        serve as your proof of payment.</strong
                                        >
                                    </li>
                                    <li>
                                        <strong
                                        >You must present the Original Universal Transaction Slip or the printed
                                        Transaction Notification slip on the day of your Visa Medical
                                        Exam.</strong
                                        >
                                    </li>
                                    <li>
                                        If your payment thru SBC or BDO online payment encounters an error and could not produce a
                                        machine validated transaction slip, please email us the reference code and the screen shot of your payment status before you proceed to
                                        your medical examination, this is for us to validate your payment status
                                        with the bank.
                                    </li>
                                    <li>
                                        For instructions on how to pay, please see attached Online Payment Guide
                                        (PDF).
                                    </li>
                                    </ol>
                                </td>
                                </tr>
                                </table>
                                <table style='border: 2px solid black; padding: 0; margin: 0; page-break-before: always; break-before: always;' width='100%' height='auto'>
                                <tr>
                                <td style='border-bottom: 2px solid black; padding: 0; margin: 0;' align='center'>
                                    <h3 class='fw-bold'>PAYMENT DETAILS (for Initial Medical)</h3>
                                </td>
                                </tr>
                                <tr>
                                <tr>
                            </tr>
                            <tr>
                                <td style='font-size: 14px'>BILLER NAME:</td>
                            </tr>
                            <tr>
                                <td style='padding-bottom: 18px; font-size: 20px'>
                                <strong> ST. LUKE'S EXTENSION CLINIC </strong>
                                </td>
                            </tr>
                            <tr>
                                <td style='font-size: 14px; padding-bottom: 10px;'>
                                SUBSCRIBER NUMBER:
                                <p style='font-family: consola; font-size: 18pt; padding:0; margin:0;'>$PayCode</p>
                                </td>
                            </tr>
                            <tr>
                                <td style='font-size: 14px'>
                                APPLICANT NAME:
                                </td>
                            </tr>
                            <tr>
                                <td style='font-size: 16px; padding-bottom: 20px;'>
                                <strong>
                                $applicant_name
                                </strong>
                                </td>
                            </tr>
                            <tr>
                                <td style='font-size: 14px' class='pt-3'>APPLICANT AGE:</td>
                            </tr>
                            <tr>
                                <td style='font-size: 16px; padding-bottom: 20px;'>
                                <strong>
                                    $age $yr_old
                                </strong>
                                </td>
                            </tr>
                             <tr>
                                <td style='font-size: 14px' class='pt-3'>PACKAGE:</td>
                            </tr>
                            <tr>
                                <td style='font-size: 16px; padding-bottom: 20px;'>
                                <strong> $package </strong>
                                </td>
                            </tr>
                            <tr>
                                <td style='font-size: 14px' class='pt-3'>DISCOUNT:</td>
                            </tr>
                            <tr>
                                <td style='font-size: 16px; padding-bottom: 20px;'>
                                <strong> $discount </strong>
                                </td>
                            </tr>
                            <tr>
                                <td style='font-size: 14px' class='pt-3'>AMOUNT TO BE PAID:</td>
                            </tr>
                            <tr>
                                <td style='font-size: 16px; padding-bottom: 20px;'>
                                <strong> PHP $medical_fee </strong>
                                </td>
                            </tr>
                            $discount_message
                            <tr>
                                <td style='font-size: 16px; padding: 30px 0px 20px;' class='p-5' align='center'>
                                <strong> (PLEASE PAY THE EXACT AMOUNT) </strong>
                                </td>
                            </tr>
                            </table>
                            <div class='col-12' style='padding-top: 10px;'>
                            <a>ONCE PAYMENT HAS BEEN MADE, PLEASE ATTACH THE
                                <strong>PROOF OF PAYMENT WITH YOUR ONLINE REGISTRATION CONFIRMATION</strong> AND
                                PRESENT AT THE CLINIC ON THE DAY OF YOUR MEDICAL EXAMINATION.<br><br>
                                MEDICAL EXAMINATION 
                                FEES MAY BE <strong>ADJUSTED</strong> BASED ON THE APPROPRIATE PACKAGE OF YOUR VISA TYPE DURING 
                                YOUR APPOINTMENT INCLUDING <strong>DISCOUNT VALIDITY</strong>.</a>
                            </div>";
                    }
                
                $html .="
                    <div class='col-12 pt-3'>
                    <p style='font-size: 13px; font-style: italic'>
                        Confidentiality Notice: The contents of this email and any attachments are
                        confidential and/or legally privileged and are intended solely for the addressee.
                        If you received this email message in error, please immediately notify the sender
                        by reply email and delete this message and its attachments. Any use, reproduction,
                        or dissemination of this transmission by persons or entities other than the
                        intended recipient is strictly prohibited.
                    </p>
                    </div>
                </body>
                </html>
                ";
                
            $link=resource_path('views/confirmation/attachments/letter/SLEC Online Registration Confirmation - '.$this->data['paycode'].'.pdf');
            $pdf = PDF::loadHTML($html)->setPaper('a4', 'portrait')->save($link);


            $email = $this->view('emails.confirmationMail')
                            ->subject('SLEC Online Registration Confirmation - '.$this->data['paycode'])
                            ->bcc('slmcec2021@gmail.com')
                            ->with(['message' => $this])
                            ->attach(resource_path('views/confirmation/attachments/letter/SLEC Online Registration Confirmation - '.$this->data["paycode"].'.pdf'))
                            ->attach(public_path('/pdf/US-Process-Flow.pdf'))
                            ->attach(public_path('/pdf/online-payment-guide.pdf'));
            
            return $email;



    }


    // public function envelope()
    // {
    //     return new Envelope(
    //         subject: 'SLEC Online Registration Confirmation - '.$data['payCode'].'',
    //     );
    // }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    // public function content()
    // {
    //     return new Content(
    //         view: 'emails.confirmationMail',
    //     );
    // }

    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    // public function attachments($data)
    // {
    //     return [
    //         Attachment::fromPath(resource_path('views\confirmation\attachments\letter\SLEC Online Registration Confirmation - '.$data["payCode"].''))
    //             ->as('SLEC Online Registration Confirmation.pdf')
    //             ->withMime('application/pdf'),
                
    //         Attachment::fromPath('C:\xampp\htdocs\slec-fullstack\backend\public\pdf\US-Process-Flow.pdf')
    //             ->as('US Process Flow.pdf')
    //             ->withMime('application/pdf'),

    //         Attachment::fromPath('C:\xampp\htdocs\slec-fullstack\backend\public\pdf\sb-bills-payment-guide.pdf')
    //             ->as('sb-bills-payment-guide.pdf')
    //             ->withMime('application/pdf'),
    //     ];

        // return [
        //     Attachment::fromPath('C:\xampp\htdocs\slec\slec-user-portal-backend\public\pdf\US-Process-Flow.pdf')
        //         ->as('US Process Flow.pdf')
        //         ->withMime('application/pdf'),

        //     Attachment::fromPath('C:\xampp\htdocs\slec\slec-user-portal-backend\public\pdf\sb-bills-payment-guide.pdf')
        //     ->as('sb-bills-payment-guide.pdf')
        //     ->withMime('application/pdf'),
        // ];
    // }
}
