@extends('layouts.app')

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

@section('content')
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">Edit Equipment: {{ $equipment->name }}</h5>
                        <a href="{{ route('equipment.index') }}" class="btn btn-outline-secondary btn-sm">
                            <i class="bi bi-arrow-left"></i> Back to Equipment
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form action="{{ route('equipment.update', $equipment) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')
                        
                        <div class="row">
                            <div class="col-md-8">
                                <!-- Basic Information -->
                                <div class="mb-4">
                                    <h6 class="text-primary mb-3">Basic Information</h6>
                                    
                                    <div class="row">
                                        <div class="col-md-6 mb-3">
                                            <label for="name" class="form-label">Equipment Name *</label>
                                            <input type="text" class="form-control @error('name') is-invalid @enderror" 
                                                   id="name" name="name" value="{{ old('name', $equipment->name) }}" required>
                                            @error('name')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                        
                                        <div class="col-md-6 mb-3">
                                            <label for="type" class="form-label">Equipment Type *</label>
                                            <select class="form-select @error('type') is-invalid @enderror" 
                                                    id="type" name="type" required>
                                                <option value="">Select Type</option>
                                                @foreach($types as $key => $name)
                                                    <option value="{{ $key }}" 
                                                            {{ old('type', $equipment->type) == $key ? 'selected' : '' }}>
                                                        {{ $name }}
                                                    </option>
                                                @endforeach
                                            </select>
                                            @error('type')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label for="description" class="form-label">Description</label>
                                        <textarea class="form-control @error('description') is-invalid @enderror" 
                                                  id="description" name="description" rows="3" 
                                                  placeholder="Describe the equipment...">{{ old('description', $equipment->description) }}</textarea>
                                        @error('description')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                </div>

                                <!-- Operating Hours -->
                                <div class="mb-4">
                                    <h6 class="text-primary mb-3">Operating Hours</h6>
                                    
                                    <div class="row">
                                        <div class="col-md-4 mb-3">
                                            <label for="start_time" class="form-label">Start Time *</label>
                                            <input type="time" class="form-control @error('start_time') is-invalid @enderror" 
                                                   id="start_time" name="start_time" 
                                                   value="{{ old('start_time', $equipment->start_time->format('H:i')) }}" required>
                                            @error('start_time')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                        
                                        <div class="col-md-4 mb-3">
                                            <label for="end_time" class="form-label">End Time *</label>
                                            <input type="time" class="form-control @error('end_time') is-invalid @enderror" 
                                                   id="end_time" name="end_time" 
                                                   value="{{ old('end_time', $equipment->end_time->format('H:i')) }}" required>
                                            @error('end_time')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                        
                                        <div class="col-md-4 mb-3">
                                            <label for="slot_duration" class="form-label">Slot Duration (minutes) *</label>
                                            <select class="form-select @error('slot_duration') is-invalid @enderror" 
                                                    id="slot_duration" name="slot_duration" required>
                                                <option value="15" {{ old('slot_duration', $equipment->slot_duration) == 15 ? 'selected' : '' }}>15 minutes</option>
                                                <option value="30" {{ old('slot_duration', $equipment->slot_duration) == 30 ? 'selected' : '' }}>30 minutes</option>
                                                <option value="45" {{ old('slot_duration', $equipment->slot_duration) == 45 ? 'selected' : '' }}>45 minutes</option>
                                                <option value="60" {{ old('slot_duration', $equipment->slot_duration) == 60 ? 'selected' : '' }}>1 hour</option>
                                                <option value="90" {{ old('slot_duration', $equipment->slot_duration) == 90 ? 'selected' : '' }}>1.5 hours</option>
                                                <option value="120" {{ old('slot_duration', $equipment->slot_duration) == 120 ? 'selected' : '' }}>2 hours</option>
                                            </select>
                                            @error('slot_duration')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                        </div>
                                    </div>
                                </div>

                                <!-- Settings -->
                                <div class="mb-4">
                                    <h6 class="text-primary mb-3">Settings</h6>
                                    
                                    <div class="row">
                                        <div class="col-md-6 mb-3">
                                            <label for="sort_order" class="form-label">Sort Order</label>
                                            <input type="number" class="form-control @error('sort_order') is-invalid @enderror" 
                                                   id="sort_order" name="sort_order" 
                                                   value="{{ old('sort_order', $equipment->sort_order) }}" min="0">
                                            @error('sort_order')
                                                <div class="invalid-feedback">{{ $message }}</div>
                                            @enderror
                                            <div class="form-text">Lower numbers appear first in lists</div>
                                        </div>
                                        
                                        <div class="col-md-6 mb-3">
                                            <div class="form-check form-switch mt-4">
                                                <input class="form-check-input" type="checkbox" id="is_active" name="is_active" 
                                                       {{ old('is_active', $equipment->is_active) ? 'checked' : '' }}>
                                                <label class="form-check-label" for="is_active">
                                                    Active Equipment
                                                </label>
                                            </div>
                                            <div class="form-text">Inactive equipment won't be available for reservations</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <!-- Image Upload -->
                                <div class="mb-4">
                                    <h6 class="text-primary mb-3">Equipment Image</h6>
                                    
                                    @if($equipment->image)
                                        <div class="mb-3">
                                            <label class="form-label">Current Image</label>
                                            <img src="{{ $equipment->image_url }}" alt="Current Image" 
                                                 class="img-fluid rounded border" style="max-height: 200px;">
                                        </div>
                                    @endif
                                    
                                    <div class="mb-3">
                                        <label for="image" class="form-label">
                                            {{ $equipment->image ? 'Replace Image' : 'Upload Image' }}
                                        </label>
                                        <input type="file" class="form-control @error('image') is-invalid @enderror" 
                                               id="image" name="image" accept="image/*">
                                        @error('image')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                        <div class="form-text">Recommended: 800x600px, max 2MB</div>
                                    </div>
                                    
                                    <div id="imagePreview" class="d-none">
                                        <label class="form-label">New Image Preview</label>
                                        <img id="previewImg" src="" alt="Preview" class="img-fluid rounded border">
                                    </div>
                                </div>

                                <!-- Equipment Stats -->
                                <div class="card bg-light">
                                    <div class="card-body">
                                        <h6 class="card-title">Equipment Statistics</h6>
                                        <ul class="list-unstyled small">
                                            <li><i class="bi bi-calendar text-primary"></i> Created: {{ $equipment->created_at->format('M d, Y') }}</li>
                                            <li><i class="bi bi-clock text-primary"></i> Updated: {{ $equipment->updated_at->format('M d, Y') }}</li>
                                            <li><i class="bi bi-calendar-check text-primary"></i> Total Reservations: {{ $equipment->reservations->count() }}</li>
                                            <li><i class="bi bi-check-circle text-primary"></i> Active Reservations: {{ $equipment->reservations->where('status', 'confirmed')->count() }}</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <hr>

                        <div class="d-flex justify-content-end gap-2">
                            <a href="{{ route('equipment.index') }}" class="btn btn-outline-secondary">
                                Cancel
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check-circle"></i> Update Equipment
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Image preview
    const imageInput = document.getElementById('image');
    const imagePreview = document.getElementById('imagePreview');
    const previewImg = document.getElementById('previewImg');
    
    imageInput.addEventListener('change', function() {
        const file = this.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(e) {
                previewImg.src = e.target.result;
                imagePreview.classList.remove('d-none');
            };
            reader.readAsDataURL(file);
        } else {
            imagePreview.classList.add('d-none');
        }
    });

    // Time validation
    const startTime = document.getElementById('start_time');
    const endTime = document.getElementById('end_time');
    
    function validateTimes() {
        if (startTime.value && endTime.value) {
            if (startTime.value >= endTime.value) {
                endTime.setCustomValidity('End time must be after start time');
            } else {
                endTime.setCustomValidity('');
            }
        }
    }
    
    startTime.addEventListener('change', validateTimes);
    endTime.addEventListener('change', validateTimes);
});
</script>
@endpush 