@extends('layouts.app')

@section('content')
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
    <div class="bg-white overflow-hidden shadow sm:rounded-lg">
        <div class="px-4 py-5 sm:px-6">
            <h3 class="text-lg leading-6 font-medium text-gray-900">
                Submit Publication Details
            </h3>
            <p class="mt-1 max-w-2xl text-sm text-gray-500">
                Fill in the details of your publication.
            </p>
        </div>
        <div class="border-t border-gray-200">
            <form method="POST" action="{{ route('faculty.PublicationsFormResponse') }}" enctype="multipart/form-data">
                @csrf
                <div class="px-4 py-5 sm:px-6">
                    <div class="space-y-6">

                        <!-- Author Information -->
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                            <div>
                                <label for="first_author_name" class="block text-sm font-medium text-gray-700">First Author Name</label>
                                <select id="first_author_name" name="first_author_name[]" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" multiple="multiple"></select>
                                <input type="hidden" id="first_author_name_hidden" name="first_author_name" />
                            </div>
                            <div>
                                <label for="co_authors" class="block text-sm font-medium text-gray-700">Co-Authors (if any)</label>
                                <select id="co_authors" name="co_authors[]" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" multiple="multiple"></select>
                                <input type="hidden" id="co_authors_hidden" name="co_authors" />
                            </div>
                        </div>

                        <!-- Publication Title -->
                        <div>
                            <label for="title" class="block text-sm font-medium text-gray-700">Publication Title</label>
                            <input type="text" name="title" id="title" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required>
                        </div>

                        <!-- Publication Venue Information -->
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                            <div>
                                <label for="affiliation" class="block text-sm font-medium text-gray-700">Conference/Journal Name</label>
                                <input type="text" name="affiliation" id="affiliation" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required>
                            </div>
                            <div>
                            <label for="organizing_institute" class="block text-sm font-medium text-gray-700">Organising Institute/Company</label>
                            <select name="organizing_institute" id="organizing_institute" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm select2" style="width: 100%;" required>
                                <option value="">Select or type...</option>
                                @foreach($organisingInstitutes as $institute)
                                    <option value="{{ $institute }}" {{ old('organizing_institute') == $institute ? 'selected' : '' }}>{{ $institute }}</option>
                                @endforeach
                            </select>
                        </div>
                        </div>

                        <div>
                            <label for="venue_address" class="block text-sm font-medium text-gray-700">Venue Address</label>
                            <input type="text" name="venue_address" id="venue_address" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required>
                        </div>

                        <!-- Department and Faculty Name -->
                        <div class="grid grid-cols-2 sm:grid-cols-2 gap-4">
                            <div>
                                <label for="department" class="block text-sm font-medium text-gray-700">Department</label>
                                <input type="text" name="department" id="department" value="{{ auth()->user()->department->name }}" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-gray-100" disabled>
                            </div>
                            <div>
                                <label for="faculty_name" class="block text-sm font-medium text-gray-700">Faculty Name</label>
                                <input type="text" name="faculty_name" id="faculty_name" value="{{ auth()->user()->name }}" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-gray-100" disabled>
                                <input type="hidden" name="faculty_id" value="{{ auth()->user()->id }}">
                            </div>
                        </div>

                        <!-- Date Information -->
                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label for="start_date" class="block text-sm font-medium text-gray-700">Start Date</label>
                                <input type="date" name="start_date" id="start_date" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required onchange="calculateDays()">
                            </div>
                            <div>
                                <label for="end_date" class="block text-sm font-medium text-gray-700">End Date</label>
                                <input type="date" name="end_date" id="end_date" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required onchange="calculateDays()">
                            </div>
                            <div>
                                <label for="num_days" class="block text-sm font-medium text-gray-700">Number of Days</label>
                                <input type="text" name="num_days" id="num_days" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-gray-100" readonly>
                            </div>
                        </div>

                        <!-- Time Fields -->
                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label for="start_time" class="block text-sm font-medium text-gray-700">Start Time</label>
                                <input type="time" name="start_time" id="start_time" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required>
                            </div>
                            <div>
                                <label for="end_time" class="block text-sm font-medium text-gray-700">End Time</label>
                                <input type="time" name="end_time" id="end_time" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required>
                            </div>
                        </div>

                        <!-- Publication Type -->
                        <div>
                            <label for="activity_type" class="block text-sm font-medium text-gray-700">Select Publication Type</label>
                            <select name="activity_type" id="activity_type" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required>
                                <option value="">Select</option>
                                @if(isset($dropdowns['publication_types']) && is_array($dropdowns['publication_types']))
                                @foreach($dropdowns['publication_types'] as $key => $label)
                                <option value="{{ $key }}">{{ $label }}</option>
                                @endforeach
                                @else
                                <option value="journal">Journal Paper</option>
                                <option value="conference">Conference Paper</option>
                                <option value="book">Book/Book Chapter</option>
                                <option value="magazine">Magazine Article</option>
                                <option value="other">Other</option>
                                @endif
                            </select>
                        </div>

                        <!-- Publication Quality -->
                        <div class="grid grid-cols-1 sm:grid-cols-3 gap-4">
                            <div>
                                <label for="is_peer_reviewed" class="block text-sm font-medium text-gray-700">Peer Reviewed</label>
                                <select name="is_peer_reviewed" id="is_peer_reviewed" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" required>
                                    <option value="yes">Yes</option>
                                    <option value="no">No</option>
                                </select>
                            </div>
                            <div>
                                <label for="scopus_link" class="block text-sm font-medium text-gray-700">Scopus Link (if indexed)</label>
                                <input type="url" name="scopus_link" id="scopus_link" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" placeholder="https://www.scopus.com/...">
                            </div>
                            <div>
                                <label for="sci_link" class="block text-sm font-medium text-gray-700">SCI Link (if indexed)</label>
                                <input type="url" name="sci_link" id="sci_link" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" placeholder="https://www.webofscience.com/...">
                            </div>
                        </div>

                        <!-- Paper File -->
                        <div>
                            <label for="paper_file" class="block text-sm font-medium text-gray-700">Upload Paper</label>
                            <input type="file" name="paper_file" id="paper_file" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" accept=".jpg,.jpeg,.png,.pdf,.doc,.docx,.zip" required>
                            <p class="mt-1 text-xs text-gray-500">Accepted formats: JPG, JPEG, PNG, PDF, DOC, DOCX, ZIP</p>
                        </div>
                    </div>
                </div>

                <!-- Submit Button -->
                <div class="px-4 py-3 sm:px-6 text-center mt-4">
                    <button type="submit" class="inline-flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-black">
                        Submit Publication
                    </button>
                </div>

            </form>
        </div>
    </div>
