@extends('layouts.yellow')
@section('content')
<div class="container-fluid">
    @if(isset($exist) && ($exist === "1"))
    <div class="row">
        <div class="col-12">
            <div class="alert alert-primary text-center">
                {{__("detail-patient.patientRegistered")}}
            </div>
        </div>
    </div>
    @endif
    
    <div class="row">
        <div class="col-12 p-4">
            <h2 class="text-dark font-weight-bold">{{$model->firstname." ".$model->lastname}}</h2>
        </div>
    </div>
    
    <div class="row">
        <div class="col-12">
            <div class="card shadow-lg rounded-lg border-0">
                <div class="card-body">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-12 text-right">
                                @php
                                    $accessMode = \App\Models\Settings::getAccessMode();
                                @endphp

                                <div class="dropdown d-inline-block">
                                    <button class="btn btn-info dropdown-toggle list-button" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                        {{__("detail-patient.actions")}}
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li>
                                            <a class="dropdown-item d-flex align-items-center" href="update-patient?id={{$model->id}}">
                                                <i class="bi bi-pencil-square me-2"></i> {{__("detail-patient.updatePatient")}}
                                            </a>
                                        </li>

                                        @if(in_array(\App\Models\Settings::APPOINTMENT_ONLY_MODE, $accessMode) ||
                                            in_array(\App\Models\Settings::FULL_ACCESS_MODE, $accessMode))
                                            <li>
                                                <a class="dropdown-item d-flex align-items-center" href="create-appointment?patientId={{$model->id}}">
                                                    <i class="bi bi-calendar-heart me-2"></i> {{__("detail-patient.createAppointment")}}
                                                </a>
                                            </li>
                                            
                                            <li>
                                                <a class="dropdown-item d-flex align-items-center" href="{{ route('recurring-appointments.index', ['patient_id' => $model->id]) }}">
                                                    <i class="bi bi-calendar-check me-2"></i> {{__("recurring-appointments.viewPatientRecurring")}}
                                                </a>
                                            </li>
                                        @endif

                                        @if(in_array(\App\Models\Settings::HISTORY_ONLY_MODE, $accessMode) ||
                                            in_array(\App\Models\Settings::FULL_ACCESS_MODE, $accessMode))
                                            @if($model->haveHistories())
                                                <li>
                                                    <a class="dropdown-item d-flex align-items-center" href="detail-history?id={{$model->histories()->first()->id}}&patientId={{$model->id}}">
                                                        <i class="bi bi-journals me-2"></i> {{__("detail-patient.viewHistory")}}
                                                    </a>
                                                </li>
                                                <li>
                                                    <a  class="dropdown-item d-flex align-items-center" href="create-control-history?historyId={{$model->histories()->first()->id}}">
                                                        <i class="bi bi-journals me-2"></i> {{__('create-control-history.create')}} 
                                                    </a>
                                                </li>
                                                @else
                                                <li>
                                                    <a class="dropdown-item d-flex align-items-center" href="create-history?patientId={{$model->id}}">
                                                        <i class="bi bi-journals me-2"></i> {{__("detail-patient.createHistory")}}
                                                    </a>
                                                </li>
                                            @endif
                                        @endif

                                        @if((int)$model->age < config("app.minimumAgeAdult"))
                                            <li>
                                                <a class="dropdown-item" href="create-representative?patientId={{$model->id}}">
                                                    {{__("options.optionSeven")}}
                                                </a>
                                            </li>
                                        @endif

                                        @if(Auth::user()->isAbleTo('create-medical-report'))
                                            <li>
                                                <a class="dropdown-item d-flex align-items-center" href="create-medical-report?patientId={{$model->id}}">
                                                    <i class="bi bi-file-earmark-arrow-down-fill me-2"></i> {{__("detail-patient.createMedicalReport")}}
                                                </a>
                                            </li>
                                        @endif

                                        <li>
                                            <a class="dropdown-item d-flex align-items-center" href="view-medical-reports?patientId={{$model->id}}">
                                                <i class="bi bi-file-earmark-word me-2"></i> {{__("detail-patient.viewMedicalReports")}}
                                            </a>
                                        </li>

                                        <li><hr class="dropdown-divider"></li>

                                        @if(Auth::user()->isAbleTo('create-medical-recipe'))
                                            <li>
                                                <a class="dropdown-item d-flex align-items-center" href="create-medical-recipe?patientId={{$model->id}}">
                                                    <i class="bi bi-file-earmark-arrow-down-fill me-2"></i> {{__("detail-patient.createMedicalRecipe")}}
                                                </a>
                                            </li>
                                        @endif

                                        <li>
                                            <a class="dropdown-item d-flex align-items-center" href="view-medical-recipes?patientId={{$model->id}}">
                                                <i class="bi bi-file-earmark-word me-2"></i> {{__('detail-patient.viewMedicalRecipes')}}
                                            </a>
                                        </li>

                                        <li><hr class="dropdown-divider"></li>

                                        <li>
                                            <a class="dropdown-item d-flex align-items-center" href="{{ route('patient.documents', ['patientId' => $model->id, 'source' => 'patient', 'sourceId' => $model->id]) }}">
                                                <i class="bi bi-folder-fill me-2"></i> {{__('detail-patient.manageDocuments')}}
                                            </a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="card shadow-lg rounded-lg border-0">
                <div class="card-body">
                    <ul class="nav nav-tabs" id="patientDetailTabs" role="tablist">
                        <li class="nav-item" role="presentation">
                            <button class="nav-link active" id="main-info-tab" data-bs-toggle="tab" data-bs-target="#main-info" 
                                type="button" role="tab" aria-controls="main-info" aria-selected="true">
                                <i class="bi bi-person-badge me-2"></i>{{ __('detail-patient.main_information') }}
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="medical-reports-tab" data-bs-toggle="tab" data-bs-target="#medical-reports" 
                                type="button" role="tab" aria-controls="medical-reports" aria-selected="false">
                                <i class="bi bi-file-earmark-word me-2"></i>{{ __('detail-patient.medical_reports') }}
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="custom-fields-tab" data-bs-toggle="tab" data-bs-target="#custom-fields" 
                                type="button" role="tab" aria-controls="custom-fields" aria-selected="false">
                                <i class="bi bi-card-list me-2"></i>{{ __('patient-custom-fields.additional_information') }}
                            </button>
                        </li>
                        @if((int)$model->age < 18)
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="representatives-tab" data-bs-toggle="tab" data-bs-target="#representatives" 
                                type="button" role="tab" aria-controls="representatives" aria-selected="false">
                                <i class="bi bi-people me-2"></i>{{ __('detail-patient.representatives') }}
                            </button>
                        </li>
                        @endif

                    </ul>
                    
                    <div class="tab-content pt-4" id="patientDetailTabsContent">
                        <!-- Main Information Tab -->
                        <div class="tab-pane fade show active" id="main-info" role="tabpanel" aria-labelledby="main-info-tab">
                            <livewire:detail-patient-view :model="$model" />
                        </div>
                        
                        <!-- Custom Fields Tab -->
                        <div class="tab-pane fade" id="custom-fields" role="tabpanel" aria-labelledby="custom-fields-tab">
                            @include('patient-custom-fields.fields-form', ['model' => $model])
                        </div>
                        
                        <!-- Medical Reports Tab -->
                        <div class="tab-pane fade" id="medical-reports" role="tabpanel" aria-labelledby="medical-reports-tab">
                            <livewire:medical-reports-table-view :model="$model"/>
                        </div>
                        
                        <!-- Representatives Tab (Only for patients under 18 years) -->
                        @if((int)$model->age < 18)
                        <div class="tab-pane fade" id="representatives" role="tabpanel" aria-labelledby="representatives-tab">
                            @php
                                $relationships = $model->relationships()->get();
                            @endphp
                            @include('patient.representatives', ['model' => $model, 'relationships' => $relationships])
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
