@extends('layouts.app')
@section('title', __('medworkforce::lang.duty_roster'))

@section('content')
<section class="content-header"><h1>@lang('medworkforce::lang.duty_roster')</h1>
    <p>@lang('medworkforce::lang.roster_help')</p>
</section>

<section class="content">
    {{-- Toolbar: week nav + filters + bulk assign --}}
    @component('components.widget', ['class' => 'box-primary'])
        <form method="GET" action="{{ route('medwf.roster.index') }}" class="form-inline" style="margin-bottom:8px;">
            <div class="btn-group" role="group">
                <a class="btn btn-default btn-sm" href="{{ route('medwf.roster.index', array_filter(['week' => $prevWeek, 'department_id' => $departmentId, 'business_location_id' => $locationId])) }}">
                    <i class="fa fa-chevron-left"></i> @lang('medworkforce::lang.prev_week')
                </a>
                <a class="btn btn-default btn-sm" href="{{ route('medwf.roster.index', array_filter(['department_id' => $departmentId, 'business_location_id' => $locationId])) }}">
                    @lang('medworkforce::lang.this_week')
                </a>
                <a class="btn btn-default btn-sm" href="{{ route('medwf.roster.index', array_filter(['week' => $nextWeek, 'department_id' => $departmentId, 'business_location_id' => $locationId])) }}">
                    @lang('medworkforce::lang.next_week') <i class="fa fa-chevron-right"></i>
                </a>
            </div>
            <strong style="margin:0 12px;">{{ __('medworkforce::lang.week_of', ['date' => $monday->format('d M Y')]) }}</strong>

            @if(count($departments))
            <select name="department_id" class="form-control input-sm" style="margin-left:8px;">
                <option value="">@lang('medworkforce::lang.department') — @lang('medworkforce::lang.all')</option>
                @foreach($departments as $id => $name)
                    <option value="{{ $id }}" @selected($departmentId === (int) $id)>{{ $name }}</option>
                @endforeach
            </select>
            @endif
            @if(count($locations))
            <select name="business_location_id" class="form-control input-sm">
                <option value="">@lang('medworkforce::lang.location') — @lang('medworkforce::lang.all')</option>
                @foreach($locations as $id => $name)
                    <option value="{{ $id }}" @selected($locationId === (int) $id)>{{ $name }}</option>
                @endforeach
            </select>
            @endif
            <input type="hidden" name="week" value="{{ $monday->toDateString() }}">
            <button type="submit" class="btn btn-default btn-sm">@lang('medworkforce::lang.apply')</button>

            @if($canManage)
            <button type="button" class="btn btn-primary btn-sm pull-right medwf-modal-btn" data-href="{{ route('medwf.roster.bulk') }}">
                <i class="fa fa-magic"></i> @lang('medworkforce::lang.bulk_assign')
            </button>
            @endif
        </form>

        {{-- Legend --}}
        @if($templates->count())
        <div style="margin-bottom:8px;">
            @foreach($templates as $tmpl)
                <span class="label" style="background:{{ $tmpl->badgeColor() }};margin-right:4px;">
                    {{ $tmpl->name }} ({{ $tmpl->timeLabel() }})
                </span>
            @endforeach
        </div>
        @endif

        {{-- Board --}}
        <div class="table-responsive">
            <table class="table table-bordered" style="min-width:900px;">
                <thead>
                    <tr>
                        <th style="width:180px;">@lang('medworkforce::lang.staff')</th>
                        @foreach($days as $day)
                            <th class="text-center @if($day->isToday()) bg-info @endif">
                                {{ $day->format('D') }}<br><small>{{ $day->format('d/m') }}</small>
                            </th>
                        @endforeach
                    </tr>
                </thead>
                <tbody>
                    @forelse($staff as $userId => $userName)
                        <tr>
                            <td><strong>{{ $userName }}</strong></td>
                            @foreach($days as $day)
                                @php $dateKey = $day->toDateString(); $cells = $grid[$userId][$dateKey] ?? []; @endphp
                                <td class="text-center medwf-cell" style="cursor:{{ $canManage ? 'pointer' : 'default' }};vertical-align:top;"
                                    @if($canManage)
                                    data-href="{{ route('medwf.roster.cell', ['user_id' => $userId, 'date' => $dateKey]) }}"
                                    @endif>
                                    @forelse($cells as $cell)
                                        @php $t = $cell->template; $bg = $t ? $t->badgeColor() : '#777'; @endphp
                                        <div class="medwf-chip @if($canManage) medwf-chip-edit @endif" style="background:{{ $bg }};color:#fff;border-radius:3px;padding:2px 4px;margin:1px 0;font-size:11px;"
                                             @if($canManage)
                                             data-href="{{ route('medwf.roster.cell', ['user_id' => $userId, 'date' => $dateKey, 'assignment_id' => $cell->id]) }}"
                                             @endif
                                             title="{{ $t ? $t->timeLabel() : '' }} · {{ $cell->status }}">
                                            {{ $t ? $t->name : __('medworkforce::lang.custom') }}
                                            @if($cell->status === 'swapped')<i class="fa fa-exchange"></i>@endif
                                            @if($cell->status === 'no_show')<i class="fa fa-user-times"></i>@endif
                                        </div>
                                    @empty
                                        @if($canManage)
                                            <span class="text-muted" style="font-size:11px;">+</span>
                                        @else
                                            <span class="text-muted">—</span>
                                        @endif
                                    @endforelse
                                </td>
                            @endforeach
                        </tr>
                    @empty
                        <tr><td colspan="{{ count($days) + 1 }}" class="text-center text-muted">@lang('medworkforce::lang.no_staff')</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    @endcomponent

    <div class="modal fade medwf_modal" tabindex="-1" role="dialog"></div>
</section>
@endsection

@section('javascript')
@include('medworkforce::partials.crud_scripts')
<script>
$(function () {
    // Reload the board after a save (cell / bulk) so the grid reflects changes.
    window.medWfAfterSave = function () { window.location.reload(); };

    // Clicking an empty cell opens the assign form; clicking a chip opens edit.
    $(document).on('click', '.medwf-chip-edit', function (e) {
        e.stopPropagation();
        var href = $(this).data('href');
        if (!href) { return; }
        $.get(href, function (html) { $('.medwf_modal').html(html).modal('show'); })
         .fail(function () { toastr.error('Could not load the form.'); });
    });
    $(document).on('click', '.medwf-cell', function (e) {
        if ($(e.target).closest('.medwf-chip').length) { return; }
        var href = $(this).data('href');
        if (!href) { return; }
        $.get(href, function (html) { $('.medwf_modal').html(html).modal('show'); })
         .fail(function () { toastr.error('Could not load the form.'); });
    });
});
</script>
@endsection
