@extends('layouts.admin')

@section('title', 'Dashboard')

@section('content')
@php
    $tab = $tab ?? 'store';
    $modes = $dashboardModes ?? ['store'];
    $tabMeta = [
        'store' => [
            'title' => 'Store',
            'hint' => 'Sales & floor',
            'icon' => 'bi-shop',
        ],
        'warehouse' => [
            'title' => 'Warehouse',
            'hint' => 'Stock & transfers',
            'icon' => 'bi bi-house-gear',
        ],
        'overview' => [
            'title' => 'Overview',
            'hint' => 'All locations',
            'icon' => 'bi-grid-1x2',
        ],
    ];
    $tabSubtitles = [
        'store' => 'Store sales — '.($dashboardLocationLabel ?? 'All stores'),
        'warehouse' => 'Warehouse ops — '.($dashboardLocationLabel ?? 'All warehouses'),
        'overview' => 'Network overview — all locations',
    ];
@endphp

<div class="tw-migrated tw-dashboard space-y-3">
    <x-ui.page-header
        title="Dashboard"
        :subtitle="$tabSubtitles[$tab] ?? 'Dashboard'"
    >
        <x-slot:actions>
            @if($tab !== 'overview' && isset($locations) && $locations->count())
                <form method="get" action="{{ route('admin.dashboard') }}" class="dash-shop-form inline-flex items-center gap-1.5 min-w-0 flex-1 sm:flex-none">
                    <input type="hidden" name="tab" value="{{ $tab }}">
                    <label for="dashboardLocation" class="text-[10px] font-semibold uppercase tracking-wide text-[var(--color-text-muted)] shrink-0">
                        {{ $tab === 'warehouse' ? 'Warehouse' : 'Store' }}
                    </label>
                    <select
                        name="location_id"
                        id="dashboardLocation"
                        class="min-w-0 flex-1 sm:flex-none rounded-md border border-[var(--color-border)] bg-[var(--color-card)] px-2 py-1.5 text-xs text-[var(--color-text)]"
                        onchange="this.form.submit()"
                        style="width: 200px;"
                    >
                        @if(!empty($canViewAllShops))
                            <option value="all" @selected(($locationId ?? null) === null)>
                                {{ $tab === 'warehouse' ? 'All warehouses' : 'All stores' }}
                            </option>
                        @endif
                        @foreach($locations as $loc)
                            <option value="{{ $loc->id }}" @selected(($locationId ?? null) == $loc->id)>
                                {{ $loc->name }}
                            </option>
                        @endforeach
                    </select>
                </form>
            @endif
            <span class="dash-updated inline-flex items-center gap-1 rounded-full border border-[var(--color-border)] bg-[var(--color-card)] px-2 py-0.5 text-[10px] text-[var(--color-text-muted)] shrink-0" style="padding: 10px;font-size: 12px;">
                <span class="h-1.5 w-1.5 rounded-full bg-[var(--color-success)]"></span>
                {{ now()->format('M d, h:i A') }}
            </span>
            @if($tab === 'store')
                @can('pos_access')
                    <a href="{{ route('admin.pos.index') }}" target="_blank" class="tw-btn-primary text-xs px-2.5 py-1.5 shrink-0">
                        <i class="bi bi-cash-stack"></i> POS
                    </a>
                @endcan
            @endif
        </x-slot:actions>
    </x-ui.page-header>

    @if(count($modes) > 1)
        <nav class="dash-mode" aria-label="Dashboard mode">
            <div class="dash-mode__track" role="tablist">
                @foreach($modes as $mode)
                    @php $meta = $tabMeta[$mode] ?? ['title' => ucfirst($mode), 'hint' => '', 'icon' => 'bi-circle']; @endphp
                    <a
                        href="{{ route('admin.dashboard', ['tab' => $mode]) }}"
                        role="tab"
                        aria-selected="{{ $tab === $mode ? 'true' : 'false' }}"
                        class="dash-mode__tab dash-mode__tab--{{ $mode }} {{ $tab === $mode ? 'is-active' : '' }}"
                    >
                        <span class="dash-mode__icon" aria-hidden="true">
                            <i class="bi {{ $meta['icon'] }}"></i>
                        </span>
                        <span class="dash-mode__copy">
                            <span class="dash-mode__title">{{ $meta['title'] }}</span>
                            @if(!empty($meta['hint']))
                                <span class="dash-mode__hint">{{ $meta['hint'] }}</span>
                            @endif
                        </span>
                        @if($tab === $mode)
                            <span class="dash-mode__check" aria-hidden="true"><i class="bi bi-check2"></i></span>
                        @endif
                    </a>
                @endforeach
            </div>
        </nav>
    @endif

    @include('admin.dashboard.partials.'.$tab)
</div>
@endsection
