@extends('layouts.app')

@section('title', 'Warehouses')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">Warehouse Registry</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Warehouses</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('warehouses.create') }}" class="btn btn-primary px-4">
            <i class="bi bi-plus-lg me-2"></i> Register Warehouse
        </a>
    </div>

    <div class="row g-4">
        @forelse($warehouses as $warehouse)
            <div class="col-md-6 col-lg-4">
                <div class="card border-0 shadow-sm h-100 overflow-hidden">
                    <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center border-0">
                        <span
                            class="badge bg-{{ $warehouse->type == 'scrap' ? 'danger' : ($warehouse->type == 'returns' ? 'warning' : 'primary') }}-subtle text-{{ $warehouse->type == 'scrap' ? 'danger' : ($warehouse->type == 'returns' ? 'warning' : 'primary') }} border">
                            {{ strtoupper(str_replace('_', ' ', $warehouse->type)) }}
                        </span>
                        <div class="dropdown">
                            <button class="btn btn-link link-dark p-0" data-bs-toggle="dropdown">
                                <i class="bi bi-three-dots-vertical"></i>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0">
                                <li><a class="dropdown-item" href="{{ route('warehouses.show', $warehouse->id) }}"><i
                                            class="bi bi-eye me-2"></i> View Stock</a></li>
                                <li><a class="dropdown-item" href="{{ route('warehouses.edit', $warehouse->id) }}"><i
                                            class="bi bi-pencil me-2"></i> Edit Details</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li>
                                    <form action="{{ route('warehouses.destroy', $warehouse->id) }}" method="POST"
                                        onsubmit="return confirm('Archive this warehouse?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="dropdown-item text-danger"><i
                                                class="bi bi-archive me-2"></i> Deactivate</button>
                                    </form>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="card-body">
                        <h5 class="fw-bold mb-1">{{ $warehouse->name }}</h5>
                        <p class="text-muted small mb-3"><i class="bi bi-geo-alt me-1"></i>
                            {{ $warehouse->location ?? 'No location assigned' }}</p>

                        <div class="d-flex align-items-center justify-content-between p-3 bg-light rounded-3">
                            <div>
                                <div class="smallest text-muted text-uppercase fw-bold">Stock Items</div>
                                <div class="fs-5 fw-bold">{{ $warehouse->inventories_count }}</div>
                            </div>
                            <div class="text-end">
                                <div class="smallest text-muted text-uppercase fw-bold">Status</div>
                                <span
                                    class="badge bg-{{ $warehouse->status ? 'success' : 'secondary' }}-subtle text-{{ $warehouse->status ? 'success' : 'secondary' }}">
                                    {{ $warehouse->status ? 'Operational' : 'Inactive' }}
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer bg-white border-0 text-center pb-3">
                        <a href="{{ route('warehouses.show', $warehouse->id) }}"
                            class="btn btn-outline-primary btn-sm w-100 rounded-pill">Manage Inventory</a>
                    </div>
                </div>
            </div>
        @empty
            <div class="col-12">
                <div class="card border-0 shadow-sm py-5 text-center">
                    <div class="text-muted opacity-25 mb-3">
                        <i class="bi bi-house-door display-1"></i>
                    </div>
                    <h5>No warehouses registered.</h5>
                    <p class="text-muted">Start by adding your first storage location.</p>
                    <a href="{{ route('warehouses.create') }}" class="btn btn-primary d-inline-block mt-2">Add Warehouse</a>
                </div>
            </div>
        @endforelse
    </div>

    <div class="mt-4">
        {{ $warehouses->links() }}
    </div>

    <style>
        .smallest {
            font-size: 0.65rem;
        }
    </style>
@endsection