@extends('layouts.yellow')
@section('content')
<style>
   .accordion-button:not(.collapsed) {
   color: #000;
   background-color:#fddc7b !important;
   background: rgb(255,200,0) !important;
   background: linear-gradient(90deg, rgba(255,200,0,0.3897934173669467) 0%, rgba(255,141,113,0.1881127450980392) 100%) !important;
   box-shadow: inset 0 -1px 0 rgb(0 0 0 / 13%);
   }
   .nav-link{
   color:#c30d0d;
   }
   .nav-link:hover{
   color:#c30d0d;
   }
   .whitespace-no-wrap {
            white-space: normal;
        }
</style>
<div class="container-fluid">
   <div class="row">
      <!--<div class="col-12">
         <x-ButtonDeleteDetail></x-ButtonDeleteDetail>  
         </div>-->
      <div class="col-12">
         @if(session('error'))
         <div class="alert alert-danger">
            {{ session('error') }}
         </div>
         @endif
      </div>
      <div class="col-12">
         @if (session('success'))
         <div class="alert alert-success">
            {{ session('success') }}
         </div>
         @endif
      </div>
   </div>
   <div class="row">
      <div class="col-12">
         <div class="float-end p-3">
            <div class="dropdown" style="display:inline-flex">
               <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>
                     <div class="p-2">
                        <a href="detail-patient?id={{$model->patient()->first()->id}}">
                           {{__('com-form-history.returnToInfoPatient')}} &nbsp;
                           <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person-badge" viewBox="0 0 16 16">
                              <path d="M6.5 2a.5.5 0 0 0 0 1h3a.5.5 0 0 0 0-1zM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0"/>
                              <path d="M4.5 0A2.5 2.5 0 0 0 2 2.5V14a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V2.5A2.5 2.5 0 0 0 11.5 0zM3 2.5A1.5 1.5 0 0 1 4.5 1h7A1.5 1.5 0 0 1 13 2.5v10.795a4.2 4.2 0 0 0-.776-.492C11.392 12.387 10.063 12 8 12s-3.392.387-4.224.803a4.2 4.2 0 0 0-.776.492z"/>
                           </svg>
                        </a>
                     </div>
                  </li>
                  <li>
                     <div class="p-2">
                        <a href="update-history?id={{$model->id}}&patientId={{$model->patient()->first()->id}}">
                           {{__('com-form-history.update')}} &nbsp;
                           <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-journals" viewBox="0 0 16 16">
                              <path d="M5 0h8a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2 2 2 0 0 1-2 2H3a2 2 0 0 1-2-2h1a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1H3a1 1 0 0 0-1 1H1a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v9a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1H3a2 2 0 0 1 2-2"/>
                              <path d="M1 6v-.5a.5.5 0 0 1 1 0V6h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1zm0 3v-.5a.5.5 0 0 1 1 0V9h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1zm0 2.5v.5H.5a.5.5 0 0 0 0 1h2a.5.5 0 0 0 0-1H2v-.5a.5.5 0 0 0-1 0"/>
                           </svg>
                        </a>
                     </div>
                  </li>
                  @if(Auth::user()->isAbleTo('create-control-history'))
                  <li>
                     <div class="p-2">
                        <a href="create-control-history?historyId={{$model->id}}">
                           {{__('create-control-history.create')}} &nbsp;
                           <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-journals" viewBox="0 0 16 16">
                              <path d="M5 0h8a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2 2 2 0 0 1-2 2H3a2 2 0 0 1-2-2h1a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1H3a1 1 0 0 0-1 1H1a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v9a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1H3a2 2 0 0 1 2-2"/>
                              <path d="M1 6v-.5a.5.5 0 0 1 1 0V6h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1zm0 3v-.5a.5.5 0 0 1 1 0V9h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1zm0 2.5v.5H.5a.5.5 0 0 0 0 1h2a.5.5 0 0 0 0-1H2v-.5a.5.5 0 0 0-1 0"/>
                           </svg>
                        </a>
                     </div>
                  </li>
                  @endif
                  <li>
                     <div class="p-2">
                        <a href="preview-history?id={{$model->id}}" target="_blank">
                           {{__('detail-history.previewHistory')}} &nbsp;
                           <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-eye-fill" viewBox="0 0 16 16">
                              <path d="M10.5 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0"/>
                              <path d="M0 8s3-5.5 8-5.5S16 8 16 8s-3 5.5-8 5.5S0 8 0 8m8 3.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7"/>
                           </svg>
                        </a>
                     </div>
                  </li>
                  <li>
                     <div class="p-2">
                        <a href="download-history?id={{$model->id}}" download>
                           {{__('detail-history.downloadHistory')}} &nbsp;
                           <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-earmark-arrow-down-fill" viewBox="0 0 16 16">
                              <path d="M9.293 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4.707A1 1 0 0 0 13.707 4L10 .293A1 1 0 0 0 9.293 0M9.5 3.5v-2l3 3h-2a1 1 0 0 1-1-1m-1 4v3.793l1.146-1.147a.5.5 0 0 1 .708.708l-2 2a.5.5 0 0 1-.708 0l-2-2a.5.5 0 0 1 .708-.708L7.5 11.293V7.5a.5.5 0 0 1 1 0"/>
                           </svg>
                        </a>
                     </div>
                  </li>
                  @if(Auth::user()->isAbleTo('create-medical-report'))
                  <li>
                     <div class="p-2">
                        <a href="create-medical-report?patientId={{$model->patient()->first()->id}}">
                           {{__("detail-patient.createMedicalReport")}} &nbsp;
                           <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-earmark-word" viewBox="0 0 16 16">
                              <path d="M5.485 6.879a.5.5 0 1 0-.97.242l1.5 6a.5.5 0 0 0 .967.01L8 9.402l1.018 3.73a.5.5 0 0 0 .967-.01l1.5-6a.5.5 0 0 0-.97-.242l-1.036 4.144-.997-3.655a.5.5 0 0 0-.964 0l-.997 3.655L5.485 6.88z"/>
                              <path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2M9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5z"/>
                           </svg>
                        </a>
                     </div>
                  </li>
                  @endif
               </ul>
            </div>
         </div>
      </div>
   </div>