</div>

<script>
    function calculateDays() {
        const startDate = new Date(document.getElementById('start_date').value);
        const endDate = new Date(document.getElementById('end_date').value);
        if (startDate && endDate) {
            const diffTime = Math.abs(endDate - startDate);
            const numDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + 1; // +1 to include both start and end days
            document.getElementById('num_days').value = numDays;
        }
    }

    document.addEventListener('DOMContentLoaded', function() {
        // Initialize Select2 for first_author_name
        $('#first_author_name').select2({
            tags: true,
            tokenSeparators: [','],
            ajax: {
                url: '{{ route('api.users.index') }}', // Adjust this route to fetch user names
                dataType: 'json',
                delay: 250,
                processResults: function(data) {
                    return {
                        results: data.map(user => ({
                            id: user.name,
                            text: user.name
                        }))
                    };
                },
                cache: true
            }
        });

        $('#first_author_name').on('change', function() {
            const selectedValues = $(this).val();
            document.getElementById('first_author_name_hidden').value = selectedValues.join(', ');
        });

        // Initialize Select2 for co_authors
        $('#co_authors').select2({
            tags: true,
            tokenSeparators: [','],
            ajax: {
                url: '{{ route('api.users.index') }}', // Adjust this route to fetch user names
                dataType: 'json',
                delay: 250,
                processResults: function(data) {
                    return {
                        results: data.map(user => ({
                            id: user.name,
                            text: user.name
                        }))
                    };
                },
                cache: true
            }
        });

        $('#co_authors').on('change', function() {
            const selectedValues = $(this).val();
            document.getElementById('co_authors_hidden').value = selectedValues.join(', ');
        });

        $('#organizing_institute').select2({
            tags: true,
            placeholder: "Select or type...",
            allowClear: true
        });

    });
</script>
@endsection