<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    @hotelSettings
    <title>Edit Reservation - {{ hotelName() }}</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-green: #1a936f;
            --secondary-green: #88d498;
            --light-green: #c6dabf;
            --dark-green: #114b5f;
            --accent: #f3e9d2;
        }
        
        body {
            background-color: #f8f9fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .navbar {
            background: linear-gradient(90deg, var(--dark-green) 0%, var(--primary-green) 100%);
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .sidebar {
            background-color: white;
            box-shadow: 3px 0 10px rgba(0, 0, 0, 0.1);
            height: calc(100vh - 56px);
            position: fixed;
            top: 56px;
            padding: 25px;
            z-index: 1000;
            overflow-y: auto;
            width: 250px;
        }
        
        .sidebar .nav-link {
            color: #495057;
            padding: 25px 20px;
            border-left: 4px solid transparent;
            transition: all 0.3s;
        }
        
        .sidebar .nav-link:hover {
            background-color: #f8f9fa;
            border-left: 4px solid var(--secondary-green);
        }
        
        .sidebar .nav-link.active {
            background-color: var(--light-green);
            color: var(--dark-green);
            border-left: 4px solid var(--primary-green);
            font-weight: 600;
        }
        
        .sidebar .nav-link i {
            margin-right: 10px;
            color: var(--primary-green);
        }
        
        .main-content {
            margin-left: 150px;
            margin-right: 150px;
            padding: 20px;
            margin-top: 56px;
        }
        
        .btn-primary {
            background-color: var(--primary-green);
            border-color: var(--primary-green);
        }
        
        .btn-primary:hover {
            background-color: var(--dark-green);
            border-color: var(--dark-green);
        }
        
        .btn-outline-primary {
            color: var(--primary-green);
            border-color: var(--primary-green);
        }
        
        .btn-outline-primary:hover {
            background-color: var(--primary-green);
            color: white;
        }
        
        .card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        .card-header {
            background: linear-gradient(90deg, var(--primary-green) 0%, var(--secondary-green) 100%);
            color: white;
            border-radius: 10px 10px 0 0 !important;
        }
        
        .form-label {
            font-weight: 600;
            color: var(--dark-green);
        }
        
        .required:after {
            content: " *";
            color: #dc3545;
        }
        
        .badge-confirmed { background-color: #28a745; }
        .badge-pending { background-color: #ffc107; color: #212529; }
        .badge-checked_in { background-color: #17a2b8; }
        .badge-checked_out { background-color: #6c757d; }
        .badge-cancelled { background-color: #dc3545; }
        
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                position: relative;
                height: auto;
                top: 0;
            }
            
            .main-content {
                margin-left: 0;
            }
        }
    </style>
</head>
<body>
    <!-- Navbar -->
   

    <!-- Main Content -->
    <div class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>Edit Reservation</h2>
            <div>
                <a href="{{ route('dashboard') }}" class="btn btn-outline-secondary me-2">
                    <i class="fas fa-tachometer-alt me-2"></i>Back to Dashboard
                </a>
                <a href="{{ route('reservations.index') }}" class="btn btn-outline-primary">
                    <i class="fas fa-arrow-left me-2"></i>Back to Reservations
                </a>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Edit Reservation Details</h5>
            </div>
            <div class="card-body">
                @if($errors->any())
                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                        <strong>Please fix the following errors:</strong>
                        <ul class="mb-0">
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                @endif

                @if(session('success'))
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        {{ session('success') }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                @endif

                @if(session('error'))
                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                        {{ session('error') }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                @endif

                <form method="POST" action="{{ route('reservations.update', $reservation->id) }}" id="reservationForm">
                    @csrf
                    @method('PUT')
                    
                    <!-- Reservation Information -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Reservation Number</label>
                                <input type="text" class="form-control" value="{{ $reservation->reservation_number }}" readonly>
                                <small class="text-muted">This is automatically generated and cannot be changed.</small>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Current Status</label>
                                <div>
                                    <span class="badge badge-{{ $reservation->status }} fs-6">
                                        {{ ucfirst(str_replace('_', ' ', $reservation->status)) }}
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Guest Selection Section -->
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="guest_id" class="form-label required">Guest</label>
                                <select class="form-select @error('guest_id') is-invalid @enderror" id="guest_id" name="guest_id" required>
                                    <option value="">Select Guest</option>
                                    @foreach($guests as $guest)
                                        <option value="{{ $guest->id }}" {{ old('guest_id', $reservation->guest_id) == $guest->id ? 'selected' : '' }}>
                                            {{ $guest->first_name }} {{ $guest->last_name }} 
                                            @if($guest->email) - {{ $guest->email }} @endif
                                        </option>
                                    @endforeach
                                </select>
                                @error('guest_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="room_id" class="form-label required">Room</label>
                                <select class="form-select @error('room_id') is-invalid @enderror" id="room_id" name="room_id" required>
                                    <option value="">Select Room</option>
                                    @foreach($rooms as $room)
                                        <option value="{{ $room->id }}" 
                                                data-price="{{ $room->price_per_night }}"
                                                {{ old('room_id', $reservation->room_id) == $room->id ? 'selected' : '' }}>
                                            {{ $room->room_number }} ({{ $room->roomType->name ?? 'N/A' }}) - 
                                            UGX {{ number_format($room->price_per_night, 2) }}/night
                                            @if($room->status !== 'available')
                                                - {{ ucfirst($room->status) }}
                                            @endif
                                        </option>
                                    @endforeach
                                </select>
                                @error('room_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label for="check_in" class="form-label required">Check-in Date</label>
                                <input type="date" class="form-control @error('check_in') is-invalid @enderror" 
                                       id="check_in" name="check_in" value="{{ old('check_in', $reservation->check_in->format('Y-m-d')) }}" required>
                                @error('check_in')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label for="check_out" class="form-label required">Check-out Date</label>
                                <input type="date" class="form-control @error('check_out') is-invalid @enderror" 
                                       id="check_out" name="check_out" value="{{ old('check_out', $reservation->check_out->format('Y-m-d')) }}" required>
                                @error('check_out')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label for="adults" class="form-label required">Adults</label>
                                <input type="number" class="form-control @error('adults') is-invalid @enderror" 
                                       id="adults" name="adults" value="{{ old('adults', $reservation->adults) }}" min="1" max="10" required>
                                @error('adults')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label for="children" class="form-label">Children</label>
                                <input type="number" class="form-control @error('children') is-invalid @enderror" 
                                       id="children" name="children" value="{{ old('children', $reservation->children) }}" min="0" max="10">
                                @error('children')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>

                    <!-- Staff Assignment Section -->
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="assigned_staff_id" class="form-label">Assigned Staff</label>
                                <select class="form-select @error('assigned_staff_id') is-invalid @enderror" 
                                        id="assigned_staff_id" name="assigned_staff_id">
                                    <option value="">Select Staff Member</option>
                                    @if($staff && $staff->count() > 0)
                                        @foreach($staff as $staffMember)
                                            <option value="{{ $staffMember->id }}" 
                                                {{ old('assigned_staff_id', $reservation->assigned_staff_id) == $staffMember->id ? 'selected' : '' }}>
                                                {{ $staffMember->first_name }} {{ $staffMember->last_name }}
                                                @if($staffMember->department || $staffMember->position)
                                                    - 
                                                    @if($staffMember->position)
                                                        {{ $staffMember->position->name ?? 'N/A' }}
                                                    @endif
                                                    @if($staffMember->department && $staffMember->position)
                                                        , 
                                                    @endif
                                                    @if($staffMember->department)
                                                        {{ $staffMember->department->name ?? 'N/A' }}
                                                    @endif
                                                @endif
                                            </option>
                                        @endforeach
                                    @else
                                        <option value="">No staff members available</option>
                                    @endif
                                </select>
                                @error('assigned_staff_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="total_amount" class="form-label required">Total Amount</label>
                                <div class="input-group">
                                    <span class="input-group-text">UGX</span>
                                    <input type="number" class="form-control @error('total_amount') is-invalid @enderror" 
                                           id="total_amount" name="total_amount" value="{{ old('total_amount', $reservation->total_amount) }}" 
                                           step="0.01" min="0" required>
                                    @error('total_amount')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="paid_amount" class="form-label">Paid Amount</label>
                                <div class="input-group">
                                    <span class="input-group-text">UGX</span>
                                    <input type="number" class="form-control @error('paid_amount') is-invalid @enderror" 
                                           id="paid_amount" name="paid_amount" value="{{ old('paid_amount', $reservation->paid_amount) }}" 
                                           step="0.01" min="0">
                                    @error('paid_amount')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                                <small class="text-muted">Balance: UGX {{ number_format($reservation->total_amount - $reservation->paid_amount, 2) }}</small>
                            </div>
                        </div>
                        
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="status" class="form-label required">Status</label>
                                <select class="form-select @error('status') is-invalid @enderror" id="status" name="status" required>
                                    <option value="pending" {{ old('status', $reservation->status) == 'pending' ? 'selected' : '' }}>Pending</option>
                                    <option value="confirmed" {{ old('status', $reservation->status) == 'confirmed' ? 'selected' : '' }}>Confirmed</option>
                                    <option value="checked_in" {{ old('status', $reservation->status) == 'checked_in' ? 'selected' : '' }}>Checked In</option>
                                    <option value="checked_out" {{ old('status', $reservation->status) == 'checked_out' ? 'selected' : '' }}>Checked Out</option>
                                    <option value="cancelled" {{ old('status', $reservation->status) == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                                </select>
                                @error('status')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="payment_status" class="form-label required">Payment Status</label>
                                <select class="form-select @error('payment_status') is-invalid @enderror" 
                                        id="payment_status" name="payment_status" required>
                                    <option value="pending" {{ old('payment_status', $reservation->payment_status) == 'pending' ? 'selected' : '' }}>Pending</option>
                                    <option value="paid" {{ old('payment_status', $reservation->payment_status) == 'paid' ? 'selected' : '' }}>Paid</option>
                                    <option value="partially_paid" {{ old('payment_status', $reservation->payment_status) == 'partially_paid' ? 'selected' : '' }}>Partially Paid</option>
                                    <option value="refunded" {{ old('payment_status', $reservation->payment_status) == 'refunded' ? 'selected' : '' }}>Refunded</option>
                                </select>
                                @error('payment_status')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="special_requests" class="form-label">Special Requests</label>
                        <textarea class="form-control @error('special_requests') is-invalid @enderror" 
                                  id="special_requests" name="special_requests" rows="3" 
                                  placeholder="Any special requests or notes...">{{ old('special_requests', $reservation->special_requests) }}</textarea>
                        @error('special_requests')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    
                    <div class="d-flex justify-content-end">
                        <button type="submit" class="btn btn-primary me-2" id="submitBtn">
                            <i class="fas fa-save me-2"></i>Update Reservation
                        </button>
                        <a href="{{ route('reservations.show', $reservation->id) }}" class="btn btn-outline-info me-2">
                            <i class="fas fa-eye me-2"></i>View Details
                        </a>
                        <a href="{{ route('reservations.index') }}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-2"></i>Cancel
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const roomSelect = document.getElementById('room_id');
            const checkInInput = document.getElementById('check_in');
            const checkOutInput = document.getElementById('check_out');
            const totalAmountInput = document.getElementById('total_amount');
            const form = document.getElementById('reservationForm');
            const submitBtn = document.getElementById('submitBtn');

            // Set minimum date for check-in to today
            const today = new Date().toISOString().split('T')[0];
            checkInInput.min = today;

            // Calculate total amount when dates or room change
            function calculateTotalAmount() {
                if (roomSelect.value && checkInInput.value && checkOutInput.value) {
                    const selectedOption = roomSelect.options[roomSelect.selectedIndex];
                    const roomPrice = parseFloat(selectedOption.getAttribute('data-price'));
                    
                    const checkIn = new Date(checkInInput.value);
                    const checkOut = new Date(checkOutInput.value);
                    
                    // Calculate number of nights
                    const timeDiff = checkOut.getTime() - checkIn.getTime();
                    const nights = Math.ceil(timeDiff / (1000 * 3600 * 24));
                    
                    if (nights > 0 && !isNaN(roomPrice)) {
                        totalAmountInput.value = (roomPrice * nights).toFixed(2);
                    }
                }
            }

            // Set minimum date for check-out to be after check-in
            checkInInput.addEventListener('change', function() {
                if (this.value) {
                    const nextDay = new Date(this.value);
                    nextDay.setDate(nextDay.getDate() + 1);
                    checkOutInput.min = nextDay.toISOString().split('T')[0];
                    
                    if (checkOutInput.value && checkOutInput.value < this.value) {
                        checkOutInput.value = '';
                    }
                }
                calculateTotalAmount();
            });

            checkOutInput.addEventListener('change', calculateTotalAmount);
            roomSelect.addEventListener('change', calculateTotalAmount);

            // Form submission handler
            form.addEventListener('submit', function(e) {
                submitBtn.disabled = true;
                submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>Updating...';
            });

            // Initialize calculation if values are already present
            calculateTotalAmount();
        });
    </script>
    
<script src="{{ asset('js/theme-applier.js') }}"></script>
</body>
</html>

