@extends('admin.layouts.app')

@section('title', 'Add Visa Success Story')

@section('content')
<div class="max-w-2xl mx-auto">
    <div class="mb-6 flex items-center gap-3">
        <a href="{{ route('admin.visa-stories.index') }}" class="text-sm text-blue-600 hover:underline">← Back</a>
        <h1 class="text-xl font-bold text-gray-800">Add Visa Success Story</h1>
    </div>

    @if($errors->any())
        <div class="mb-4 rounded-lg bg-red-100 border border-red-300 text-red-700 px-4 py-3 text-sm">
            <ul class="list-disc list-inside space-y-1">@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul>
        </div>
    @endif

    <form action="{{ route('admin.visa-stories.store') }}" method="POST" enctype="multipart/form-data" class="bg-white rounded-xl border border-gray-200 p-6 space-y-4">
        @csrf
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div class="md:col-span-2">
                <label class="text-sm font-medium text-gray-700">Story Title <span class="text-red-500">*</span></label>
                <input name="title" value="{{ old('title') }}" required class="w-full mt-1 rounded-lg border-gray-300 text-sm" placeholder="e.g. Got Australian Student Visa Successfully">
            </div>
            <div>
                <label class="text-sm font-medium text-gray-700">Student Name</label>
                <input name="student_name" value="{{ old('student_name') }}" class="w-full mt-1 rounded-lg border-gray-300 text-sm" placeholder="e.g. Mahafuj Hossain">
            </div>
            <div>
                <label class="text-sm font-medium text-gray-700">Program</label>
                <input name="program" value="{{ old('program') }}" class="w-full mt-1 rounded-lg border-gray-300 text-sm" placeholder="e.g. Masters in IT">
            </div>
            <div>
                <label class="text-sm font-medium text-gray-700">University</label>
                <input name="university" value="{{ old('university') }}" class="w-full mt-1 rounded-lg border-gray-300 text-sm" placeholder="e.g. La Trobe University">
            </div>
            <div>
                <label class="text-sm font-medium text-gray-700">Country</label>
                <input name="country_name" value="{{ old('country_name') }}" class="w-full mt-1 rounded-lg border-gray-300 text-sm" placeholder="e.g. Australia">
            </div>
            <div class="md:col-span-2">
                <label class="text-sm font-medium text-gray-700">Description</label>
                <textarea name="description" rows="3" class="w-full mt-1 rounded-lg border-gray-300 text-sm" placeholder="Brief description of the student's journey...">{{ old('description') }}</textarea>
            </div>
            <div>
                <label class="text-sm font-medium text-gray-700">Image</label>
                <input type="file" name="image" accept="image/*" class="w-full mt-1 text-sm" id="visa-story-image-input">
                <img src="" alt="Selected image preview" class="mt-2 h-20 rounded-lg object-cover border border-gray-200 hidden" id="visa-story-image-preview">
                <p class="text-xs text-gray-400 mt-1">JPG/PNG/WebP, max 5MB</p>
            </div>
            <div>
                <label class="text-sm font-medium text-gray-700">Sort Order</label>
                <input type="number" name="sort_order" value="{{ old('sort_order', 0) }}" min="0" class="w-full mt-1 rounded-lg border-gray-300 text-sm">
                <p class="text-xs text-gray-400 mt-1">Lower = shown first</p>
            </div>
            <div class="flex items-center gap-2 mt-2">
                <input type="hidden" name="is_active" value="0">
                <input type="checkbox" name="is_active" id="is_active" value="1" {{ old('is_active', '1') == '1' ? 'checked' : '' }} class="rounded border-gray-300">
                <label for="is_active" class="text-sm font-medium text-gray-700">Active (show on website)</label>
            </div>
        </div>
        <div class="flex gap-3 pt-2">
            <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white font-semibold px-6 py-2.5 rounded-lg text-sm">Save Story</button>
            <a href="{{ route('admin.visa-stories.index') }}" class="px-6 py-2.5 rounded-lg text-sm border border-gray-300 text-gray-600 hover:bg-gray-50">Cancel</a>
        </div>
    </form>
</div>
@endsection

@push('scripts')
<script>
(() => {
    const input = document.getElementById('visa-story-image-input');
    const preview = document.getElementById('visa-story-image-preview');
    if (!input || !preview) return;

    input.addEventListener('change', () => {
        const file = input.files && input.files[0];
        if (!file) return;
        preview.src = URL.createObjectURL(file);
        preview.classList.remove('hidden');
    });
})();
</script>
@endpush
