@extends('admin.layouts.app')

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

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="card-title mb-0">Edit University</h3>
                    <div class="d-flex gap-2">
                        <a href="{{ route('admin.universities.show', $university) }}" class="btn btn-info">
                            <i class="fas fa-eye me-2"></i>View
                        </a>
                        <a href="{{ route('admin.universities.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-2"></i>Back to Universities
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.universities.update', $university) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')
                        
                        <div class="row">
                            <div class="col-md-8">
                                <!-- University Name -->
                                <div class="mb-3">
                                    <label for="name" class="form-label">University Name <span class="text-danger">*</span></label>
                                    <input type="text" 
                                           class="form-control @error('name') is-invalid @enderror" 
                                           id="name" 
                                           name="name" 
                                           value="{{ old('name', $university->name) }}" 
                                           required>
                                    @error('name')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>

                                <!-- Address -->
                                <div class="mb-3">
                                    <label for="address" class="form-label">Address <span class="text-danger">*</span></label>
                                    <textarea class="form-control @error('address') is-invalid @enderror" 
                                              id="address" 
                                              name="address" 
                                              rows="3" 
                                              required>{{ old('address', $university->address) }}</textarea>
                                    @error('address')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>

                                <!-- Phone -->
                                <div class="mb-3">
                                    <label for="phone" class="form-label">Phone Number</label>
                                    <input type="text" 
                                           class="form-control @error('phone') is-invalid @enderror" 
                                           id="phone" 
                                           name="phone" 
                                           value="{{ old('phone', $university->phone) }}">
                                    @error('phone')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>

                                <!-- Email -->
                                <div class="mb-3">
                                    <label for="email" class="form-label">Email Address</label>
                                    <input type="email" 
                                           class="form-control @error('email') is-invalid @enderror" 
                                           id="email" 
                                           name="email" 
                                           value="{{ old('email', $university->email) }}">
                                    @error('email')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-4">
                                <!-- Current Logo -->
                                @if($university->logo)
                                <div class="mb-3">
                                    <label class="form-label">Current Logo</label>
                                    <div class="current-logo text-center p-3 border rounded">
                                        <img src="{{ Storage::url($university->logo) }}" 
                                             alt="{{ $university->name }} Logo" 
                                             class="img-fluid rounded" 
                                             style="max-height: 150px;">
                                    </div>
                                </div>
                                @endif

                                <!-- Logo Upload -->
                                <div class="mb-3">
                                    <label for="logo" class="form-label">{{ $university->logo ? 'Change Logo' : 'Upload Logo' }}</label>
                                    <div class="logo-upload-area border rounded p-3 text-center">
                                        <div id="logoPreview" class="mb-3" style="display: none;">
                                            <img id="logoImage" src="" alt="Logo Preview" class="img-fluid rounded" style="max-height: 150px;">
                                        </div>
                                        <div id="logoPlaceholder" class="mb-3">
                                            <i class="fas fa-upload text-muted" style="font-size: 2rem;"></i>
                                            <p class="text-muted mt-2">{{ $university->logo ? 'Upload New Logo' : 'Upload University Logo' }}</p>
                                        </div>
                                        <input type="file" 
                                               class="form-control @error('logo') is-invalid @enderror" 
                                               id="logo" 
                                               name="logo" 
                                               accept="image/*"
                                               onchange="previewLogo(this)">
                                        <small class="text-muted">Supported formats: JPG, PNG, GIF (Max: 2MB)</small>
                                        @error('logo')
                                            <div class="invalid-feedback d-block">{{ $message }}</div>
                                        @enderror
                                    </div>
                                </div>

                                <!-- Preview Card -->
                                <div class="card bg-light">
                                    <div class="card-header">
                                        <h6 class="mb-0">Preview</h6>
                                    </div>
                                    <div class="card-body text-center">
                                        <div id="previewLogo" class="mb-2">
                                            @if($university->logo)
                                                <img src="{{ Storage::url($university->logo) }}" 
                                                     alt="Logo" 
                                                     class="rounded-circle" 
                                                     style="width: 60px; height: 60px; object-fit: cover;">
                                            @else
                                                <div class="bg-white rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 60px; height: 60px;">
                                                    <i class="fas fa-university text-muted"></i>
                                                </div>
                                            @endif
                                        </div>
                                        <h6 id="previewName" class="text-dark">{{ $university->name }}</h6>
                                        <small id="previewAddress" class="text-muted">{{ $university->address }}</small>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <hr>

                        <div class="row">
                            <div class="col-12">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <small class="text-muted">
                                            <i class="fas fa-clock me-1"></i>
                                            Created: {{ $university->created_at->format('M d, Y \\a\\t h:i A') }}
                                            @if($university->updated_at != $university->created_at)
                                                | Updated: {{ $university->updated_at->format('M d, Y \\a\\t h:i A') }}
                                            @endif
                                        </small>
                                    </div>
                                    <div class="d-flex gap-2">
                                        <a href="{{ route('admin.universities.index') }}" class="btn btn-secondary">
                                            <i class="fas fa-times me-2"></i>Cancel
                                        </a>
                                        <button type="submit" class="btn btn-primary">
                                            <i class="fas fa-save me-2"></i>Update University
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function previewLogo(input) {
    const file = input.files[0];
    const logoPreview = document.getElementById('logoPreview');
    const logoPlaceholder = document.getElementById('logoPlaceholder');
    const logoImage = document.getElementById('logoImage');
    const previewLogo = document.getElementById('previewLogo');
    
    if (file) {
        const reader = new FileReader();
        reader.onload = function(e) {
            logoImage.src = e.target.result;
            logoPreview.style.display = 'block';
            logoPlaceholder.style.display = 'none';
            
            // Update preview card
            previewLogo.innerHTML = `<img src="${e.target.result}" alt="Logo" class="rounded-circle" style="width: 60px; height: 60px; object-fit: cover;">`;
        };
        reader.readAsDataURL(file);
    } else {
        logoPreview.style.display = 'none';
        logoPlaceholder.style.display = 'block';
        
        // Reset to original logo or placeholder
        @if($university->logo)
            previewLogo.innerHTML = '<img src="{{ Storage::url($university->logo) }}" alt="Logo" class="rounded-circle" style="width: 60px; height: 60px; object-fit: cover;">';
        @else
            previewLogo.innerHTML = '<div class="bg-white rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 60px; height: 60px;"><i class="fas fa-university text-muted"></i></div>';
        @endif
    }
}

// Live preview updates
document.getElementById('name').addEventListener('input', function() {
    const previewName = document.getElementById('previewName');
    previewName.textContent = this.value || '{{ $university->name }}';
});

document.getElementById('address').addEventListener('input', function() {
    const previewAddress = document.getElementById('previewAddress');
    previewAddress.textContent = this.value || '{{ $university->address }}';
});
</script>

<style>
.logo-upload-area {
    background-color: #f8f9fa;
    border: 2px dashed #dee2e6;
    transition: border-color 0.3s ease;
}

.logo-upload-area:hover {
    border-color: #007bff;
}

.current-logo {
    background-color: #f8f9fa;
}

.gap-2 {
    gap: 0.5rem;
}
</style>
@endsection