@extends('layouts.app')

@section('title', 'Equipment Management')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3 mb-0">Equipment Management</h1>
                <a href="{{ route('equipment.create') }}" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> Add Equipment
                </a>
            </div>

            <!-- Filters -->
            <div class="card mb-4">
                <div class="card-body">
                    <form method="GET" action="{{ route('equipment.index') }}" class="row g-3">
                        <div class="col-md-3">
                            <label for="type" class="form-label">Equipment Type</label>
                            <select name="type" id="type" class="form-select">
                                <option value="">All Types</option>
                                @foreach($types as $key => $name)
                                    <option value="{{ $key }}" {{ request('type') == $key ? 'selected' : '' }}>
                                        {{ $name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label for="status" class="form-label">Status</label>
                            <select name="status" id="status" class="form-select">
                                <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-md-4">
                            <label for="search" class="form-label">Search</label>
                            <input type="text" name="search" id="search" class="form-control" 
                                   placeholder="Search by name or description..." 
                                   value="{{ request('search') }}">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">&nbsp;</label>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-outline-primary">
                                    <i class="bi bi-search"></i> Search
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Equipment Grid -->
            <div class="row">
                @forelse($equipment as $item)
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card h-100 shadow-sm">
                            <div class="position-relative">
                                @if($item->image)
                                    <img src="{{ $item->image_url }}" class="card-img-top" alt="{{ $item->name }}" 
                                         style="height: 200px; object-fit: cover;">
                                @else
                                    <div class="card-img-top bg-light d-flex align-items-center justify-content-center" 
                                         style="height: 200px;">
                                        <i class="bi bi-gear-wide-connected text-muted" style="font-size: 3rem;"></i>
                                    </div>
                                @endif
                                <div class="position-absolute top-0 end-0 m-2">
                                    @if($item->is_active)
                                        <span class="badge bg-success">Active</span>
                                    @else
                                        <span class="badge bg-secondary">Inactive</span>
                                    @endif
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <h5 class="card-title mb-0">{{ $item->name }}</h5>
                                    <span class="badge bg-info">{{ $item->type_name }}</span>
                                </div>
                                
                                @if($item->description)
                                    <p class="card-text text-muted small mb-3">
                                        {{ Str::limit($item->description, 100) }}
                                    </p>
                                @endif

                                <div class="row text-center mb-3">
                                    <div class="col-4">
                                        <small class="text-muted d-block">Operating Hours</small>
                                        <strong>{{ $item->start_time->format('H:i') }} - {{ $item->end_time->format('H:i') }}</strong>
                                    </div>
                                    <div class="col-4">
                                        <small class="text-muted d-block">Slot Duration</small>
                                        <strong>{{ $item->slot_duration }} min</strong>
                                    </div>
                                    <div class="col-4">
                                        <small class="text-muted d-block">Sort Order</small>
                                        <strong>{{ $item->sort_order }}</strong>
                                    </div>
                                </div>

                                <div class="d-flex justify-content-between align-items-center">
                                    <div class="btn-group" role="group">
                                        <a href="{{ route('equipment.show', $item) }}" class="btn btn-outline-primary btn-sm">
                                            <i class="bi bi-eye"></i> View
                                        </a>
                                        <a href="{{ route('equipment.edit', $item) }}" class="btn btn-outline-secondary btn-sm">
                                            <i class="bi bi-pencil"></i> Edit
                                        </a>
                                    </div>
                                    <div class="dropdown">
                                        <button class="btn btn-outline-secondary btn-sm dropdown-toggle" type="button" 
                                                data-bs-toggle="dropdown">
                                            <i class="bi bi-three-dots"></i>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li>
                                                <a class="dropdown-item" href="{{ route('equipment.time-slots', $item) }}">
                                                    <i class="bi bi-clock"></i> View Time Slots
                                                </a>
                                            </li>
                                            <li>
                                                <form action="{{ route('equipment.toggle-active', $item) }}" method="POST" class="d-inline">
                                                    @csrf
                                                    <button type="submit" class="dropdown-item">
                                                        <i class="bi bi-toggle-{{ $item->is_active ? 'on' : 'off' }}"></i>
                                                        {{ $item->is_active ? 'Deactivate' : 'Activate' }}
                                                    </button>
                                                </form>
                                            </li>
                                            <li><hr class="dropdown-divider"></li>
                                            <li>
                                                <form action="{{ route('equipment.destroy', $item) }}" method="POST" 
                                                      onsubmit="return confirm('Are you sure you want to delete this equipment?')">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="dropdown-item text-danger">
                                                        <i class="bi bi-trash"></i> Delete
                                                    </button>
                                                </form>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                @empty
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body text-center py-5">
                                <i class="bi bi-gear-wide-connected text-muted mb-3" style="font-size: 3rem;"></i>
                                <h5 class="text-muted">No Equipment Found</h5>
                                <p class="text-muted">No equipment matches your current filters.</p>
                                <a href="{{ route('equipment.create') }}" class="btn btn-primary">
                                    <i class="bi bi-plus-circle"></i> Add First Equipment
                                </a>
                            </div>
                        </div>
                    </div>
                @endforelse
            </div>

            <!-- Pagination -->
            @if($equipment->hasPages())
                <div class="d-flex justify-content-center mt-4">
                    {{ $equipment->appends(request()->query())->links() }}
                </div>
            @endif
        </div>
    </div>
</div>

<!-- Statistics Modal -->
<div class="modal fade" id="statisticsModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Equipment Statistics</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="card bg-primary text-white">
                            <div class="card-body">
                                <h6 class="card-title">Total Equipment</h6>
                                <h3 class="mb-0">{{ $equipment->total() }}</h3>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="card bg-success text-white">
                            <div class="card-body">
                                <h6 class="card-title">Active Equipment</h6>
                                <h3 class="mb-0">{{ $equipment->where('is_active', true)->count() }}</h3>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="mt-3">
                    <h6>Equipment by Type</h6>
                    @foreach($types as $key => $name)
                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <span>{{ $name }}</span>
                            <span class="badge bg-info">{{ $equipment->where('type', $key)->count() }}</span>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Auto-submit form when filters change
    const filterSelects = document.querySelectorAll('#type, #status');
    filterSelects.forEach(select => {
        select.addEventListener('change', function() {
            this.closest('form').submit();
        });
    });

    // Search with debounce
    let searchTimeout;
    const searchInput = document.getElementById('search');
    searchInput.addEventListener('input', function() {
        clearTimeout(searchTimeout);
        searchTimeout = setTimeout(() => {
            this.closest('form').submit();
        }, 500);
    });
});
</script>
@endpush 