@extends('layouts.app')

@section('content')
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
    <div class="bg-white overflow-hidden shadow sm:rounded-lg">
        <div class="px-4 py-5 sm:px-6">
            <h3 class="text-xl leading-6 font-semibold text-gray-900">
                Edit Industrial Visit
            </h3>
        </div>

        <div class="px-4 py-5 sm:p-6">
            @if ($errors->any())
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif

            <form action="{{ request()->is('admin/*') ? route('admin.IvOrganised.update', $response->id) : (request()->is('coordinator/*') ? route('coordinator.IvOrganised.update', $response->id) : route('faculty.IvOrganised.update', $response->id)) }}" method="POST" enctype="multipart/form-data">
                @csrf
                @method('PUT')

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- Company Name -->
                    <div>
                        <label for="company_name" class="block text-sm font-medium text-gray-700">Name of the Company/Field/Institute</label>
                        <input type="text" name="company_name" id="company_name" value="{{ old('company_name', $response->company_name) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- Company Address -->
                    <div>
                        <label for="company_address" class="block text-sm font-medium text-gray-700">Address of the Company/Field/Institute</label>
                        <input type="text" name="company_address" id="company_address" value="{{ old('company_address', $response->company_address) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- Resource Person Name -->
                    <div>
                        <label for="resource_person_name" class="block text-sm font-medium text-gray-700">Resource Person Name</label>
                        <input type="text" name="resource_person_name" id="resource_person_name" value="{{ old('resource_person_name', $response->resource_person_name) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- Resource Person Contact Details -->
                    <div>
                        <label for="resource_person_contact_details" class="block text-sm font-medium text-gray-700">Resource Person Contact Details</label>
                        <input type="text" name="resource_person_contact_details" id="resource_person_contact_details" value="{{ old('resource_person_contact_details', $response->resource_person_contact_details) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- Target Audience -->
                    <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="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                            <option value="faculty" {{ old('target_audience', $response->target_audience) == 'faculty' ? 'selected' : '' }}>Faculty</option>
                            <option value="student" {{ old('target_audience', $response->target_audience) == 'student' ? 'selected' : '' }}>Student</option>
                            <option value="both" {{ old('target_audience', $response->target_audience) == 'both' ? 'selected' : '' }}>Both</option>
                        </select>
                    </div>

                    <!-- Student Year (conditionally displayed) -->
                    <div id="student_year_container" class="{{ old('target_audience', $response->target_audience) == 'faculty' ? 'hidden' : '' }}">
                        <label for="student_year" class="block text-sm font-medium text-gray-700">Year of Study</label>
                        <input type="text" name="student_year" id="student_year" value="{{ old('student_year', $response->student_year) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" {{ old('target_audience', $response->target_audience) == 'faculty' ? '' : 'required' }}>
                    </div>

                    <!-- Number of Participants -->
                    <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" value="{{ old('number_of_participants', $response->number_of_participants) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- Department -->
                    <div>
                        <label for="department" class="block text-sm font-medium text-gray-700">Department</label>
                        <input type="text" id="department" value="{{ $response->department->name ?? 'Unknown' }}" class="mt-1 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md bg-gray-100" disabled>
                    </div>

                    <!-- Faculty -->
                    <div>
                        <label for="faculty" class="block text-sm font-medium text-gray-700">Organising Faculty (Convenor/Coordinator)</label>
                        <input type="text" id="faculty" value="{{ $response->faculty->name ?? 'Unknown' }}" class="mt-1 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md bg-gray-100" disabled>
                    </div>

                    <!-- Start Date -->
                    <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" value="{{ old('start_date', \Carbon\Carbon::parse($response->start_date)->format('Y-m-d')) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- Start Time -->
                    <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" value="{{ old('start_time', \Carbon\Carbon::parse($response->start_date)->format('H:i')) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- End Date -->
                    <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" value="{{ old('end_date', \Carbon\Carbon::parse($response->end_date)->format('Y-m-d')) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- End Time -->
                    <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" value="{{ old('end_time', \Carbon\Carbon::parse($response->end_date)->format('H:i')) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>
                    </div>

                    <!-- Objective -->
                    <div class="col-span-1 md:col-span-2">
                        <label for="objective" class="block text-sm font-medium text-gray-700">Objective of Activity</label>
                        <textarea name="objective" id="objective" rows="3" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>{{ old('objective', $response->objective) }}</textarea>
                    </div>

                    <!-- Outcomes -->
                    <div class="col-span-1 md:col-span-2">
                        <label for="outcomes" class="block text-sm font-medium text-gray-700">Outcomes of Activity</label>
                        <textarea name="outcomes" id="outcomes" rows="3" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" required>{{ old('outcomes', $response->outcomes) }}</textarea>
                    </div>

                    <!-- Proof -->
                    <div class="col-span-1 md:col-span-2">
                        <label for="proof" class="block text-sm font-medium text-gray-700">Proof</label>
                        @if ($response->proof)
                        <div class="mb-2">
                            <span class="text-sm text-gray-600">Current file:</span>
                            <a href="{{ asset('storage/' . $response->proof) }}" target="_blank" class="ml-2 text-blue-600 hover:text-blue-800">View</a>
                        </div>
                        @endif
                        <input type="file" name="proof" id="proof" class="mt-1 block w-full text-sm text-gray-500
                            file:mr-4 file:py-2 file:px-4
                            file:rounded-full file:border-0
                            file:text-sm file:font-semibold
                            file:bg-blue-50 file:text-blue-700
                            hover:file:bg-blue-100">
                        <p class="mt-1 text-sm text-gray-500">Upload new proof (JPG, JPEG, PNG, PDF, DOC, DOCX, ZIP)</p>
                    </div>
                </div>

                <div class="mt-6 flex items-center justify-end">
                    <a href="{{ request()->is('admin/*') ? route('admin.IvOrganisedResponses') : (request()->is('coordinator/*') ? route('coordinator.IvOrganisedResponses') : route('faculty.IvOrganisedResponses')) }}" class="bg-gray-200 py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500 mr-3">
                        Cancel
                    </a>
                    <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">
                        Update
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Handle showing/hiding student year field based on target audience
        const targetAudienceField = document.getElementById('target_audience');
        const studentYearContainer = document.getElementById('student_year_container');
        const studentYearField = document.getElementById('student_year');
        
        function toggleStudentYear() {
            if (targetAudienceField.value === 'faculty') {
                studentYearContainer.classList.add('hidden');
                studentYearField.required = false;
                studentYearField.value = '';
            } else {
                studentYearContainer.classList.remove('hidden');
                studentYearField.required = true;
            }
        }
        
        targetAudienceField.addEventListener('change', toggleStudentYear);
        
        // Initialize on page load
        toggleStudentYear();
    });
</script>
@endsection