@extends('layouts.app')
@section('title', __('medsurgery::lang.or_board'))
@section('content')
<section class="content-header"><h1>@lang('medsurgery::lang.or_board')</h1>
    <p>@lang('medsurgery::lang.board_help')</p>
</section>
<section class="content">
  @component('components.widget', ['class' => 'box-primary'])
    {{-- Toolbar --}}
    <div class="row" style="margin-bottom:12px;">
      <div class="col-md-6">
        <div class="btn-group btn-group-sm">
          <a class="btn {{ $viewMode === 'week' ? 'btn-primary' : 'btn-default' }}"
             href="{{ route('medsx.board.index', ['view' => 'week', 'date' => $anchor->toDateString()]) }}">@lang('medsurgery::lang.week_view')</a>
          <a class="btn {{ $viewMode === 'day' ? 'btn-primary' : 'btn-default' }}"
             href="{{ route('medsx.board.index', ['view' => 'day', 'date' => $anchor->toDateString()]) }}">@lang('medsurgery::lang.day_view')</a>
        </div>
      </div>
      <div class="col-md-6 text-right">
        <a class="btn btn-default btn-sm" href="{{ route('medsx.board.index', ['view' => $viewMode, 'date' => $prevDate]) }}"><i class="fa fa-chevron-left"></i> @lang('medsurgery::lang.prev')</a>
        <a class="btn btn-default btn-sm" href="{{ route('medsx.board.index', ['view' => $viewMode]) }}">@lang('medsurgery::lang.today')</a>
        <a class="btn btn-default btn-sm" href="{{ route('medsx.board.index', ['view' => $viewMode, 'date' => $nextDate]) }}">@lang('medsurgery::lang.next') <i class="fa fa-chevron-right"></i></a>
      </div>
    </div>

    @if(empty($rooms))
      <div class="callout callout-warning">
        <i class="fa fa-info-circle"></i>
        @if($orgInstalled)
          @lang('medsurgery::lang.no_or_rooms_board')
        @else
          @lang('medsurgery::lang.no_or_rooms_hint')
        @endif
      </div>
    @else
      <div class="table-responsive">
        <table class="table table-bordered" style="table-layout:fixed;">
          <thead>
            <tr>
              <th style="width:140px;">@lang('medsurgery::lang.or_room')</th>
              @foreach($days as $day)
                <th class="text-center">{{ $day->format('D') }}<br><small>{{ $day->format('M j') }}</small></th>
              @endforeach
            </tr>
          </thead>
          <tbody>
            @foreach($rooms as $room)
              <tr>
                <td><strong>{{ $room['name'] }}</strong></td>
                @foreach($days as $day)
                  @php
                    $dayKey = $day->format('Y-m-d');
                    $blocks = $grid[$room['id']][$dayKey] ?? [];
                  @endphp
                  <td style="vertical-align:top;">
                    @forelse($blocks as $b)
                      <a href="{{ route('medsx.cases.show', $b->id) }}"
                         class="label {{ isset($conflictIds[$b->id]) ? 'label-danger' : 'label-info' }}"
                         style="display:block; margin-bottom:4px; text-align:left; white-space:normal;"
                         @if(isset($conflictIds[$b->id])) title="@lang('medsurgery::lang.conflict')" @endif>
                        <strong>{{ \Carbon\Carbon::parse($b->scheduled_start)->format('H:i') }}</strong>
                        @if($b->scheduled_end)–{{ \Carbon\Carbon::parse($b->scheduled_end)->format('H:i') }}@endif
                        <br>{{ $b->case_no }}
                        <br><small>{{ $patientNames[$b->patient_id] ?? ('Patient #' . $b->patient_id) }}</small>
                        @if(isset($conflictIds[$b->id])) <i class="fa fa-exclamation-triangle"></i> @endif
                      </a>
                    @empty
                      <span class="text-muted"><small>—</small></span>
                    @endforelse
                  </td>
                @endforeach
              </tr>
            @endforeach
          </tbody>
        </table>
      </div>
      <p class="text-muted"><small><span class="label label-danger">&nbsp;</span> @lang('medsurgery::lang.conflict')</small></p>
    @endif
  @endcomponent
</section>
@endsection
