@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 Activities Organised
            </h3>
            <p class="mt-1 max-w-2xl text-sm text-gray-500">
                Fill in the details of the activity you organised.
            </p>
        </div>
        <div class="border-t border-gray-200">
            <form method="POST" action="{{ route('faculty.ActivitiesOrganisedFormResponse') }}" enctype="multipart/form-data">
                @csrf
                <div class="px-4 py-5 sm:px-6">
                    <div class="space-y-6">

                        <!-- Title -->
                        <div>
                            <label for="title" class="block text-sm font-medium text-gray-700">Title/Description</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>

                        <!-- Resource Person Details -->
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                            <div>
                                <label for="resource_person_name" class="block text-sm font-medium text-gray-700">Resource Person Name</label>
                                <select id="resource_person_name" name="resource_person_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="resource_person_name_hidden" name="resource_person_name" />
                            </div>
                            <div>
                                <label for="resource_person_organization" class="block text-sm font-medium text-gray-700">Resource Person Organization</label>
                                <input type="text" name="resource_person_organization" id="resource_person_organization" 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>

                        <!-- Target Audience and Participants -->
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                            <div>
                                <label for="target_audience" class="block text-sm font-medium text-gray-700">Target Audience</label>
                                <select name="target_audience" id="target_audience" 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['target_groups']) && is_array($dropdowns['target_groups']))
                                    @foreach($dropdowns['target_groups'] as $key => $label)
                                    <option value="{{ $key }}">{{ $label }}</option>
                                    @endforeach
                                    @endif
                                </select>
                            </div>
                            <div>
                                <label for="number_of_participants" class="block text-sm font-medium text-gray-700">Number of Participants</label>
                                <input type="number" name="number_of_participants" id="number_of_participants" 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>

                        <!-- Venue -->
                        <div>
                            <label for="venue" class="block text-sm font-medium text-gray-700">Venue</label>
                            <input type="text" name="venue" id="venue" 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>
                            </div>
                        </div>

                        <!-- Start Date, End Date, Number of Days -->
                        <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>

                        <!-- Activity Type, Category, Level -->
                        <div class="grid grid-cols-2 sm:grid-cols-3 gap-4">
                            <div>
                                <label for="activity_type" class="block text-sm font-medium text-gray-700">Select Activity 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['activity_types']) && is_array($dropdowns['activity_types']))
                                    @foreach($dropdowns['activity_types'] as $key => $label)
                                    <option value="{{ $key }}">{{ $label }}</option>
                                    @endforeach
                                    @endif
                                </select>
                            </div>
                            <div>
                                <label for="category" class="block text-sm font-medium text-gray-700">Select Category</label>
                                <select name="category" id="category" 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['categories']) && is_array($dropdowns['categories']))
                                    @foreach($dropdowns['categories'] as $key => $label)
                                    <option value="{{ $key }}">{{ $label }}</option>
                                    @endforeach
                                    @endif
                                </select>
                            </div>
                            <div>
                                <label for="level" class="block text-sm font-medium text-gray-700">Select Level</label>
                                <select name="level" id="level" 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['levels']) && is_array($dropdowns['levels']))
                                    @foreach($dropdowns['levels'] as $key => $label)
                                    <option value="{{ $key }}">{{ $label }}</option>
                                    @endforeach
                                    @endif
                                </select>
                            </div>
                        </div>

                        <!-- Objective and Outcomes -->
                        <div>
                            <label for="objective" class="block text-sm font-medium text-gray-700">Objective</label>
                            <textarea name="objective" id="objective" rows="3" 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></textarea>
                        </div>

                        <div>
                            <label for="outcomes" class="block text-sm font-medium text-gray-700">Outcomes</label>
                            <textarea name="outcomes" id="outcomes" rows="3" 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></textarea>
                        </div>

                        <!-- Proof Document -->
                        <div>
                            <label for="proof" class="block text-sm font-medium text-gray-700">Upload Proof/Document</label>
                            <input type="file" name="proof" id="proof" 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">
                            <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 Response
                    </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 resource_person_name
        $('#resource_person_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
            }
        });

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

        // Initialize Select2 for activity_type, category, and level
        $('#target_audience').select2({
            placeholder: 'Select Target Audience',
            allowClear: true
        });
        $('#activity_type').select2({
            tags: true,
            placeholder: 'Select Activity Type',
            allowClear: true
        });

        $('#category').select2({
            placeholder: 'Select Category',
            allowClear: true
        });

        $('#level').select2({
            placeholder: 'Select Level',
            allowClear: true
        });

        // const form = document.querySelector('form');
        // form.addEventListener('submit', function () {
        //     const resourcePersonNameSelect = document.getElementById('resource_person_name');
        //     const resourcePersonNameHidden = document.getElementById('resource_person_name_hidden');

        //     if (resourcePersonNameSelect) {
        //         const selectedValues = $(resourcePersonNameSelect).val();
        //         resourcePersonNameHidden.value = selectedValues ? selectedValues.join(', ') : '';
        //     }
        // });
    });
</script>
@endsection