<?php

namespace App\View\Components;

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


class HistoryForm extends Component
{
    public $history;
    public $new;
    public $optionsEspeciality;
    public $patients;
    public $sectionTypes;
    public $physicalTest;
    public $patientSelected;
    public $configuration;

    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct($history,$patients,$sectionTypes,$physicalTest,$new=true,$configuration=null)
    {
        $patientsView = [];

        if($new == true){
            foreach($patients as $patient){
                $identification = ( (isset($patient->identification)) && ($patient->identification !== "N/A")) ? $patient->identification : Patients::getIdentificationByRepresentatives($patient);
                if(!($patient->haveHistories())){
                    array_push($patientsView,[
                        "id"=>$patient->id,
                        "name"=>$patient->firstname ." ".$patient->lastname." - ".$identification,
                        "representative"=>Patients::getRepresentatives($patient)
                    ]);
                }
            }
        }else{
            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->physicalTest = $physicalTest;
        $this->sectionTypes = $sectionTypes;
        $this->patients =  $patientsView;
        $this->history = $history;
        $this->new = $new;
        $this->configuration = $configuration;
        $this->optionsEspeciality = Especiality::getEspecialities();
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.history-form',[
            "history"=>$this->history,
            "new"=>$this->new,
            "physicalTest"=>$this->physicalTest,
            "configuration"=>$this->configuration
        ]);
    }
}