</div>
<div class="container-fluid">
   <div class="row">
      <div class="col-12">
         <ul class="nav nav-tabs" id="myTabHistoryDetail" role="tablist">
            <li class="nav-item">
               <a class="nav-link active" id="form-history-tab" data-toggle="tab" href="#form-history" role="tab" aria-controls="form-history" aria-selected="true">
               {{__('detail-history.history')}}
               </a>
            </li>
            @if(count($model->getSectionHistory()) > 0)
            <li class="nav-item">
               <a class="nav-link" id="backgrounds-tab" data-toggle="tab" href="#backgrounds" role="tab" aria-controls="backgrounds" aria-selected="false">
               {{__("backgrounds.Background")}}
               </a>
            </li>
            @endif
            @if(count($model->getPhyscalExam()) > 0)
            <li class="nav-item">
               <a class="nav-link" id="test-tab" data-toggle="tab" href="#test" role="tab" aria-controls="test" aria-selected="false">
               {{__("backgrounds.Physical_Test")}}
               </a>
            </li>
            @endif
            <li class="nav-item">
               <a class="nav-link" id="controls-tab" data-toggle="tab" href="#controls" role="tab" aria-controls="controls" aria-selected="false">
               {{__("backgrounds.controls")}}
               </a>
            </li>
            <li class="nav-item">
               <a class="nav-link" id="controls-tab" data-toggle="tab" href="#medicalReports" role="tab" aria-controls="medicalReports" aria-selected="false">
               {{__("detail-patient.MedicalReports")}}
               </a>
            </li>
            <li class="nav-item">
               <a class="nav-link" id="controls-tab" data-toggle="tab" href="#files" role="tab" aria-controls="files" aria-selected="false">
               {{__("backgrounds.Files")}}
               </a>
            </li>
         </ul>
      </div>
   </div>
