@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-laptop-code me-2 text-primary"></i>All Online Courses
                    </h3>
                </div>
                <div class="card-body">
                    <!-- Table -->
                    <div class="table-responsive">
                        <table id="onlineCourse-table" class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Faculty</th>
                                    <th>Department</th>
                                    <th>Course</th>
                                    <th>Offered By</th>
                                    <th>Start Date</th>
                                    <th>End Date</th>
                                    <th>Num Days</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 = "Online Courses";

        var initAjaxRoute = function(route) {
            table = $("#onlineCourse-table").DataTable({
                fnDestroy: true,
                processing: true,
                serverSide: true,
                responsive: true,
                ajax: {
                    url: route,
                },
                columns: [
                    { data: 'id', name: 'id', searchable: false },
                    { data: 'user_name', name: 'user_name', orderable: true },
                    { data: 'department_name', name: 'department_name', orderable: true },
                    { data: 'course', name: 'course', orderable: true },
                    { data: 'offered_by', name: 'offered_by', orderable: true },
                    { data: 'start_date', name: 'start_date', orderable: true },
                    { data: 'end_date', name: 'end_date', orderable: true },
                    { data: 'num_days', name: 'num_days', 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.OnlineCoursesResponses.data') }}";

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

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