<?php

namespace App\Http\Livewire;

use LaravelViews\Views\TableView;
use App\Models\Appointments;
use App\Models\Especiality;
use App\Models\Patients;
use DateTime;
use App\Actions\ViewDetailAppointment;
use App\Actions\DeleteAppointment;
use LaravelViews\Facades\UI;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use App\Http\Livewire\ConfigSearch;

class AppointmentTableView extends TableView
{
    use ConfigSearch;
    /**
     * Sets a model class to get the initial data
     */
    protected $model = Appointments::class;

    protected $paginate = 10;


    public $searchBy = ['code', 'patient.firstname','patient.lastname','patient.age','especiality','user.name','date'];

    public function repository(){
        $userId = Auth::user()->id;
        if(isset($_GET["today"]) && ($_GET["today"]==="1") )
            return Appointments::where("active",'=', '1')->whereDate("date",Carbon::today());
        else if(isset($_GET["myAppointments"]) && ($_GET["myAppointments"]==="1"))
            return Appointments::where("active",'=', '1')
            ->where("users_id", '=', $userId);
        else if (isset($_GET["day"]))
            return Appointments::where("active",'=', '1')->whereDate("date",Carbon::parse($_GET["day"]));
        else
             return Appointments::where("active",'=', '1');
    }

    /**
     * Sets the headers of the table as you want to be displayed
     *
     * @return array<string> Array of headers
     */
    public function headers(): array
    {
        return [
          /*  __('common-table.table.code'),*/
            __('common-table.table.firstname'),
            __('common-table.table.especiality'),
            __('common-table.table.doctorname'),
           /* __('common-table.table.phones'),*/
            __('common-table.table.date')
        ];
    }

    /**
     * Sets the data to every cell of a single row
     *
     * @param $model Current model for each row
     */
    public function row($model): array
    {
        $patient = $model->patient()->get()->first();
        $representantivesData = "";
        $phoneData = (isset($patient->phone)) ? $patient->phone : implode(",",Patients::getPhoneForRepresentatives($patient));

        $date  = new DateTime($model->date);

        foreach($model->patient()->get() as $patient){
            $relationships = $patient->relationships()->get();
            foreach($relationships as $relationship){
                foreach($relationship->representative()->get() as $representative){
                  $representantivesData .= $representative->firstname ." ".$representative->lastname.",";
                }
              }
        }

        

        return [
            /*UI::link(
                $model->code, 
                 'detail-appointment?id='.$model->id),*/
            UI::link(
                $patient->firstname . " ".$patient->lastname, 
                'detail-appointment?id='.$model->id),
            Especiality::getEspecialities()[$model->especiality],
            $model->user()->first()->name,
          /*  $phoneData,*/
            //$date->format("d/m/Y")
            $model->date
        ];
    }

    /** For actions by item */
    protected function actionsByRow(){
        return [
            new ViewDetailAppointment,
            new DeleteAppointment
        ];
    }
}