</div>
<br>
<br>
<div class="tab-content" id="myTabHistoryDetailContent">
   <div class="tab-pane fade show active" id="form-history" role="tabpanel" aria-labelledby="form-history-tab">
      <div class="container-fluid">
         <div class="row">
            <div class="col-12">
               <livewire:history-detail-view :model="$model"/>
            </div>
         </div>
      </div>
   </div>
   <div class="tab-pane fade" id="backgrounds" role="tabpanel" aria-labelledby="backgrounds-tab">
      <div class="container-fluid">
         <div class="row">
            <div class="col-12">
               <ul class="nav nav-tabs" id="myTabBackgrounds" role="tablist">
                  @foreach($model->getSectionHistory() as $index => $sectionHistory)
                  @php
                  $tabId = 'form-history-'.Str::slug($sectionHistory->title);
                  @endphp
                  <li class="nav-item">
                     <a class="nav-link @if($index === 0) active @endif" 
                        id="{{$tabId}}-tab" 
                        data-toggle="tab" 
                        href="#{{$tabId}}" 
                        role="tab" 
                        aria-controls="{{$tabId}}" 
                        aria-selected="{{ $index === 0 ? 'true' : 'false' }}">
                     {{$sectionHistory->title}}  
                     </a>
                  </li>
                  @endforeach
               </ul>
            </div>
         </div>
         <div class="tab-content">
            @foreach($model->getSectionHistory() as $index => $section)
            @php
            $tabId = 'form-history-'.Str::slug($section->title);
            $entries = collect();
            // Entrada principal
            $entries->push([
            'type' => 'main',
            'user' => $model->user()->first(),
            'date' => $model->created_at,
            'content' => $section->summary
            ]);
            // Controles relacionados
            foreach($controlHistories as $control) {
            foreach($control->getControlSectionHistory() as $controlSection) {
            if($section->section_type_id === $controlSection->section_type_id) {
            $entries->push([
            'type' => 'control',
            'user' => $control->user()->first(),
            'date' => $control->created_at,
            'content' => $controlSection->summary,
            'control_data' => $control
            ]);
            }
            }
            }
            @endphp
            <div class="tab-pane fade @if($index === 0) show active @endif" 
               id="{{$tabId}}" 
               role="tabpanel" 
               aria-labelledby="{{$tabId}}-tab">
               <div class="row mt-3">
                  <div class="col-12">
                     <ul class="nav nav-tabs sub-tabs" role="tablist" id="myTabsDoctor">
                        @foreach($entries as $entryIndex => $entry)
                        @php
                        $subTabId = Str::slug($section->title).'-'.Str::slug($entry['user']->name).'-'.$entryIndex;
                        @endphp
                        <li class="nav-item">
                           <a class="nav-link @if($loop->first) active @endif" 
                              id="{{ $subTabId }}-tab" 
                              data-toggle="tab" 
                              href="#{{ $subTabId }}" 
                              role="tab">
                           {{ $entry['user']->name }}
                           <small class="d-block">{{ $entry['date']->format('d/m/Y H:i') }}</small>
                           </a>
                        </li>
                        @endforeach
                     </ul>
                     <div class="tab-content sub-tab-content">
                        @foreach($entries as $entryIndex => $entry)
                        @php
                        $subTabId = Str::slug($section->title).'-'.Str::slug($entry['user']->name).'-'.$entryIndex;
                        @endphp
                        <div class="tab-pane fade @if($loop->first) show active @endif" 
                           id="{{ $subTabId }}" 
                           role="tabpanel">
                           <div class="card mt-2">
                              <div class="card-body">
                                 <div class="row">
                                    <div class="col-8">
                                       <span class="badge bg-primary rounded-pill">
                                       {{ __("detail-history.doctor") }} {{ $entry['user']->name }}
                                       </span>
                                       @if($entry['type'] === 'control')
                                       <span class="badge bg-secondary rounded-pill">
                                       {{ $especialities[$entry['control_data']->especiality] }}
                                       </span>
                                       @endif
                                    </div>
                                    <div class="col-4 text-right">
                                       @if($entry['type'] === 'control')
                                       <span class="badge bg-success rounded-pill">
                                       {{ __("com-form-history.idControl") }} {{ substr($entry['control_data']->id, -4) }}
                                       </span>
                                       @endif
                                    </div>
                                 </div>
                                 <div class="p-3"></div>
                                 <p class="card-text">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-journal-plus" viewBox="0 0 16 16">
                                       <!-- Icono -->
                                    </svg>
                                    {{ $entry['content'] }}
                                 </p>
                              </div>
                           </div>
                        </div>
                        @endforeach
                     </div>
                  </div>
               </div>
            </div>
            @endforeach
         </div>
      </div>
   </div>
   <div class="tab-pane fade" id="test" role="tabpanel" aria-labelledby="test-tab">
      <div class="container-fluid">
         <div class="row">
            <div class="col-12">
               <ul class="nav nav-tabs" id="physcalExamTabs" role="tablist">
                  @foreach($model->getPhyscalExam() as $key => $value)
                  @php
                  $mainTabId = 'physcal-exam-'.Str::slug($key);
                  @endphp
                  <li class="nav-item">
                     <a class="nav-link @if($loop->first) active @endif" 
                        id="{{$mainTabId}}-tab" 
                        data-toggle="tab" 
                        href="#{{$mainTabId}}" 
                        role="tab"
                        aria-controls="{{$mainTabId}}" 
                        aria-selected="{{$loop->first ? 'true' : 'false' }}">
                     {{__("com-form-history.".$key)}}
                     </a>
                  </li>
                  @endforeach
               </ul>
            </div>
         </div>
         <div class="tab-content">
            @foreach($model->getPhyscalExam() as $key => $value)
            @php
            $mainTabId = 'physcal-exam-'.Str::slug($key);
            $entries = collect();
            // Entrada principal
            $entries->push([
            'type' => 'main',
            'user' => $model->user()->first(),
            'date' => $model->created_at,
            'content' => $value
            ]);
            // Controles relacionados
            foreach($controlHistories as $control) {
            $controlExam = $control->getControlPhyscalExam();
            if($controlExam && isset($controlExam[$key])) {
            $entries->push([
            'type' => 'control',
            'user' => $control->user()->first(),
            'date' => $control->created_at,
            'content' => $controlExam[$key],
            'control_data' => $control
            ]);
            }
            }
            @endphp
            <div class="tab-pane fade @if($loop->first) show active @endif" 
               id="{{$mainTabId}}" 
               role="tabpanel" 
               aria-labelledby="{{$mainTabId}}-tab">
               <div class="row mt-3">
                  <div class="col-12">
                     <ul class="nav nav-tabs sub-tabs" id="myTabsDoctorTest" role="tablist">
                        @foreach($entries as $entryIndex => $entry)
                        @php
                        $subTabId = Str::slug($key).'-'.Str::slug($entry['user']->name).'-'.$entryIndex;
                        @endphp
                        <li class="nav-item">
                           <a class="nav-link @if($loop->first) active @endif" 
                              id="{{$subTabId}}-tab" 
                              data-toggle="tab" 
                              href="#{{$subTabId}}" 
                              role="tab">
                           {{ $entry['user']->name }}
                           <small class="d-block">{{ $entry['date']->format('d/m/Y H:i') }}</small>
                           </a>
                        </li>
                        @endforeach
                     </ul>
                     <div class="tab-content sub-tab-content">
                        @foreach($entries as $entryIndex => $entry)
                        @php
                        $subTabId = Str::slug($key).'-'.Str::slug($entry['user']->name).'-'.$entryIndex;
                        @endphp
                        <div class="tab-pane fade @if($loop->first) show active @endif" 
                           id="{{$subTabId}}" 
                           role="tabpanel">
                           <div class="card mt-2">
                              <div class="card-body">
                                 <div class="row">
                                    <div class="col-8">
                                       <span class="badge bg-primary rounded-pill">
                                       {{ __("detail-history.doctor") }} {{ $entry['user']->name }}
                                       </span>
                                       @if($entry['type'] === 'control')
                                       <span class="badge bg-secondary rounded-pill">
                                       {{ $especialities[$entry['control_data']->especiality] }}
                                       </span>
                                       @endif
                                    </div>
                                    <div class="col-4 text-right">
                                       @if($entry['type'] === 'control')
                                       <span class="badge bg-success rounded-pill">
                                       {{ __("com-form-history.idControl") }} {{ substr($entry['control_data']->id, -4) }}
                                       </span>
                                       <span class="badge bg-primary rounded-pill">
                                       {{ $entry['date']->format('d/m/Y H:i') }}
                                       </span>
                                       @endif
                                    </div>
                                 </div>
                                 <div class="p-3"></div>
                                 <p class="card-text">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-journal-plus" viewBox="0 0 16 16">
                                       <path fill-rule="evenodd" d="M8 5.5a.5.5 0 0 1 .5.5v1.5H10a.5.5 0 0 1 0 1H8.5V10a.5.5 0 0 1-1 0V8.5H6a.5.5 0 0 1 0-1h1.5V6a.5.5 0 0 1 .5-.5"/>
                                       <path d="M3 0h10a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2v-1h1v1a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H3a1 1 0 0 0-1 1v1H1V2a2 2 0 0 1 2-2"/>
                                       <path d="M1 5v-.5a.5.5 0 0 1 1 0V5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1zm0 3v-.5a.5.5 0 0 1 1 0V8h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1zm0 3v-.5a.5.5 0 0 1 1 0v.5h.5a.5.5 0 0 1 0 1h-2a.5.5 0 0 1 0-1z"/>
                                    </svg>
                                    {{ $entry['content'] }}
                                 </p>
                              </div>
                           </div>
                        </div>
                        @endforeach
                     </div>
                  </div>
               </div>
            </div>
            @endforeach
         </div>
      </div>
   </div>
   <div class="tab-pane fade" id="controls" role="tabpanel" aria-labelledby="controls-tab">
      <div class="container-fluid">
         <div class="row">
            <div class="col-12 p-5">
               <livewire:control-history-table-view :history="$model" />
            </div>
         </div>
      </div>
   </div>
   <div class="tab-pane fade" id="medicalReports" role="tabpanel" aria-labelledby="medicalReports-tab">
      <div class="container-fluid">
         <div class="row">
            <div class="col-12 p-3">
               <livewire:medical-reports-table-view/>
            </div>
         </div>
      </div>
   </div>
   <div class="tab-pane fade" id="files" role="tabpanel" aria-labelledby="files-tab">
      <div class="container-fluid">
         <div class="row">
            <div class="col-12 p-3">
               <livewire:files-table-view/>
            </div>
         </div>
         <div class="row">
            <div class="col-12 p-3">
               <x-FilesMultipleUpload :route="'/admin/history-create-file'" :extraFields="['history_id'=>$model->id, 'patientId'=>$model->patient()->first()->id]"></x-filesMultipleUpload>
            </div>
         </div>
      </div>
   </div>
</div>
@endsection
@section('javascript') 
<script src="{{ asset('js/detail-history.js') }}" defer></script>
@endsection