<?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 NZ_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');
        $medCertType = Str::upper($this->data['ExamType_NZ']);
        $medical_branch = $this->data['branch'];
        
        if($age == '00' || $age == '01') {
            $yr_old = 'year old';
        } else {
            $yr_old = 'years old';
        }

        if($age<5) {
            if ($nationality == "PHILIPPINES" && $discount == "Y") {
                $package='New Zealand Pedia Medical (0-4 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='New Zealand Pedia Medical (0-4 years old)';
                $discount = "0%";
            }
        } elseif($age>4 && $age<11) {
            if ($nationality == "PHILIPPINES" && $discount == "Y") {
                $package='New Zealand Pedia Medical (5-10 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='New Zealand Pedia Medical (5-10 years old)';
                $discount = "0%";
            }
        } elseif($age>10 && $age<15) {
            if ($nationality == "PHILIPPINES" && $discount == "Y") {
                $package='New Zealand Pedia Medical (11-14 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='New Zealand Pedia Medical (11-14 years old)';
                $discount = "0%";
            }
        } elseif($age>14) {
            if ($age > 59) {
                if ($nationality == "PHILIPPINES") {
                    $package='New Zealand Adult Medical (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='New Zealand Adult Medical (Senior Citizen)';
                    $discount = "0%";
                }
            } else {
                if ($nationality == "PHILIPPINES" && $discount == "Y") {
                    $package='New Zealand Adult Medical (15 years and older) | 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='New Zealand Adult Medical (15 years and older)';
                    $discount = "0%";
                }
            }
            
        } elseif($age>59) {
            $package='New Zealand Adult Medical (Senior Citizen)';
        }


        if($medCertType == 'FULL') {
            $exam_type = 'Full Medical Examination - '.$package;
        } elseif($medCertType == 'CXR') {
            $exam_type = 'Chest X-ray Only - '.$package;
        } elseif($medCertType == 'LIMITED') {
            $exam_type = 'Limited Medical Examination - '.$package;
        }

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

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

        $medical_fee = $this->data['medical_fee'];
        $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<hr/></td>
                    </tr>
                    <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 New Zealand visa applicant
                            <strong> $applicant_name </strong> at St. Luke s Medical Center Extension Clinic
                            - <strong>$medical_branch</strong>, 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. <b>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:</li>
                        <ol start='1'>
                            <li>Two (2) pieces recent passport-size pictures (white or blue background)</li>
                            <li>Printed copy of this email</li>
                            <li>Passport (<i>must be valid for at least 6 months</i>) – original and two (2) photocopies of the biographic data page (actual size).</li>
                        </ol>
                    </td>
                </tr>
                <tr>
                    <td>
                        <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>
                        <ol start='5'>
                            <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 government-issued ID – two (2) photocopies</li>
                            <li>Authorized companion\'s government-issued ID – original and two (2) photocopies</li>
                        </ol>
                        <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>
                            <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>
                    </td>
                </tr>
                <tr>
                    <td><hr /></td>
                </tr>
                <tr>		
				    <td><center><h3>Clinic Contact Details</h3></center></td>
				</tr>
		        <tr>
		            <td>
			            <table border='0' cellpadding='0' cellspacing='0' width='100%' align='center'>
				            <tr>
				                    <td colspan='2' valign='top' align='center'>
				 	                    <table align='left' border='0' cellpadding='10' cellspacing='0' width='95%'>
					                        <tr>
                                                <td valign='top' style='font-size: 10pt; font-family: Arial, Tahoma;' >
                                                    <strong>ST. LUKE'S MEDICAL CENTER EXTENSION CLINIC - MANILA</strong>
                                                    <br />
                                                    1177 Jorge Bocobo St.,<br /> Ermita, Manila<br />
                                					<br />
                                					Telephone Numbers:<br />
                                					(02) 8521-0020<br />
                                					<br />
                                					<br />Clinic Schedule:<br />
                                					<strong>Mondays to Fridays from 7:00AM to 4:30PM</strong>
                                                </td>
                                            </tr>
                                        </table>
			                        </td>
			                        <td colspan='2' valign='top'>
					                    <table align='right' border='0' cellpadding='10' cellspacing='0' width='95%'>
                                            <tr>
                                                <td valign='top' style='font-size: 10pt; font-family: Arial, Tahoma;' >
                                                    <strong>BONIFACIO GLOBAL CITY</strong><br />
												    Room 1002 Medical Arts Bldg<br />
													St. Luke's Medical Center<br />
													Bonifacio Global City, Taguig<br />
													<br />
                                					Telephone Numbers:<br />
                                					(02) 8789-7700 loc 7002, 7003, 7005<br />
                                					<br />Clinic Schedule:<br />
													<strong>Mondays to Fridays from 8:00AM to 2:30PM</strong>
                                                </td>
											</tr>
                                        </table>
			                        </td>
			                    </tr>
		                    </table>
		                </td>
		            </tr>
		            <tr>
				        <td colspan='4' valign='top'>
        				   <p align='center'><strong>We are closed on Philippine holidays.</strong></p>
        				   <p align='center'><strong>For more information on your visa medical exam, please visit</strong> 
        				   <strong><a href='https://www.slec.ph'>https://www.slec.ph</a></strong></p>
				        </td>
		            </tr>
                <tr>
                <td>
                    <hr />
                </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>
                        </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> $exam_type </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/online-payment-guide.pdf'));
        
        return $email;

        

    }


    // public function envelope()
    // {

    // }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    // public function content()
    // {

    // }

    /**
     * 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'),
        // ];
    // }
}
