@extends('layouts.app')
@section('title', __('medbloodbank::lang.blood_bank'))
@section('content')
<section class="content-header">
    <h1><i class="fa fa-tint text-danger"></i> @lang('medbloodbank::lang.blood_bank')</h1>
    <p>@lang('medbloodbank::lang.dashboard_help')</p>
</section>

<section class="content">
    {{-- ── Stock grid: blood group × product type ─────────────────────── --}}
    @component('components.widget', ['class' => 'box-primary', 'title' => __('medbloodbank::lang.stock_by_group')])
        @if(auth()->user()->can('mbb.donations.manage'))
        <div style="margin-bottom:12px;">
            <button class="btn btn-default btn-sm mbb-action-btn" data-href="{{ route('mbb.sweep') }}"
                data-confirm="{{ __('medbloodbank::lang.sweep_confirm') }}">
                <i class="fa fa-clock"></i> @lang('medbloodbank::lang.run_expiry_sweep')
            </button>
            <button class="btn btn-default btn-sm mbb-action-btn" data-href="{{ route('mbb.notify_low') }}">
                <i class="fa fa-bell"></i> @lang('medbloodbank::lang.notify_low_stock')
            </button>
        </div>
        @endif

        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>@lang('medbloodbank::lang.blood_group')</th>
                        @foreach($stock['products'] as $pt)
                            <th class="text-center">@lang('medbloodbank::lang.product_' . $pt)</th>
                        @endforeach
                        <th class="text-center">@lang('medbloodbank::lang.total')</th>
                        <th class="text-center">@lang('medbloodbank::lang.min')</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($stock['groups'] as $group => $data)
                        <tr class="{{ $data['low'] ? 'danger' : '' }}">
                            <td><strong>{{ $group }}</strong>
                                @if($data['low'])
                                    <span class="label label-danger" title="@lang('medbloodbank::lang.low_stock')">
                                        <i class="fa fa-exclamation-triangle"></i>
                                    </span>
                                @endif
                            </td>
                            @foreach($stock['products'] as $pt)
                                <td class="text-center">{{ $data['products'][$pt] ?: '—' }}</td>
                            @endforeach
                            <td class="text-center"><strong>{{ $data['total'] }}</strong></td>
                            <td class="text-center text-muted">{{ $data['min'] }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @if(!empty($stock['low_groups']))
            <div class="alert alert-warning" style="margin-top:8px;">
                <i class="fa fa-exclamation-triangle"></i>
                @lang('medbloodbank::lang.low_stock_groups', ['groups' => implode(', ', $stock['low_groups'])])
            </div>
        @endif
    @endcomponent

    {{-- ── Expiring soon: 7 / 15 / 30-day ladder ──────────────────────── --}}
    @component('components.widget', ['class' => 'box-warning', 'title' => __('medbloodbank::lang.expiring_soon')])
        @php
            sort($ladder);
            $rungLabel = function ($daysLeft) use ($ladder) {
                foreach ($ladder as $rung) { if ($daysLeft <= $rung) { return $rung; } }
                return end($ladder);
            };
        @endphp
        @if($expiringSoon->isEmpty())
            <p class="text-muted">@lang('medbloodbank::lang.nothing_expiring')</p>
        @else
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead><tr>
                    <th>@lang('medbloodbank::lang.bag_no')</th>
                    <th>@lang('medbloodbank::lang.blood_group')</th>
                    <th>@lang('medbloodbank::lang.product')</th>
                    <th>@lang('medbloodbank::lang.expiry_date')</th>
                    <th>@lang('medbloodbank::lang.days_left')</th>
                </tr></thead>
                <tbody>
                    @foreach($expiringSoon as $bag)
                        @php $daysLeft = $today->diffInDays($bag->expiry_date, false); @endphp
                        <tr>
                            <td>{{ $bag->bag_no }}</td>
                            <td><strong>{{ $bag->blood_group }}</strong></td>
                            <td>@lang('medbloodbank::lang.product_' . $bag->product_type)</td>
                            <td>{{ $bag->expiry_date->toDateString() }}</td>
                            <td>
                                <span class="label label-{{ $daysLeft <= 7 ? 'danger' : ($daysLeft <= 15 ? 'warning' : 'info') }}">
                                    {{ max(0, $daysLeft) }} @lang('medbloodbank::lang.days')
                                    (≤{{ $rungLabel($daysLeft) }}d)
                                </span>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @endif
    @endcomponent

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

@section('javascript')
@include('medbloodbank::partials.crud_scripts')
@endsection
