<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Helper\Helper;
use PDF;
use App\Models\Especiality;
use App\Models\ClinicIdentity;

class AppointmentMailWithTemplate extends Mailable
{
    use Queueable, SerializesModels;
    
    public $fromAddress;
    public $subject;
    public $data;
    public $helper;
    public $appointment;
    public $clinicIdentity;

   /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(
        $from,
        $subject,
        $data,
        $model = NULL)
    {
        $this->fromAddress = $from;
        $this->subject = $subject;
        $this->data = $data;
        $this->helper = Helper::class;
        $this->appointment = $model;
        $this->clinicIdentity = ClinicIdentity::getActive();
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $especialities = Especiality::getEspecialities();
        $this->from($this->fromAddress, $this->subject);
        $this->appointment->clinicIdentity = $this->clinicIdentity;
        
        $pdf = PDF::loadView('reports.appointments', [
            'appointment' => $this->appointment,
            'patient' => $this->appointment->patient()->first(),
            "especialities"=>$especialities,
            "data"=>$this->data,
            "clinicIdentity" => $this->clinicIdentity
            ]);
        return $this->view('mail.appointment-with-template-mail')->attachData($pdf->output(), $this->appointment->patient()->first()->firstname." ".$this->appointment->patient()->first()->lastname.'.pdf', [
            'mime' => 'application/pdf',
            ]);;
    }
}
