@extends('layouts.app')

@section('title', 'Submit Abstract')

@section('content')
<!-- Page Header -->
<section class="bg-gray-100 py-16">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <h1 class="text-3xl md:text-4xl font-bold text-gray-900">Submit Abstract</h1>
        <p class="text-gray-600 mt-2">Submit your research abstract for review</p>
    </div>
</section>

<!-- Conference Sub-themes -->
<!--<section class="py-12 bg-gray-50">-->
<!--    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">-->
<!--        <div class="text-center mb-8">-->
<!--            <h2 class="text-2xl font-bold text-gray-900 mb-2">Conference Sub-themes</h2>-->
<!--            <p class="text-gray-600">Select the appropriate conference for your submission</p>-->
<!--        </div>-->
        
<!--        <div class="grid grid-cols-1 md:grid-cols-3 gap-6">-->
            <!-- TEFLIN -->
<!--            <div class="bg-white rounded-xl p-6 border border-gray-200 hover:border-teflin-blue/50 transition">-->
<!--                <div class="w-12 h-12 bg-teflin-blue/10 rounded-lg flex items-center justify-center mb-4">-->
<!--                    <svg class="w-6 h-6 text-teflin-blue" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9m-9 9a9 9 0 019-9"/></svg>-->
<!--                </div>-->
<!--                <h3 class="font-semibold text-gray-900 mb-2">72nd TEFLIN International Conference</h3>-->
<!--                <p class="text-sm text-gray-600">Multilingualism, digital literacy, AI in education, inclusive learning, teacher development.</p>-->
<!--            </div>-->

            <!-- ICEMS -->
<!--            <div class="bg-white rounded-xl p-6 border border-gray-200 hover:border-teflin-gold/50 transition">-->
<!--                <div class="w-12 h-12 bg-teflin-gold/10 rounded-lg flex items-center justify-center mb-4">-->
<!--                    <svg class="w-6 h-6 text-teflin-gold" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/></svg>-->
<!--                </div>-->
<!--                <h3 class="font-semibold text-gray-900 mb-2">10th ICEMS</h3>-->
<!--                <p class="text-sm text-gray-600">Curriculum, educational policy, religious education, psychology, language & culture, digital education.</p>-->
<!--            </div>-->

            <!-- TESOL -->
<!--            <div class="bg-white rounded-xl p-6 border border-gray-200 hover:border-green-500/50 transition">-->
<!--                <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center mb-4">-->
<!--                    <svg class="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"/></svg>-->
<!--                </div>-->
<!--                <h3 class="font-semibold text-gray-900 mb-2">TESOL Research Colloquium</h3>-->
<!--                <p class="text-sm text-gray-600">Classroom-based studies, small-scale research in ELT, learning assessment, technology in ELT.</p>-->
<!--            </div>-->
<!--        </div>-->
<!--    </div>-->
<!--</section>-->

