@extends('layouts.app')

@section('title', __('bms::lang.dashboard'))

@section('content')

<section class="content-header">
    <h1>
        <i class="fa fas fa-glass-cheers"></i> @lang('bms::lang.bms_module')
        <small>@lang('bms::lang.dashboard')</small>
    </h1>
</section>

<section class="content">

    {{-- KPI cards --}}
    <div class="row">
        @php
            $cards = [
                ['label' => __('bms::lang.leads'),    'value' => $stats['leads'],    'icon' => 'fa-user-plus',     'bg' => 'bg-purple'],
                ['label' => __('bms::lang.bookings'), 'value' => $stats['bookings'], 'icon' => 'fa-calendar-check','bg' => 'bg-red'],
                ['label' => __('bms::lang.upcoming_visits'), 'value' => $stats['upcoming_visits'], 'icon' => 'fa-map-marker-alt', 'bg' => 'bg-aqua'],
                ['label' => __('bms::lang.halls'),    'value' => $stats['halls'],    'icon' => 'fa-archway',       'bg' => 'bg-green'],
                ['label' => __('bms::lang.packages'), 'value' => $stats['packages'], 'icon' => 'fa-box-open',      'bg' => 'bg-yellow'],
                ['label' => __('bms::lang.venues'),   'value' => $stats['venues'],   'icon' => 'fa-building',      'bg' => 'bg-light-blue'],
            ];
        @endphp
        @foreach($cards as $card)
            <div class="col-lg-2 col-md-4 col-sm-6 col-xs-12">
                <div class="small-box {{ $card['bg'] }}">
                    <div class="inner"><h3>{{ $card['value'] }}</h3><p>{{ $card['label'] }}</p></div>
                    <div class="icon"><i class="fa fas {{ $card['icon'] }}"></i></div>
                </div>
            </div>
        @endforeach
    </div>

    {{-- Revenue --}}
    <div class="row">
        <div class="col-md-4">
            <div class="info-box bg-aqua"><span class="info-box-icon"><i class="fa fas fa-file-invoice-dollar"></i></span>
                <div class="info-box-content"><span class="info-box-text">@lang('bms::lang.invoiced')</span>
                    <span class="info-box-number">{{ number_format($revenue['invoiced'], 2) }}</span></div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="info-box bg-green"><span class="info-box-icon"><i class="fa fas fa-hand-holding-usd"></i></span>
                <div class="info-box-content"><span class="info-box-text">@lang('bms::lang.collected')</span>
                    <span class="info-box-number">{{ number_format($revenue['collected'], 2) }}</span>
                    <div class="progress"><div class="progress-bar" style="width: {{ $revenue['collected_pct'] }}%"></div></div>
                    <span class="progress-description">{{ $revenue['collected_pct'] }}% @lang('bms::lang.collected')</span>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="info-box bg-yellow"><span class="info-box-icon"><i class="fa fas fa-clock"></i></span>
                <div class="info-box-content"><span class="info-box-text">@lang('bms::lang.outstanding')</span>
                    <span class="info-box-number">{{ number_format($revenue['due'], 2) }}</span></div>
            </div>
        </div>
    </div>

    <div class="row">
        {{-- Pipeline by stage --}}
        <div class="col-md-7">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.pipeline')])
                @forelse($pipeline as $row)
                    <div style="margin-bottom:12px;">
                        <div style="display:flex;justify-content:space-between;font-size:13px;margin-bottom:4px;">
                            <span>@lang('bms::lang.stage_'.$row['stage'])</span><strong>{{ $row['count'] }}</strong>
                        </div>
                        <div style="background:#eef0f3;border-radius:6px;height:14px;overflow:hidden;">
                            <div style="height:14px;border-radius:6px;background:linear-gradient(90deg,#c5a44f,#a07c33);width:{{ max(4, round($row['count'] / $pipelineMax * 100)) }}%;"></div>
                        </div>
                    </div>
                @empty
                    <p class="text-muted">@lang('bms::lang.no_leads')</p>
                @endforelse
            @endcomponent
        </div>

        {{-- Bookings by month --}}
        <div class="col-md-5">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.bookings_by_month')])
                <div style="display:flex;align-items:flex-end;gap:10px;height:160px;padding-top:10px;">
                    @foreach($months as $m)
                        <div style="flex:1;display:flex;flex-direction:column;align-items:center;justify-content:flex-end;height:100%;">
                            <span style="font-size:12px;font-weight:600;">{{ $m['count'] }}</span>
                            <div title="{{ $m['count'] }}" style="width:100%;border-radius:6px 6px 0 0;background:linear-gradient(180deg,#7a5a86,#5a4063);height:{{ max(3, round($m['count'] / $monthsMax * 130)) }}px;"></div>
                            <span style="font-size:11px;color:#888;margin-top:6px;">{{ $m['label'] }}</span>
                        </div>
                    @endforeach
                </div>
            @endcomponent
        </div>
    </div>

    {{-- Operations + matching --}}
    <div class="row">
        <div class="col-md-7">
            @component('components.widget', ['class' => 'box-solid', 'title' => __('bms::lang.operations')])
                <div class="row">
                    <div class="col-sm-3 col-xs-6 text-center">
                        <h4 style="margin:0;color:#5a7d5a;">{{ $ops['tasks_pct'] }}%</h4>
                        <small class="text-muted">@lang('bms::lang.tasks') ({{ $ops['tasks_done'] }}/{{ $ops['tasks_total'] }})</small>
                    </div>
                    <div class="col-sm-3 col-xs-6 text-center">
                        <h4 style="margin:0;{{ $ops['tasks_overdue'] ? 'color:#b4533f;' : '' }}">{{ $ops['tasks_overdue'] }}</h4>
                        <small class="text-muted">@lang('bms::lang.overdue') @lang('bms::lang.tasks')</small>
                    </div>
                    <div class="col-sm-3 col-xs-6 text-center">
                        <h4 style="margin:0;{{ $ops['maint_overdue'] ? 'color:#b4533f;' : '' }}">{{ $ops['maint_open'] }}</h4>
                        <small class="text-muted">@lang('bms::lang.maintenance') ({{ $ops['maint_overdue'] }} @lang('bms::lang.overdue'))</small>
                    </div>
                    <div class="col-sm-3 col-xs-6 text-center">
                        <h4 style="margin:0;">{{ number_format($ops['assets_value']) }}</h4>
                        <small class="text-muted">@lang('bms::lang.assets') ({{ $ops['assets'] }})</small>
                    </div>
                </div>
            @endcomponent
        </div>

        <div class="col-md-5">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.matching')])
                <p style="margin-bottom:8px;"><strong>@lang('bms::lang.threshold'):</strong> {{ $matching->threshold }}%</p>
                @foreach(($matching->weights ?? []) as $criterion => $weight)
                    <div style="display:flex;justify-content:space-between;font-size:13px;padding:3px 0;border-bottom:1px dashed #eee;">
                        <span>{{ ucwords(str_replace('_', ' ', $criterion)) }}</span><strong>{{ $weight }}</strong>
                    </div>
                @endforeach
            @endcomponent
        </div>
    </div>

</section>

@endsection
