@extends('admin.layouts.app')

@section('title', 'Create Pathway')

@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">Create New Pathway</h1>
                <a href="{{ route('admin.pathways.index') }}" class="btn btn-outline-secondary">
                    <i class="bi bi-arrow-left"></i> Back to Pathways
                </a>
            </div>
        </div>
    </div>

    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">Pathway Information</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.pathways.store') }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        
                        <!-- Name Field -->
                        <div class="mb-3">
                            <label for="name" class="form-label">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') }}" required>
                            @error('name')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <!-- Short Description Field -->
                        <div class="mb-3">
                            <label for="short_description" class="form-label">Short Description</label>
                            <textarea class="form-control @error('short_description') is-invalid @enderror" 
                                      id="short_description" name="short_description" rows="3" 
                                      placeholder="Brief description of the pathway...">{{ old('short_description') }}</textarea>
                            @error('short_description')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <div class="form-text">Maximum 1000 characters</div>
                        </div>

                        <!-- Logo Upload Field -->
                        <div class="mb-3">
                            <label for="logo" class="form-label">Logo</label>
                            <input type="file" class="form-control @error('logo') is-invalid @enderror" 
                                   id="logo" name="logo" accept="image/*">
                            @error('logo')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <div class="form-text">
                                Supported formats: JPEG, PNG, JPG, GIF, SVG. Maximum size: 2MB
                            </div>
                            
                            <!-- Logo Preview -->
                            <div id="logoPreview" class="mt-2" style="display: none;">
                                <img id="previewImage" src="" alt="Logo Preview" 
                                     class="img-thumbnail" style="max-width: 200px; max-height: 200px;">
                            </div>
                        </div>

                        <!-- Status Field -->
                        <div class="mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="status" name="status" 
                                       {{ old('status', true) ? 'checked' : '' }}>
                                <label class="form-check-label" for="status">
                                    Active Status
                                </label>
                            </div>
                            <div class="form-text">Enable this pathway to be visible and accessible</div>
                        </div>

                        <!-- Sorting Field -->
                        <div class="mb-3">
                            <label for="sorting" class="form-label">Sort Order</label>
                            <input type="number" class="form-control @error('sorting') is-invalid @enderror" 
                                   id="sorting" name="sorting" value="{{ old('sorting', 0) }}" min="0">
                            @error('sorting')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <div class="form-text">Lower numbers appear first. Default is 0.</div>
                        </div>

                        <!-- Form Actions -->
                        <div class="d-flex justify-content-between">
                            <a href="{{ route('admin.pathways.index') }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x-circle"></i> Cancel
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check-circle"></i> Create Pathway
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // Logo preview functionality
    $('#logo').change(function() {
        const file = this.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(e) {
                $('#previewImage').attr('src', e.target.result);
                $('#logoPreview').show();
            };
            reader.readAsDataURL(file);
        } else {
            $('#logoPreview').hide();
        }
    });

    // Form validation
    $('form').submit(function(e) {
        let isValid = true;
        
        // Validate name field
        const name = $('#name').val().trim();
        if (name.length === 0) {
            showFieldError('#name', 'Name is required');
            isValid = false;
        } else if (name.length > 255) {
            showFieldError('#name', 'Name must not exceed 255 characters');
            isValid = false;
        } else {
            clearFieldError('#name');
        }
        
        // Validate description length
        const description = $('#short_description').val().trim();
        if (description.length > 1000) {
            showFieldError('#short_description', 'Description must not exceed 1000 characters');
            isValid = false;
        } else {
            clearFieldError('#short_description');
        }
        
        // Validate sorting field
        const sorting = $('#sorting').val();
        if (sorting !== '' && (isNaN(sorting) || parseInt(sorting) < 0)) {
            showFieldError('#sorting', 'Sort order must be a non-negative number');
            isValid = false;
        } else {
            clearFieldError('#sorting');
        }
        
        if (!isValid) {
            e.preventDefault();
        }
    });
    
    // Helper functions for validation
    function showFieldError(fieldSelector, message) {
        const field = $(fieldSelector);
        field.addClass('is-invalid');
        field.siblings('.invalid-feedback').remove();
        field.after(`<div class="invalid-feedback">${message}</div>`);
    }
    
    function clearFieldError(fieldSelector) {
        const field = $(fieldSelector);
        field.removeClass('is-invalid');
        field.siblings('.invalid-feedback').remove();
    }
    
    // Character counter for description
    $('#short_description').on('input', function() {
        const maxLength = 1000;
        const currentLength = $(this).val().length;
        const remaining = maxLength - currentLength;
        
        let counterText = `${currentLength}/${maxLength} characters`;
        if (remaining < 100) {
            counterText += ` (${remaining} remaining)`;
        }
        
        // Update or create character counter
        let counter = $(this).siblings('.char-counter');
        if (counter.length === 0) {
            $(this).after(`<div class="form-text char-counter">${counterText}</div>`);
        } else {
            counter.text(counterText);
        }
        
        // Add warning color if approaching limit
        if (remaining < 50) {
            counter.addClass('text-warning');
        } else {
            counter.removeClass('text-warning');
        }
        
        if (remaining < 0) {
            counter.addClass('text-danger').removeClass('text-warning');
        } else {
            counter.removeClass('text-danger');
        }
    });
});
</script>
@endpush