@canany(['dashboard.view-kpis', 'dashboard.overview.view'])
@php
    $totalWhatsAppCount = $whatsApp->total_credits ?? 0;
    $usedWhatsAppCount = $whatsApp->used_credits ?? 0;
    $RemainingWhatsAppCount = max($totalWhatsAppCount - $usedWhatsAppCount, 0);
@endphp

<div class="grid gap-2 grid-cols-2 md:grid-cols-4 xl:grid-cols-8">
    <x-ui.stat-card compact tone="primary" label="Today Sales" :value="'₹'.number_format($todaySales ?? 0, 0)" :hint="($todayOrders ?? 0).' orders'" icon="bi bi-cart-check" />
    <x-ui.stat-card compact tone="primary" label="Revenue MTD" :value="'₹'.number_format($monthSales ?? 0, 0)" hint="All locations" icon="bi bi-cash-coin" />
    <x-ui.stat-card compact tone="warning" label="Placed" :value="(string) ($pendingOrders ?? 0)" hint="Needs action" icon="bi bi-clock-history" />
    <x-ui.stat-card compact tone="info" label="Locations" :value="(string) ($locationCount ?? 0)" hint="Active access" icon="bi bi-geo-alt" />
    <x-ui.stat-card compact tone="muted" label="Store stock" :value="number_format($storeStockUnits ?? 0)" hint="Units" icon="bi bi-shop" />
    <x-ui.stat-card compact tone="muted" label="WH stock" :value="number_format($warehouseStockUnits ?? 0)" hint="Units" icon="bi bi-building" />
    <x-ui.stat-card compact tone="danger" label="Open transfers" :value="(string) ($openTransfers ?? 0)" hint="Network" icon="bi bi-arrow-left-right" />
    <x-ui.stat-card compact tone="info" label="Customers" :value="number_format($totalCustomers ?? 0)" hint="Accounts" icon="bi bi-people" />
</div>

<div class="grid gap-2 grid-cols-2 md:grid-cols-4">
    <x-ui.stat-card compact tone="warning" label="Products" :value="number_format($totalProducts ?? 0)" :hint="($activeProducts ?? 0).' active'" icon="bi bi-box-seam" />
    <a href="{{ route('admin.whatsapp.credits') }}" target="_blank" class="block no-underline">
        <x-ui.stat-card compact tone="info" label="WhatsApp" :value="number_format($RemainingWhatsAppCount)" :hint="'Used '.$usedWhatsAppCount" icon="bi bi-whatsapp" />
    </a>
    <a href="{{ $smsUrl ?? '#' }}" target="_blank" class="block no-underline">
        <x-ui.stat-card compact tone="muted" label="SMS" :value="number_format($SMScount ?? 0)" :hint="'Wallet ₹'.($SmsWallet ?? 0)" icon="bi bi-envelope" />
    </a>
    @can('reports.location-ops')
        <a href="{{ route('admin.reports.location-ops') }}" class="block no-underline">
            <x-ui.stat-card compact tone="primary" label="Location ops" value="Report" hint="Drill down" icon="bi bi-graph-up" />
        </a>
    @endcan
</div>
@endcanany

<div class="dash-panel">
    <div class="dash-panel__head">
        <h3 class="dash-panel__title">Locations today</h3>
    </div>
    <div class="dash-panel__body p-0">
        @if(isset($locationCards) && $locationCards->count())
            <div class="overflow-x-auto">
                <table class="dash-table">
                    <thead>
                        <tr>
                            <th>Location</th>
                            <th>Type</th>
                            <th>Sales today</th>
                            <th>Orders</th>
                            <th>On hand</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($locationCards as $card)
                            <tr>
                                <td class="font-medium">{{ $card->name }}</td>
                                <td class="text-center">
                                    <span class="dash-chip {{ $card->type === 'warehouse' ? 'dash-chip--ok' : '' }}">
                                        {{ $card->type === 'warehouse' ? 'Warehouse' : 'Store' }}
                                    </span>
                                </td>
                                <td class="text-center">₹{{ number_format($card->sales_today, 0) }}</td>
                                <td class="text-center">{{ $card->orders_today }}</td>
                                <td class="text-center">{{ number_format($card->on_hand) }}</td>
                                <td class="text-center">
                                    <a
                                        href="{{ route('admin.dashboard', [
                                            'tab' => $card->type === 'warehouse' ? 'warehouse' : 'store',
                                            'location_id' => $card->id,
                                        ]) }}"
                                        class="tw-btn-outline tw-btn-sm"
                                    >Open</a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <p class="mb-0 px-3 py-4 text-center text-xs text-[var(--color-text-muted)]">No locations</p>
        @endif
    </div>
</div>
