@extends('layouts.app')

@section('title', __('medregistry::lang.module_instances'))

@section('content')
<section class="content-header">
    <h1>@lang('medregistry::lang.module_instances')
        <small>@lang('medregistry::lang.instances_subtitle')</small>
    </h1>
</section>

<section class="content">

    {{-- Tenant × location matrix --}}
    @component('components.widget', ['class' => 'box-primary', 'title' => __('medregistry::lang.instances_matrix')])
        <p class="help-block">@lang('medregistry::lang.instances_matrix_help')</p>
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>@lang('medregistry::lang.module')</th>
                        <th>@lang('medregistry::lang.scope_tenant')</th>
                        @foreach($locations as $locId => $locName)
                            <th>{{ $locName }}</th>
                        @endforeach
                    </tr>
                </thead>
                <tbody>
                    @foreach($modules as $module)
                        <tr>
                            <td>
                                <strong>{{ $module->display_name }}</strong>
                                <br>
                                <small class="text-muted">
                                    @lang('medregistry::lang.category_' . $module->category)
                                    @if($module->spec_key) · {{ $module->spec_key }} @endif
                                </small>
                            </td>

                            {{-- tenant cell + location cells share one renderer --}}
                            @php
                                $cells = ['tenant' => null] + collect($locations)->mapWithKeys(fn ($n, $id) => ['loc:' . $id => $id])->all();
                            @endphp
                            @foreach($cells as $scopeKey => $locId)
                                @php
                                    $instance = $instances[$module->id][$scopeKey] ?? null;
                                    $status = $instance->status ?? null;
                                @endphp
                                <td>
                                    @if($status === 'enabled')
                                        <span class="label label-success">@lang('medregistry::lang.status_enabled')</span>
                                    @elseif($status === 'disabled')
                                        <span class="label label-danger">@lang('medregistry::lang.status_disabled')</span>
                                    @elseif($status === 'soft_locked')
                                        <span class="label label-warning">@lang('medregistry::lang.status_soft_locked')</span>
                                    @elseif($status === 'installed')
                                        <span class="label label-info">@lang('medregistry::lang.status_installed')</span>
                                    @else
                                        <span class="label label-default"
                                              title="@lang('medregistry::lang.default_status_help')">
                                            @if(in_array($module->category, ['medical', 'specialty', 'facility']))
                                                @lang('medregistry::lang.default_disabled')
                                            @else
                                                @lang('medregistry::lang.default_enabled')
                                            @endif
                                        </span>
                                    @endif

                                    <div style="margin-top:4px; white-space:nowrap;">
                                        <form method="post" action="{{ route('medreg.instances.set') }}" style="display:inline;">
                                            @csrf
                                            <input type="hidden" name="module_id" value="{{ $module->id }}">
                                            <input type="hidden" name="location_id" value="{{ $locId }}">
                                            <button type="submit" name="action" value="enable" class="btn btn-xs btn-success" title="@lang('medregistry::lang.enable')"><i class="fa fa-check"></i></button>
                                            <button type="submit" name="action" value="disable" class="btn btn-xs btn-danger" title="@lang('medregistry::lang.disable')"><i class="fa fa-ban"></i></button>
                                            <button type="submit" name="action" value="soft_lock" class="btn btn-xs btn-warning" title="@lang('medregistry::lang.soft_lock')"><i class="fa fa-lock"></i></button>
                                            @if($instance)
                                                <button type="submit" name="action" value="uninstall" class="btn btn-xs btn-default" title="@lang('medregistry::lang.uninstall')"><i class="fa fa-trash"></i></button>
                                            @endif
                                        </form>
                                        @if($instance && !empty($module->settings_schema))
                                            <a href="{{ route('medreg.instances.settings.edit', $instance->id) }}"
                                               class="btn btn-xs btn-primary" title="@lang('medregistry::lang.settings')">
                                                <i class="fa fa-cog"></i>
                                            </a>
                                        @endif
                                    </div>
                                </td>
                            @endforeach
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    @endcomponent

    {{-- Department-scoped instances --}}
    @if(!empty($departments))
        @component('components.widget', ['class' => 'box-info', 'title' => __('medregistry::lang.department_scope')])
            <p class="help-block">@lang('medregistry::lang.department_scope_help')</p>
            <form method="post" action="{{ route('medreg.instances.set') }}" class="form-inline">
                @csrf
                <div class="form-group">
                    <select name="module_id" class="form-control" required>
                        <option value="">@lang('medregistry::lang.module')…</option>
                        @foreach($modules as $module)
                            <option value="{{ $module->id }}">{{ $module->display_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <select name="department_id" class="form-control" required>
                        <option value="">@lang('medregistry::lang.department')…</option>
                        @foreach($departments as $deptId => $deptName)
                            <option value="{{ $deptId }}">{{ $deptName }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group">
                    <select name="action" class="form-control" required>
                        <option value="enable">@lang('medregistry::lang.enable')</option>
                        <option value="disable">@lang('medregistry::lang.disable')</option>
                        <option value="soft_lock">@lang('medregistry::lang.soft_lock')</option>
                        <option value="uninstall">@lang('medregistry::lang.uninstall')</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">@lang('medregistry::lang.apply')</button>
            </form>

            @php
                $deptInstances = collect($instances)->flatMap(fn ($byScope) => collect($byScope)
                    ->filter(fn ($i, $scope) => strpos($scope, 'dept:') === 0));
            @endphp
            @if($deptInstances->isNotEmpty())
                <table class="table table-condensed" style="margin-top:15px;">
                    <thead>
                        <tr>
                            <th>@lang('medregistry::lang.module')</th>
                            <th>@lang('medregistry::lang.department')</th>
                            <th>@lang('medregistry::lang.status')</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($deptInstances as $instance)
                            <tr>
                                <td>{{ optional($modules->firstWhere('id', $instance->module_id))->display_name }}</td>
                                <td>{{ $departments[$instance->department_id] ?? ('#' . $instance->department_id) }}</td>
                                <td><span class="label label-default">@lang('medregistry::lang.status_' . $instance->status)</span></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            @endif
        @endcomponent
    @endif
</section>
@endsection
