@extends('layouts.app')

@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}">
<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-open me-2 text-primary"></i>All Publications
                    </h3>
                    @if(auth()->user()->role->name === 'Faculty')
                    <input type="hidden" id="scholar-url" value="{{ $scholarUrl ?? '' }}">
                    <button id="btn-fetch-scholar" class="btn btn-outline-primary">
                        <i class="fab fa-google me-1"></i> Import from Google Scholar
                    </button>
                    @endif
                </div>
                <div class="card-body">
                    <!-- 🔹 Google Scholar Modal -->
                    @if(auth()->user()->role->name === 'Faculty')
                    <div class="modal fade" id="scholarModal" tabindex="-1" aria-labelledby="scholarModalLabel" aria-hidden="true">
                        <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="scholarModalLabel">Select Publications to Import</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <div id="scholar-loading" class="text-center my-4 d-none">
                                        <div class="spinner-border text-primary"></div>
                                        <p class="mt-2">Fetching from Google Scholar...</p>
                                    </div>

                                    <div id="scholar-table-wrapper" class="table-responsive d-none">
                                        <table class="table table-bordered align-middle">
                                            <thead>
                                                <tr>
                                                    <th><input type="checkbox" id="select-all"></th>
                                                    <th>Title</th>
                                                    <th>Authors</th>
                                                    <th>Journal</th>
                                                    <th>Year</th>
                                                    <th>Link</th>
                                                </tr>
                                            </thead>
                                            <tbody id="scholar-table-body"></tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                    <button id="btn-import-selected" class="btn btn-success" disabled>Import Selected</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif

                    <!-- Table -->
                    <div class="table-responsive">
                        <table id="publications-table" class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Title</th>
                                    <th>First Author</th>
                                    <th>Type</th>
                                    <th>Journal/Conference</th>
                                    <th>Faculty</th>
                                    <th>Department</th>
                                    <th>Start Date</th>
                                    <th>End Date</th>
                                    <th>Peer Reviewed</th>
                                    <th>Scopus</th>
                                    <th>SCI</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>
    // 🔹 Global CSRF setup for all fetch() calls
    window.csrfToken = document.querySelector('meta[name="csrf-token"]').content;

    $(document).ready(function() {
        const sheetName = "Publications";

        var initAjaxRoute = function(route) {
            table = $("#publications-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: 'first_author_name', name: 'first_author_name', orderable: true },
                    { data: 'activity_type', name: 'activity_type', orderable: true },
                    { data: 'affiliation', name: 'affiliation', orderable: true },
                    { data: 'user_name', name: 'user_name', orderable: false },
                    { data: 'department_name', name: 'department_name', orderable: false },
                    { data: 'start_date', name: 'start_date', orderable: true },
                    { data: 'end_date', name: 'end_date', orderable: true },
                    { data: 'is_peer_reviewed', name: 'is_peer_reviewed', orderable: true },
                    { data: 'is_scopus_indexed', name: 'is_scopus_indexed', orderable: false },
                    { data: 'is_sci_indexed', name: 'is_sci_indexed', 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.PublicationsResponses.data') }}";

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

        initAjaxRoute(dataRoute);
    });

    // 🔹 Handle delete button click (for AJAX DataTable)
    $(document).on('click', '.delete-btn', function () {
        const button = $(this);
        const id = button.data('id');
        const url = button.data('url');

        if (!confirm('Are you sure you want to delete this publication?')) return;

        fetch(url, {
            method: 'DELETE',
            headers: {
                'X-CSRF-TOKEN': window.csrfToken,
                'Accept': 'application/json',
            },
        })
            .then(res => res.json())
            .then(data => {
                alert(data.success || 'Deleted successfully');
                $('#publications-table').DataTable().ajax.reload(null, false);
            })
            .catch(err => {
                console.error(err);
                alert('Failed to delete publication.');
            });
    });

    // 🔹 Google Scholar Integration Script (Faculty Only)
    @if(auth()->user()->role->name === 'Faculty')
    const fetchBtn = document.getElementById('btn-fetch-scholar');
    if (fetchBtn) {
        const modal = new bootstrap.Modal(document.getElementById('scholarModal'));
        const body = document.getElementById('scholar-table-body');
        const loading = document.getElementById('scholar-loading');
        const tableWrap = document.getElementById('scholar-table-wrapper');
        const importBtn = document.getElementById('btn-import-selected');
        const scholarUrlInput = document.getElementById('scholar-url');

        fetchBtn.addEventListener('click', () => {
            const scholarUrl = scholarUrlInput.value.trim();

            if (!scholarUrl) {
                alert('Please set your Google Scholar profile URL in your profile settings first.');
                return;
            }

            modal.show();
            loading.classList.remove('d-none');
            tableWrap.classList.add('d-none');
            body.innerHTML = '';

            // ✅ No need to redeclare scholarUrl here again
            fetch("{{ route('faculty.publications.fetchScholar') }}", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                    "X-CSRF-TOKEN": window.csrfToken
                },
                body: JSON.stringify({ url: scholarUrl })
            })
            .then(res => res.json())
            .then(data => {
                loading.classList.add('d-none');
                if (data.error) {
                    body.innerHTML = `<tr><td colspan='6' class='text-danger text-center'>${data.error}</td></tr>`;
                } else {
                    const pubs = data.data;
                    pubs.forEach((p, i) => {
                        body.innerHTML += `
                            <tr>
                                <td><input type="checkbox" class="pub-check" data-index="${i}"></td>
                                <td>${p.title}</td>
                                <td>${p.authors || '-'}</td>
                                <td>${p.journal || '-'}</td>
                                <td>${p.year || '-'}</td>
                                <td><a href="${p.link}" target="_blank">View</a></td>
                            </tr>`;
                    });
                    tableWrap.classList.remove('d-none');
                    importBtn.disabled = false;
                }
            })
            .catch(() => {
                loading.classList.add('d-none');
                body.innerHTML = `<tr><td colspan='6' class='text-danger text-center'>Failed to fetch data.</td></tr>`;
            });
        });

        document.getElementById('select-all').addEventListener('change', function() {
            document.querySelectorAll('.pub-check').forEach(cb => cb.checked = this.checked);
        });

        importBtn.addEventListener('click', () => {
            const selected = Array.from(document.querySelectorAll('.pub-check:checked')).map(cb => {
                const row = cb.closest('tr').children;
                return {
                    title: row[1].innerText.trim(),
                    authors: row[2].innerText.trim(),
                    journal: row[3].innerText.trim(),
                    year: row[4].innerText.trim(),
                    link: row[5].querySelector('a')?.href || null
                };
            });

            if (selected.length === 0) return alert("Please select at least one publication.");

            fetch("{{ route('faculty.publications.importScholar') }}", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                    "X-CSRF-TOKEN": window.csrfToken
                },
                body: JSON.stringify({ publications: selected })
            })
            .then(res => res.json())
            .then(data => {
                alert(data.message);
                modal.hide();
                location.reload();
            })
            .catch(() => alert("Import failed!"));
        });
    }
    @endif
</script>
@endsection
