@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-book me-2 text-primary"></i>All Books Published
                    </h3>
                </div>
                <div class="card-body">
                    <!-- Table -->
                    <div class="table-responsive">
                        <table id="booksPublished-table" class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Title</th>
                                    <th>Author</th>
                                    <th>Publisher</th>
                                    <th>Date of Publication</th>
                                    <th>ISSN/eISSN number</th>
                                    <th>Department</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 = "Books Published";

        var initAjaxRoute = function(route) {
            table = $("#booksPublished-table").DataTable({
                fnDestroy: true,
                processing: true,
                serverSide: true,
                responsive: true,
                ajax: {
                    url: route,
                },
                columns: [
                    { data: 'id', name: 'id', searchable: false },
                    { data: 'title', name: 'title', orderable: true },
                    { data: 'author', name: 'author', orderable: true },
                    { data: 'publisher', name: 'publisher', orderable: true },
                    { data: 'date_of_publication', name: 'date_of_publication', orderable: true },
                    { data: 'issn', name: 'issn', orderable: false },
                    { data: 'department_name', name: 'department_name', orderable: false },
                    { 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.BooksPublishedResponses.data') }}";

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

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