@extends('layouts.app')

@section('content')
<div class="container py-4">
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="page-title m-0">
                        <i class="fas fa-industry me-2 text-primary"></i>All Industrial Visits Organised
                    </h3>
                </div>
                <div class="card-body">
                    <!-- Table -->
                    <div class="table-responsive">
                        <table id="ivOrganised-table" class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Company Name</th>
                                    <th>Resource Person</th>
                                    <th>Target Audience</th>
                                    <th>Department</th>
                                    <th>Faculty</th>
                                    <th>Start Date</th>
                                    <th>End Date</th>
                                    <th>Student Year</th>
                                    <th>Participants</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <!-- Data will be loaded via AJAX -->
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        const sheetName = "Industrial Visits Organised";

        var initAjaxRoute = function(route) {
            table = $("#ivOrganised-table").DataTable({
                fnDestroy: true,
                processing: true,
                serverSide: true,
                responsive: true,
                ajax: {
                    url: route,
                },
                columns: [
                    { data: 'id', name: 'id', searchable: false },
                    { data: 'company_name', name: 'company_name', orderable: true },
                    { data: 'resource_person_name', name: 'resource_person_name', orderable: true },
                    { data: 'target_audience', name: 'target_audience', orderable: true },
                    { data: 'department_name', name: 'department_name', orderable: true },
                    { data: 'user_name', name: 'user_name', orderable: true },
                    { data: 'start_date', name: 'start_date', orderable: true },
                    { data: 'end_date', name: 'end_date', orderable: true },
                    { data: 'student_year', name: 'student_year', orderable: true },
                    { data: 'number_of_participants', name: 'number_of_participants', orderable: true },
                    { data: 'action', name: 'action', orderable: false, searchable: false },
                ],
                columnDefs: [
                    { targets: '_all', className: 'text-center wrap-text' },
                ],
                dom: '<"d-flex justify-content-between align-items-center mb-3"<"d-flex align-items-center"l><"d-flex"f<"ms-2"B>>>rtip',
                buttons: [
                    { extend: 'copy', text: '<i class="fas fa-copy me-1"></i> Copy', className: 'btn btn-sm btn-outline-white', title: sheetName },
                    { extend: 'csv', text: '<i class="fas fa-file-csv me-1"></i> CSV', className: 'btn btn-sm btn-outline-white', title: sheetName },
                    { extend: 'excel', text: '<i class="fas fa-file-excel me-1"></i> Excel', className: 'btn btn-sm btn-outline-white', title: sheetName },
                    { extend: 'pdf', text: '<i class="fas fa-file-pdf me-1"></i> PDF', className: 'btn btn-sm btn-outline-white', title: sheetName },
                    { extend: 'print', text: '<i class="fas fa-print me-1"></i> Print', className: 'btn btn-sm btn-outline-white', title: sheetName },
                ],
                language: {
                    search: "<i class='fas fa-search'></i> _INPUT_",
                    searchPlaceholder: "Search records...",
                    lengthMenu: "<i class='fas fa-list me-1'></i> _MENU_ records per page",
                    info: "Showing _START_ to _END_ of _TOTAL_ entries",
                    paginate: {
                        first: "<i class='fas fa-angle-double-left'></i>",
                        last: "<i class='fas fa-angle-double-right'></i>",
                        next: "<i class='fas fa-angle-right'></i>",
                        previous: "<i class='fas fa-angle-left'></i>"
                    }
                }
            });
        };

        // Set appropriate route based on user role
        const userRole = "{{ auth()->user()->role->name }}";
        let dataRoute = "{{ route('admin.IvOrganisedResponses.data') }}";

        if (userRole === 'Coordinator') {
            dataRoute = "{{ route('coordinator.IvOrganisedResponses.data') }}";
        }
        if (userRole === 'Faculty') {
            dataRoute = "{{ route('faculty.IvOrganisedResponses.data') }}";
        }

        initAjaxRoute(dataRoute);
    });
</script>
@endsection