<section class="py-12">
    <div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="bg-white rounded-xl shadow-sm p-6 lg:p-8">
            @if($errors->any())
            <div class="mb-6 p-4 bg-red-50 border border-red-200 text-red-700 rounded-lg">
                <ul class="list-disc list-inside space-y-1">
                    @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif

            <form action="{{ route('submission.abstract.store') }}" method="POST" enctype="multipart/form-data" class="space-y-6">
                @csrf
                
                <div class="border-b pb-6">
                    <h2 class="text-lg font-semibold text-gray-900 mb-4">Author Information</h2>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Full Name *</label>
                            <input type="text" name="author_name" value="{{ old('author_name') }}" required class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Email *</label>
                            <input type="email" name="author_email" value="{{ old('author_email') }}" required class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Phone *</label>
                            <input type="text" name="author_phone" value="{{ old('author_phone') }}" required class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Institution</label>
                            <input type="text" name="institution" value="{{ old('institution') }}" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Country</label>
                            <input type="text" name="country" value="{{ old('country') }}" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                    </div>
                    <div class="mt-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Co-Authors (if any)</label>
                        <textarea name="co_authors" rows="2" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500" placeholder="Name 1, Institution; Name 2, Institution">{{ old('co_authors') }}</textarea>
                    </div>
                </div>

                <div class="border-b pb-6">
                    <h2 class="text-lg font-semibold text-gray-900 mb-4">Paper Details</h2>
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Conference Type *</label>
                            <select name="conference_type" required class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                <option value="">Select conference type</option>
                                <option value="TEFLIN Conference" {{ old('conference_type') === 'TEFLIN Conference' ? 'selected' : '' }}>TEFLIN Conference</option>
                                <option value="ICEMS Conference" {{ old('conference_type') === 'ICEMS Conference' ? 'selected' : '' }}>ICEMS Conference</option>
                                <option value="TISOL Colloquium" {{ old('conference_type') === 'TISOL Colloquium' ? 'selected' : '' }}>TESOL Colloquium (only for students)</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Presentation Type *</label>
                            <select name="presentation_type" required class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                <option value="">Select presentation type</option>
                                <option value="Parallel Presentation" {{ old('presentation_type') === 'Parallel Presentation' ? 'selected' : '' }}>Parallel Presentation</option>
                                <option value="Poster Presentation" {{ old('presentation_type') === 'Poster Presentation' ? 'selected' : '' }}>Poster Presentation</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Paper Title *</label>
                            <input type="text" name="title" value="{{ old('title') }}" required class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Sub-theme *</label>
                            <select name="sub_theme" required class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                <option value="">Select a sub-theme</option>
                                @foreach($subThemes as $theme)
                                <option value="{{ $theme->title }}" {{ old('sub_theme') === $theme->title ? 'selected' : '' }}>{{ $theme->title }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                Abstract * (150-250 words)
                                <span id="word-count" class="text-xs text-gray-500 ml-2">0 words</span>
                            </label>
                            <textarea 
                                name="abstract" 
                                id="abstract-text"
                                rows="8" 
                                required 
                                class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500" 
                                placeholder="Enter your abstract here..."
                                oninput="countWords()">{{ old('abstract') }}</textarea>
                            <p id="word-warning" class="text-xs mt-1 hidden"></p>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Keywords (3-5 keywords, comma separated)</label>
                            <input type="text" name="keywords" value="{{ old('keywords') }}" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500" placeholder="keyword1, keyword2, keyword3">
                        </div>
                    </div>
                </div>

                <div>
                    <h2 class="text-lg font-semibold text-gray-900 mb-4">Upload Abstract File *</h2>
                    <div class="border-2 border-dashed border-gray-300 rounded-lg p-6 text-center" id="file-upload-area">
                        <input type="file" name="abstract_file" accept=".pdf,.doc,.docx" class="hidden" id="abstract_file" required onchange="updateFileName(this)">
                        <label for="abstract_file" class="cursor-pointer block">
                            <svg class="w-12 h-12 text-gray-400 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"/></svg>
                            <p class="text-gray-600" id="file-upload-text">Click to upload or drag and drop</p>
                            <p class="text-sm text-gray-500">PDF, DOC, DOCX (Max 5MB)</p>
                        </label>
                        <div id="file-name-display" class="hidden mt-3 text-sm text-green-600 font-medium"></div>
                    </div>
                </div>

                <script>
                function updateFileName(input) {
                    const fileNameDisplay = document.getElementById('file-name-display');
                    const uploadText = document.getElementById('file-upload-text');
                    const uploadArea = document.getElementById('file-upload-area');
                    
                    if (input.files && input.files[0]) {
                        const fileName = input.files[0].name;
                        const fileSize = (input.files[0].size / 1024 / 1024).toFixed(2);
                        
                        fileNameDisplay.innerHTML = `✓ Selected: ${fileName} (${fileSize} MB)`;
                        fileNameDisplay.classList.remove('hidden');
                        uploadText.textContent = 'File selected - Click to change';
                        uploadArea.classList.add('border-green-500', 'bg-green-50');
                        uploadArea.classList.remove('border-gray-300');
                    }
                }

                function countWords() {
                    const textarea = document.getElementById('abstract-text');
                    const wordCountDisplay = document.getElementById('word-count');
                    const warningDisplay = document.getElementById('word-warning');
                    const submitButton = document.querySelector('button[type="submit"]');
                    
                    const text = textarea.value.trim();
                    const words = text ? text.split(/\s+/).filter(word => word.length > 0) : [];
                    const wordCount = words.length;
                    
                    wordCountDisplay.textContent = `${wordCount} words`;
                    
                    if (wordCount < 150 && wordCount > 0) {
                        wordCountDisplay.className = 'text-xs text-orange-600 ml-2 font-medium';
                        warningDisplay.textContent = `⚠️ Abstract must be at least 150 words (${150 - wordCount} more needed)`;
                        warningDisplay.className = 'text-xs mt-1 text-orange-600';
                        warningDisplay.classList.remove('hidden');
                        textarea.classList.add('border-orange-500');
                        textarea.classList.remove('border-red-500', 'border-green-500');
                        submitButton.disabled = true;
                        submitButton.classList.add('opacity-50', 'cursor-not-allowed');
                    } else if (wordCount > 250) {
                        wordCountDisplay.className = 'text-xs text-red-600 ml-2 font-medium';
                        warningDisplay.textContent = `❌ Abstract exceeds maximum of 250 words (${wordCount - 250} words over limit)`;
                        warningDisplay.className = 'text-xs mt-1 text-red-600 font-medium';
                        warningDisplay.classList.remove('hidden');
                        textarea.classList.add('border-red-500');
                        textarea.classList.remove('border-orange-500', 'border-green-500');
                        submitButton.disabled = true;
                        submitButton.classList.add('opacity-50', 'cursor-not-allowed');
                    } else if (wordCount >= 150 && wordCount <= 250) {
                        wordCountDisplay.className = 'text-xs text-green-600 ml-2 font-medium';
                        warningDisplay.classList.add('hidden');
                        textarea.classList.add('border-green-500');
                        textarea.classList.remove('border-orange-500', 'border-red-500');
                        submitButton.disabled = false;
                        submitButton.classList.remove('opacity-50', 'cursor-not-allowed');
                    } else {
                        wordCountDisplay.className = 'text-xs text-gray-500 ml-2';
                        warningDisplay.classList.add('hidden');
                        textarea.classList.remove('border-orange-500', 'border-red-500', 'border-green-500');
                        submitButton.disabled = false;
                        submitButton.classList.remove('opacity-50', 'cursor-not-allowed');
                    }
                }

                // Count words on page load if there's old input
                window.addEventListener('DOMContentLoaded', function() {
                    countWords();
                });
                </script>

                <div class="flex items-start gap-3 p-4 bg-gray-50 rounded-lg">
                    <input type="checkbox" required class="mt-1 h-4 w-4 text-primary-600 border-gray-300 rounded focus:ring-primary-500">
                    <p class="text-sm text-gray-600">I confirm that this submission is my original work and has not been published elsewhere. I agree to the conference terms and conditions.</p>
                </div>

                <button type="submit" class="w-full py-3 bg-teflin-blue text-white font-semibold rounded-lg hover:bg-teflin-blue-dark transition">
                    Submit Abstract
                </button>
            </form>
        </div>
    </div>
</section>
@endsection
