<?php

namespace App\View\Components;

use Illuminate\View\Component;
use App\Models\Patients;
use App\Models\RoleUser;

class AppointmentForm extends Component
{

    public $appointment;
    public $patients; 
    public $especialities;
    public $calendar;
    public $new;
    public $patientSelected;
    public $doctors;
    public $isExternal;
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct($appointment,$patients,$especialities,$calendar,$doctors,$new=true,$isExternal=false)
    {
        $patientsView = [];

        foreach($patients as $patient){
            $identification = ( (isset($patient->identification)) && ($patient->identification !== "N/A")) ? $patient->identification : Patients::getIdentificationByRepresentatives($patient);
                array_push($patientsView,[
                    "id"=>$patient->id,
                    "name"=>$patient->firstname ." ".$patient->lastname." - ".$identification,
                    "representative"=>Patients::getRepresentatives($patient)
                ]);
        }

        if(isset($_GET["patientId"])){
            foreach($patients as $patient)
                if($patient->id === trim($_GET["patientId"])){
                    $this->patientSelected = $patient;
                    break;
                }
        }


        $this->appointment = $appointment;
        $this->patients = $patientsView;
        $this->especialities = $especialities;
        $this->calendar  = $calendar;

        $this->doctors = [];

        foreach($doctors as $doctor){
            if(!(strtoupper(RoleUser::getRolShortname($doctor)) === "ADMIN" OR strtoupper(RoleUser::getRolShortname($doctor)) === "ROOT"))
                array_push($this->doctors,$doctor);
        }

        $this->new = $new;
        $this->isExternal = $isExternal;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.appointment-form');
    }
}
