<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Edit User - {{ $user->first_name }} {{ $user->last_name }} | WEPESI HOTEL MANAGEMENT SOULUTION</title>
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome 6 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    
    <style>
        :root {
            --primary: #059669;
            --primary-dark: #047857;
            --primary-light: #10b981;
            --secondary: #64748b;
            --success: #10b981;
            --info: #3b82f6;
            --warning: #f59e0b;
            --danger: #ef4444;
            --gray-50: #f8fafc;
            --gray-100: #f1f5f9;
            --gray-200: #e2e8f0;
            --gray-300: #cbd5e1;
            --gray-600: #475569;
            --gray-700: #334155;
            --gray-800: #1e293b;
            --gray-900: #0f172a;
        }
        
        body {
            background-color: var(--gray-50);
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            color: var(--gray-800);
        }
        
        /* Top Navigation */
        .top-nav {
            background: white;
            box-shadow: 0 4px 20px rgba(5, 150, 105, 0.1);
            padding: 1rem 2rem;
            position: sticky;
            top: 0;
            z-index: 1000;
            border-bottom: 3px solid var(--primary);
        }
        
        .nav-brand {
            font-size: 1.5rem;
            font-weight: 800;
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        .nav-brand i {
            font-size: 2rem;
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .back-to-dashboard {
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            color: white;
            padding: 0.6rem 1.5rem;
            border-radius: 50px;
            text-decoration: none;
            font-weight: 600;
            font-size: 0.95rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            transition: all 0.3s;
            border: none;
            box-shadow: 0 4px 12px rgba(5, 150, 105, 0.3);
        }
        
        .back-to-dashboard:hover {
            background: linear-gradient(135deg, var(--primary-dark), var(--primary));
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(5, 150, 105, 0.4);
            color: white;
        }
        
        .logout-btn {
            background: none;
            border: none;
            color: var(--gray-600);
            font-size: 0.95rem;
            padding: 0.6rem 1.2rem;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            border-radius: 50px;
        }
        
        .logout-btn:hover {
            background: #fee2e2;
            color: var(--danger);
        }
        
        /* Main Content */
        .main-content {
            padding: 2rem;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        /* Page Header */
        .page-header {
            margin-bottom: 2rem;
            padding: 1.5rem 2rem;
            background: white;
            border-radius: 20px;
            box-shadow: 0 4px 12px rgba(5, 150, 105, 0.08);
            border-bottom: 3px solid var(--primary-light);
        }
        
        .page-title {
            color: var(--gray-800);
            font-weight: 800;
            font-size: 2rem;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 1rem;
        }
        
        .page-title i {
            color: var(--primary);
            font-size: 2.2rem;
        }
        
        .breadcrumb {
            background: transparent;
            padding: 0;
            margin: 0.5rem 0 0 0;
        }
        
        .breadcrumb-item a {
            color: var(--primary);
            text-decoration: none;
            font-weight: 500;
        }
        
        .breadcrumb-item a:hover {
            color: var(--primary-dark);
            text-decoration: underline;
        }
        
        .breadcrumb-item.active {
            color: var(--gray-600);
        }
        
        /* Form Card */
        .form-card {
            background: white;
            border-radius: 24px;
            padding: 2rem;
            box-shadow: 0 8px 24px rgba(5, 150, 105, 0.12);
            border: 1px solid var(--gray-200);
        }
        
        .form-section-title {
            color: var(--primary);
            font-weight: 700;
            font-size: 1.2rem;
            margin-bottom: 1.5rem;
            padding-bottom: 0.75rem;
            border-bottom: 2px solid var(--gray-200);
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .form-label {
            font-weight: 600;
            color: var(--gray-700);
            margin-bottom: 0.5rem;
        }
        
        .form-control-custom, .form-select-custom {
            border: 2px solid var(--gray-200);
            border-radius: 12px;
            padding: 0.75rem 1rem;
            font-size: 0.95rem;
            transition: all 0.2s;
        }
        
        .form-control-custom:focus, .form-select-custom:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 4px rgba(5, 150, 105, 0.1);
            outline: none;
        }
        
        .form-control-custom.is-invalid, .form-select-custom.is-invalid {
            border-color: var(--danger);
        }
        
        .invalid-feedback {
            color: var(--danger);
            font-size: 0.85rem;
            margin-top: 0.25rem;
        }
        
        .required-star {
            color: var(--danger);
            margin-left: 2px;
        }
        
        /* Action Buttons */
        .action-buttons {
            display: flex;
            gap: 1rem;
            margin-top: 2rem;
        }
        
        .btn-primary-custom {
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            color: white;
            border: none;
            padding: 0.75rem 2rem;
            border-radius: 50px;
            font-weight: 600;
            font-size: 1rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            transition: all 0.3s;
            box-shadow: 0 4px 12px rgba(5, 150, 105, 0.2);
        }
        
        .btn-primary-custom:hover {
            background: linear-gradient(135deg, var(--primary-dark), var(--primary));
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(5, 150, 105, 0.3);
            color: white;
        }
        
        .btn-outline-custom {
            background: white;
            border: 2px solid var(--primary-light);
            color: var(--primary);
            padding: 0.75rem 2rem;
            border-radius: 50px;
            font-weight: 600;
            font-size: 1rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            transition: all 0.3s;
        }
        
        .btn-outline-custom:hover {
            background: var(--primary);
            color: white;
            border-color: var(--primary);
            transform: translateY(-2px);
        }
        
        .btn-danger-custom {
            background: linear-gradient(135deg, var(--danger), #dc2626);
            color: white;
            border: none;
            padding: 0.75rem 2rem;
            border-radius: 50px;
            font-weight: 600;
            font-size: 1rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            transition: all 0.3s;
            box-shadow: 0 4px 12px rgba(239, 68, 68, 0.2);
        }
        
        .btn-danger-custom:hover {
            background: linear-gradient(135deg, #dc2626, var(--danger));
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(239, 68, 68, 0.3);
            color: white;
        }
        
        /* Loading Overlay */
        .loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(255, 255, 255, 0.9);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 9999;
            backdrop-filter: blur(4px);
        }
        
        .loading-spinner {
            width: 60px;
            height: 60px;
            border: 5px solid var(--gray-200);
            border-top-color: var(--primary);
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        /* Alert */
        .alert-custom {
            border-radius: 12px;
            padding: 1rem 1.5rem;
            margin-bottom: 2rem;
            border-left: 4px solid;
        }
        
        .alert-custom.success {
            background: #d1fae5;
            border-left-color: var(--success);
            color: #065f46;
        }
        
        .alert-custom.error {
            background: #fee2e2;
            border-left-color: var(--danger);
            color: #991b1b;
        }
        
        /* Password Strength */
        .password-strength {
            margin-top: 0.5rem;
        }
        
        .strength-bar {
            height: 4px;
            background: var(--gray-200);
            border-radius: 2px;
            overflow: hidden;
        }
        
        .strength-fill {
            height: 100%;
            width: 0;
            transition: width 0.3s;
        }
        
        .strength-fill.weak { background: var(--danger); width: 33.33%; }
        .strength-fill.medium { background: var(--warning); width: 66.66%; }
        .strength-fill.strong { background: var(--success); width: 100%; }
        
        .strength-text {
            font-size: 0.8rem;
            margin-top: 0.25rem;
        }
        
        @media (max-width: 768px) {
            .main-content {
                padding: 1rem;
            }
            
            .action-buttons {
                flex-direction: column;
            }
            
            .btn-primary-custom, .btn-outline-custom {
                width: 100%;
                justify-content: center;
            }
        }
        /* 2026 elegance refresh */
        body {
            background:
                radial-gradient(circle at 10% 3%, rgba(16, 185, 129, 0.1), transparent 34%),
                radial-gradient(circle at 93% 8%, rgba(5, 150, 105, 0.08), transparent 31%),
                #f4f8f5;
        }

        .top-nav,
        .navbar {
            box-shadow: 0 10px 28px rgba(5, 150, 105, 0.2);
        }

        .edit-card,
        .form-card,
        .card {
            border-radius: 16px !important;
            border: 1px solid rgba(5, 150, 105, 0.11) !important;
            box-shadow: 0 12px 26px rgba(5, 150, 105, 0.1) !important;
        }

        .form-control,
        .form-select {
            border-radius: 12px;
            border-color: #cfeadf;
        }

        .btn {
            border-radius: 999px;
            font-weight: 600;
        }
    </style>
    <link rel="stylesheet" href="{{ asset('css/admin-elegant-theme.css') }}">
</head>
<body class="super-admin-elegant">
    <!-- Loading Overlay -->
    <div class="loading-overlay" id="loadingOverlay">
        <div class="loading-spinner"></div>
    </div>

    <!-- Top Navigation -->
    <nav class="top-nav">
        <div class="d-flex justify-content-between align-items-center">
            <div class="d-flex align-items-center gap-3">
                <a href="{{ route('super.admin.dashboard') }}" class="back-to-dashboard">
                    <i class="fas fa-arrow-left"></i>
                    <span>Back to Dashboard</span>
                </a>
                
                <form method="POST" action="{{ route('logout') }}" class="m-0">
                    @csrf
                    <button type="submit" class="logout-btn">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>Logout</span>
                    </button>
                </form>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <div class="main-content">
        <!-- Page Header -->
        <div class="page-header">
            <h1 class="page-title">
                <i class="fas fa-edit"></i>
                Edit User
            </h1>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('super.admin.dashboard') }}">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('super.admin.users.index') }}">Users</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('super.admin.users.show', $user->id) }}">{{ $user->first_name }} {{ $user->last_name }}</a></li>
                    <li class="breadcrumb-item active">Edit</li>
                </ol>
            </nav>
        </div>

        <!-- Alert Messages -->
        @if(session('success'))
            <div class="alert-custom success">
                <i class="fas fa-check-circle me-2"></i>
                {{ session('success') }}
            </div>
        @endif

        @if(session('error'))
            <div class="alert-custom error">
                <i class="fas fa-exclamation-circle me-2"></i>
                {{ session('error') }}
            </div>
        @endif

        <!-- Edit Form -->
        <div class="form-card">
            <form method="POST" action="{{ route('super.admin.users.update', $user->id) }}" id="editUserForm">
                @csrf
                @method('PUT')
                
                <div class="form-section-title">
                    <i class="fas fa-user-circle"></i>
                    Personal Information
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">
                            First Name <span class="required-star">*</span>
                        </label>
                        <input type="text" 
                               class="form-control form-control-custom @error('first_name') is-invalid @enderror" 
                               name="first_name" 
                               value="{{ old('first_name', $user->first_name) }}" 
                               required>
                        @error('first_name')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">
                            Last Name <span class="required-star">*</span>
                        </label>
                        <input type="text" 
                               class="form-control form-control-custom @error('last_name') is-invalid @enderror" 
                               name="last_name" 
                               value="{{ old('last_name', $user->last_name) }}" 
                               required>
                        @error('last_name')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">
                            Email Address <span class="required-star">*</span>
                        </label>
                        <input type="email" 
                               class="form-control form-control-custom @error('email') is-invalid @enderror" 
                               name="email" 
                               value="{{ old('email', $user->email) }}" 
                               required>
                        @error('email')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Phone Number</label>
                        <input type="tel" 
                               class="form-control form-control-custom @error('phone') is-invalid @enderror" 
                               name="phone" 
                               value="{{ old('phone', $user->phone) }}">
                        @error('phone')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">
                            Role <span class="required-star">*</span>
                        </label>
                        <select class="form-select form-select-custom @error('role') is-invalid @enderror" 
                                name="role" 
                                required>
                            <option value="">Select Role</option>
                            <option value="super_admin" {{ old('role', $user->role) == 'super_admin' ? 'selected' : '' }}>Super Admin</option>
                            <option value="director" {{ old('role', $user->role) == 'director' ? 'selected' : '' }}>Director</option>
                            <option value="manager" {{ old('role', $user->role) == 'manager' ? 'selected' : '' }}>Manager</option>
                            <option value="front_desk" {{ old('role', $user->role) == 'front_desk' ? 'selected' : '' }}>Front Desk</option>
                            <option value="housekeeping" {{ old('role', $user->role) == 'housekeeping' ? 'selected' : '' }}>Housekeeping</option>
                            <option value="food_beverage" {{ old('role', $user->role) == 'food_beverage' ? 'selected' : '' }}>Food & Beverage</option>
                            <option value="maintenance" {{ old('role', $user->role) == 'maintenance' ? 'selected' : '' }}>Maintenance</option>
                            <option value="accountant" {{ old('role', $user->role) == 'accountant' ? 'selected' : '' }}>Accountant</option>
                        </select>
                        @error('role')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Hotel</label>
                        <select class="form-select form-select-custom @error('hotel_id') is-invalid @enderror" 
                                name="hotel_id" 
                                id="hotelSelect">
                            <option value="">No Hotel</option>
                            @foreach($hotels ?? [] as $hotel)
                                <option value="{{ $hotel->id }}" 
                                        {{ old('hotel_id', $user->hotel_id) == $hotel->id ? 'selected' : '' }}
                                        data-users-count="{{ $hotel->users_count }}" 
                                        data-max-users="{{ $hotel->max_users ?? 'Unlimited' }}">
                                    {{ $hotel->name }} ({{ $hotel->users_count }}/{{ $hotel->max_users ?? '∞' }} users)
                                </option>
                            @endforeach
                        </select>
                        <small class="text-muted" id="hotelCapacityInfo"></small>
                        @error('hotel_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="form-section-title mt-4">
                    <i class="fas fa-lock"></i>
                    Change Password (Optional)
                </div>
                <p class="text-muted small mb-3">Leave blank to keep current password</p>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">New Password</label>
                        <input type="password" 
                               class="form-control form-control-custom @error('password') is-invalid @enderror" 
                               name="password" 
                               id="password"
                               minlength="8">
                        <div class="password-strength" id="passwordStrength" style="display: none;">
                            <div class="strength-bar">
                                <div class="strength-fill" id="strengthFill"></div>
                            </div>
                            <div class="strength-text" id="strengthText"></div>
                        </div>
                        @error('password')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Confirm New Password</label>
                        <input type="password" 
                               class="form-control form-control-custom" 
                               name="password_confirmation" 
                               id="password_confirmation">
                        <div class="invalid-feedback" id="passwordMatchError" style="display: none;">
                            Passwords do not match
                        </div>
                    </div>
                </div>
                
                <div class="action-buttons">
                    <button type="submit" class="btn btn-primary-custom">
                        <i class="fas fa-save"></i>
                        Update User
                    </button>
                    <a href="{{ route('super.admin.users.show', $user->id) }}" class="btn btn-outline-custom">
                        <i class="fas fa-times"></i>
                        Cancel
                    </a>
                    @if($user->role !== 'super_admin')
                    <button type="button" class="btn btn-danger-custom" onclick="confirmDelete({{ $user->id }})">
                        <i class="fas fa-trash"></i>
                        Delete User
                    </button>
                    @endif
                </div>
            </form>
        </div>
    </div>

    <!-- Scripts -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <script>
        // CSRF Token Setup
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        // Loading Overlay
        function showLoading() {
            document.getElementById('loadingOverlay').style.display = 'flex';
        }
        
        function hideLoading() {
            document.getElementById('loadingOverlay').style.display = 'none';
        }

        // Hotel Capacity Check
        $('#hotelSelect').change(function() {
            let selected = $(this).find('option:selected');
            let usersCount = selected.data('users-count') || 0;
            let maxUsers = selected.data('max-users') || 'Unlimited';
            
            // Get current user ID (we're editing, so exclude current user from count)
            let currentUserId = {{ $user->id }};
            
            // You might need to adjust this logic based on your user counting
            if (maxUsers !== 'Unlimited' && usersCount >= maxUsers) {
                // Check if current user is already in this hotel
                let currentHotelId = '{{ $user->hotel_id }}';
                if (currentHotelId != selected.val()) {
                    $('#hotelCapacityInfo').html('<span class="text-danger"><i class="fas fa-exclamation-triangle me-1"></i>This hotel has reached its user limit!</span>');
                } else {
                    $('#hotelCapacityInfo').html('');
                }
            } else {
                $('#hotelCapacityInfo').html('');
            }
        });

        // Password Strength Checker
        $('#password').on('input', function() {
            let password = $(this).val();
            
            if (password.length > 0) {
                $('#passwordStrength').show();
                
                // Check password strength
                let strength = 0;
                
                // Length check
                if (password.length >= 8) strength += 1;
                if (password.length >= 10) strength += 1;
                
                // Character variety checks
                if (/[a-z]/.test(password)) strength += 1;
                if (/[A-Z]/.test(password)) strength += 1;
                if (/[0-9]/.test(password)) strength += 1;
                if (/[^a-zA-Z0-9]/.test(password)) strength += 1;
                
                // Determine strength level
                let strengthClass = 'weak';
                let strengthText = 'Weak';
                
                if (strength >= 5) {
                    strengthClass = 'strong';
                    strengthText = 'Strong';
                } else if (strength >= 3) {
                    strengthClass = 'medium';
                    strengthText = 'Medium';
                }
                
                $('#strengthFill').removeClass('weak medium strong').addClass(strengthClass);
                $('#strengthText').text('Password strength: ' + strengthText);
            } else {
                $('#passwordStrength').hide();
            }
            
            // Check password match
            checkPasswordMatch();
        });

        $('#password_confirmation').on('input', function() {
            checkPasswordMatch();
        });

        function checkPasswordMatch() {
            let password = $('#password').val();
            let confirm = $('#password_confirmation').val();
            
            if (confirm.length > 0) {
                if (password !== confirm) {
                    $('#password_confirmation').addClass('is-invalid');
                    $('#passwordMatchError').show();
                } else {
                    $('#password_confirmation').removeClass('is-invalid');
                    $('#passwordMatchError').hide();
                }
            } else {
                $('#password_confirmation').removeClass('is-invalid');
                $('#passwordMatchError').hide();
            }
        }

        // Form Validation
        $('#editUserForm').on('submit', function(e) {
            let password = $('#password').val();
            let confirm = $('#password_confirmation').val();
            
            if (password || confirm) {
                if (password !== confirm) {
                    e.preventDefault();
                    Swal.fire({
                        icon: 'error',
                        title: 'Password Mismatch',
                        text: 'New password and confirmation do not match.'
                    });
                } else if (password.length < 8) {
                    e.preventDefault();
                    Swal.fire({
                        icon: 'error',
                        title: 'Invalid Password',
                        text: 'Password must be at least 8 characters long.'
                    });
                }
            }
        });

        // Delete Confirmation
        function confirmDelete(id) {
            Swal.fire({
                title: 'Delete User',
                text: 'Are you sure you want to delete this user? This action cannot be undone.',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#ef4444',
                cancelButtonColor: '#6b7280',
                confirmButtonText: 'Yes, Delete',
                cancelButtonText: 'Cancel'
            }).then((result) => {
                if (result.isConfirmed) {
                    showLoading();
                    
                    $.ajax({
                        url: '/super-admin/users/' + id,
                        method: 'DELETE',
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        success: function(response) {
                            hideLoading();
                            Swal.fire({
                                icon: 'success',
                                title: 'Deleted!',
                                text: response.message || 'User has been deleted',
                                timer: 2000
                            }).then(() => {
                                window.location.href = '{{ route("super.admin.users.index") }}';
                            });
                        },
                        error: function(xhr) {
                            hideLoading();
                            Swal.fire({
                                icon: 'error',
                                title: 'Error',
                                text: xhr.responseJSON?.message || 'Failed to delete user'
                            });
                        }
                    });
                }
            });
        }

        // Trigger change on page load to check capacity
        $(document).ready(function() {
            $('#hotelSelect').trigger('change');
        });
    </script>
</body>
</html>