@extends('layouts.app')
@section('title', __('mednotify::lang.my_preferences'))
@section('content')
<section class="content-header"><h1>@lang('mednotify::lang.my_preferences')</h1>
    <p>@lang('mednotify::lang.preferences_help')</p>
</section>
<section class="content">
    {!! Form::open(['route' => 'mednotify.preferences.update', 'method' => 'PUT']) !!}

    @component('components.widget', ['class' => 'box-primary', 'title' => __('mednotify::lang.delivery_options')])
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    {!! Form::label('digest', __('mednotify::lang.digest') . ':') !!}
                    {!! Form::select('digest', [
                        'immediate' => __('mednotify::lang.digest_immediate'),
                        'hourly'    => __('mednotify::lang.digest_hourly'),
                        'daily'     => __('mednotify::lang.digest_daily'),
                    ], $global->digest ?? 'immediate', ['class' => 'form-control']) !!}
                    <span class="help-block">@lang('mednotify::lang.digest_help')</span>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    {!! Form::label('quiet_from', __('mednotify::lang.quiet_from') . ':') !!}
                    {!! Form::time('quiet_from', $global->quiet_from ? \Carbon\Carbon::parse($global->quiet_from)->format('H:i') : null, ['class' => 'form-control']) !!}
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    {!! Form::label('quiet_to', __('mednotify::lang.quiet_to') . ':') !!}
                    {!! Form::time('quiet_to', $global->quiet_to ? \Carbon\Carbon::parse($global->quiet_to)->format('H:i') : null, ['class' => 'form-control']) !!}
                    <span class="help-block">@lang('mednotify::lang.quiet_help')</span>
                </div>
            </div>
        </div>
    @endcomponent

    @component('components.widget', ['class' => 'box-primary', 'title' => __('mednotify::lang.per_event_channel')])
        <p class="help-block">@lang('mednotify::lang.matrix_help')</p>
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead><tr>
                    <th>@lang('mednotify::lang.event')</th>
                    @foreach($channels as $channel)
                        <th class="text-center">@lang('mednotify::lang.channel_' . $channel)</th>
                    @endforeach
                </tr></thead>
                <tbody>
                @forelse($events as $event)
                    <tr>
                        <td>{{ $event->label }}<br><small class="text-muted"><code>{{ $event->event_key }}</code></small></td>
                        @foreach($channels as $channel)
                            @php
                                $row = $rows[$event->event_key . '|' . $channel] ?? null;
                                // Default ON unless an explicit disabled row exists.
                                $isChecked = $row ? (bool) $row->enabled : true;
                            @endphp
                            <td class="text-center">
                                <input type="checkbox"
                                    name="matrix[{{ $event->event_key }}][{{ $channel }}]"
                                    value="1" {{ $isChecked ? 'checked' : '' }}>
                            </td>
                        @endforeach
                    </tr>
                @empty
                    <tr><td colspan="{{ count($channels) + 1 }}" class="text-center text-muted">@lang('mednotify::lang.no_events')</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    @endcomponent

    <div class="form-group">
        <button type="submit" class="btn btn-primary">@lang('messages.save')</button>
    </div>

    {!! Form::close() !!}
</section>
@endsection
