@extends('layouts.admin')

@section('title', 'Locations')

@section('content')
@php
    $sortKey = $sortKey ?? 'type_priority';
    $stats = $stats ?? ['total' => 0, 'stores' => 0, 'warehouses' => 0, 'active' => 0];
@endphp

<div class="ci-page">
    <nav class="ci-crumb" aria-label="Breadcrumb">
        <span>Inventory</span>
        <span class="ci-crumb__sep">/</span>
        <span class="ci-crumb__current">Locations</span>
    </nav>

    <div class="ci-header">
        <div>
            <h1>Locations</h1>
            <p class="ci-header__sub">Stores and warehouses used for stock, POS, and online fulfilment.</p>
        </div>
        <div class="ci-header__actions">
            <a href="{{ route('admin.locations.create') }}" class="ci-btn ci-btn--primary">
                <i class="bi bi-plus-lg"></i> Add Location
            </a>
        </div>
    </div>

    <div class="ci-stats">
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Total</p>
                <p class="ci-stat__value">{{ number_format($stats['total']) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-geo-alt"></i></span>
        </div>
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Stores</p>
                <p class="ci-stat__value">{{ number_format($stats['stores']) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-shop"></i></span>
        </div>
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Warehouses</p>
                <p class="ci-stat__value ci-stat__value--warning">{{ number_format($stats['warehouses']) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-building"></i></span>
        </div>
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Active</p>
                <p class="ci-stat__value ci-stat__value--success">{{ number_format($stats['active']) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-check-circle"></i></span>
        </div>
    </div>

    <form method="GET" action="{{ route('admin.locations.index') }}" class="ci-filters">
        <div class="row g-2 align-items-center justify-content-end flex-nowrap">
            <div class="col-lg-2">
                <div class="products-search-wrap">
                    <i class="bi bi-search"></i>
                    <input type="text"
                           name="search"
                           class="admin-form-control form-control"
                           placeholder="Search name, code, or city…"
                           value="{{ request('search') }}">
                </div>
            </div>
            @if(isset($companies) && $companies->count() > 0)
                <div class="col-lg-2">
                    <select name="company_id" onchange="this.form.submit()">
                        <option value="">All companies</option>
                        @foreach($companies as $company)
                            <option value="{{ $company->id }}" @selected((string) request('company_id') === (string) $company->id)>
                                {{ $company->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
            @endif
            <div class="col-lg-2">
                <select name="type" onchange="this.form.submit()">
                    <option value="">All types</option>
                    <option value="store" {{ request('type') === 'store' ? 'selected' : '' }}>Store</option>
                    <option value="warehouse" {{ request('type') === 'warehouse' ? 'selected' : '' }}>Warehouse</option>
                </select>
            </div>
            <div class="col-lg-2">
                <select name="status" onchange="this.form.submit()">
                    <option value="">All status</option>
                    <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>Active</option>
                    <option value="inactive" {{ request('status') === 'inactive' ? 'selected' : '' }}>Inactive</option>
                </select>
            </div>
            <div class="col-lg-2">
                <select name="sort" onchange="this.form.submit()">
                    <option value="type_priority" {{ $sortKey === 'type_priority' ? 'selected' : '' }}>Type / priority</option>
                    <option value="name_asc" {{ $sortKey === 'name_asc' ? 'selected' : '' }}>Name A–Z</option>
                    <option value="name_desc" {{ $sortKey === 'name_desc' ? 'selected' : '' }}>Name Z–A</option>
                    <option value="priority_asc" {{ $sortKey === 'priority_asc' ? 'selected' : '' }}>Priority ↑</option>
                    <option value="updated_at_desc" {{ $sortKey === 'updated_at_desc' ? 'selected' : '' }}>Recently updated</option>
                </select>
            </div>
            <div class="ci-filters__actions col-lg-2">
                <button type="submit" class="ci-btn ci-btn--primary">Filter</button>
                @if(request()->hasAny(['search','company_id','type','status','sort']))
                    <a href="{{ route('admin.locations.index') }}" class="ci-btn ci-btn--ghost ci-btn--icon" title="Clear filters">
                        <i class="bi bi-arrow-clockwise"></i>
                    </a>
                @endif
            </div>
        </div>
        <div class="ci-filters__count ci-filters__count--bottom text-end">
            Showing <strong>{{ $locations->count() }}</strong> of <strong>{{ $locations->total() }}</strong> locations
        </div>
    </form>

    <div class="ci-table-card">
        <div class="ci-table-wrap">
            <table class="ci-table">
                <thead>
                    <tr>
                        <th>Location</th>
                        <th>City</th>
                        <th>Type</th>
                        <th>Status</th>
                        <th>Updated</th>
                        <th style="text-align:right;">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($locations as $location)
                        @php
                            $initials = strtoupper(mb_substr($location->name, 0, 2));
                        @endphp
                        <tr>
                            <td>
                                <div class="ci-entity">
                                    <span class="ci-avatar">{{ $initials }}</span>
                                    <div class="ci-entity__meta">
                                        <p class="ci-entity__name">
                                            {{ $location->name }}
                                            @if($location->is_default)
                                                <span class="ci-pill ci-pill--count" style="margin-left:0.35rem;">Default</span>
                                            @endif
                                        </p>
                                        <p class="ci-entity__sub">
                                            <code class="ci-code">{{ $location->code }}</code>
                                            @if($location->company)
                                                · {{ $location->company->name }}
                                            @endif
                                        </p>
                                    </div>
                                </div>
                            </td>
                            <td class="ci-muted">
                                {{ $location->city ?: '—' }}
                                @if($location->pincode)
                                    <span class="ci-entity__sub" style="display:block;">{{ $location->pincode }}</span>
                                @endif
                            </td>
                            <td>
                                <span class="ci-pill {{ $location->type === 'warehouse' ? 'ci-pill--type-warehouse' : 'ci-pill--type' }}">
                                    {{ ucfirst($location->type) }}
                                </span>
                            </td>
                            <td>
                                <div class="ci-chips">
                                    <span class="ci-pill {{ $location->is_active ? 'ci-pill--active' : 'ci-pill--inactive' }}">
                                        {{ $location->is_active ? 'Active' : 'Inactive' }}
                                    </span>
                                    @if($location->fulfills_online)
                                        <span class="ci-pill ci-pill--count">Online</span>
                                    @endif
                                    @if($location->allows_pickup)
                                        <span class="ci-pill ci-pill--chip">Pickup</span>
                                    @endif
                                </div>
                            </td>
                            <td class="ci-muted">
                                {{ optional($location->updated_at)->format('M d, Y') ?? '—' }}
                            </td>
                            <td>
                                <div class="ci-actions">
                                    <a href="{{ route('admin.locations.edit', $location) }}"
                                       class="ci-btn ci-btn--ghost ci-btn--icon"
                                       title="Edit">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="{{ route('admin.inventory.balances', ['location_id' => $location->id]) }}"
                                       class="ci-btn ci-btn--ghost"
                                       style="height:2.25rem;padding:0 0.75rem;"
                                       title="Stock balances">
                                        <i class="bi bi-boxes"></i> Stock
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6">
                                <div class="ci-empty">
                                    <i class="bi bi-geo-alt"></i>
                                    <h3>No Locations Found</h3>
                                    <p>Create a store or warehouse to manage inventory.</p>
                                    <a href="{{ route('admin.locations.create') }}" class="ci-btn ci-btn--primary">
                                        <i class="bi bi-plus-lg"></i> Add Location
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($locations->hasPages())
        <div class="ci-pagination">
            <div class="ci-pagination__info">
                Showing <strong>{{ $locations->firstItem() ?? 0 }}</strong>–<strong>{{ $locations->lastItem() ?? 0 }}</strong>
                of <strong>{{ $locations->total() }}</strong>
            </div>
            <div>
                {{ $locations->withQueryString()->links('vendor.pagination.admin-categories') }}
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
