<!-- JAVASCRIPT -->
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"
        integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script src="{{ asset('assets/libs/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
    <script src="{{ asset('assets/libs/simplebar/simplebar.min.js') }}"></script>
    <script src="{{ asset('assets/libs/node-waves/waves.min.js') }}"></script>
    <script src="{{ asset('assets/libs/feather-icons/feather.min.js') }}"></script>
    <script src="{{ asset('assets/js/pages/plugins/lord-icon-2.1.0.js') }}"></script>
    <script src="{{ asset('assets/js/plugins.js') }}"></script>

    <script>
        // Initialize Bootstrap Tooltips
        document.addEventListener('DOMContentLoaded', function () {
            var tooltipTriggerList = [].slice.call(document.querySelectorAll('[title], [data-bs-toggle="tooltip"]'))
            var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
                return new bootstrap.Tooltip(tooltipTriggerEl)
            })
        });
    </script>

    <script src="{{ asset('assets/js/app.js') }}"></script>
    <script src="{{ asset('assets/js/ajax-form.js') }}"></script>
    <script src="{{ asset('assets/js/datatable-filters.js') }}"></script>

    <!-- DataTables Core & Bootstrap 5 Integration -->
    <script src="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.7/js/dataTables.bootstrap5.min.js"></script>

    <!-- DataTables Buttons Extension & Dependencies -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.bootstrap5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.print.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.colVis.min.js"></script>

    <!-- DataTables Responsive Extension -->
    <script src="https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.5.0/js/responsive.bootstrap5.min.js"></script>


    <script src="{{ asset('assets/js/preview-image.js') }}"></script>
    <script src="{{ asset('assets/js/preview-multi-image.js') }}"></script>

    <!-- SweetAlert2 -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <!-- Delete & Status Toggle Handlers -->
    <script>
        $(document).ready(function() {
            // Delete button handler with confirmation
            $(document).on('click', '.delete-btn', function(e) {
                e.preventDefault();
                var btn = $(this);
                var url = btn.data('url');
                var name = btn.data('name') || '{{ __("dashboard.this_record") }}';

                Swal.fire({
                    title: '{{ __("dashboard.confirm_delete") }}',
                    html: '{!! __("dashboard.confirm_delete_message") !!}'.replace(':name', '<strong>' + name + '</strong>'),
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#d33',
                    cancelButtonColor: '#6c757d',
                    confirmButtonText: '{{ __("dashboard.yes_delete") }}',
                    cancelButtonText: '{{ __("dashboard.cancel") }}'
                }).then((result) => {
                    if (result.isConfirmed) {
                        $.ajax({
                            url: url,
                            type: 'DELETE',
                            data: { _token: '{{ csrf_token() }}' },
                            success: function(response) {
                                if (response.success !== false) {
                                    Toastify({
                                        text: response.message || '{{ __("dashboard.deleted_success") }}',
                                        className: "bg-success",
                                        duration: 3000
                                    }).showToast();

                                    // Reload DataTable if exists
                                    if ($.fn.DataTable) {
                                        $('.dataTable').each(function() {
                                            if ($.fn.DataTable.isDataTable(this)) {
                                                $(this).DataTable().ajax.reload(null, false);
                                            }
                                        });
                                    }
                                } else {
                                    Toastify({
                                        text: response.message || '{{ __("dashboard.delete_failed") }}',
                                        className: "bg-danger",
                                        duration: 3000
                                    }).showToast();
                                }
                            },
                            error: function(xhr) {
                                Toastify({
                                    text: xhr.responseJSON?.message || '{{ __("dashboard.delete_failed") }}',
                                    className: "bg-danger",
                                    duration: 3000
                                }).showToast();
                            }
                        });
                    }
                });
            });

            // Status toggle handler
            $(document).on('click', '.status-toggle', function(e) {
                e.preventDefault();
                var btn = $(this);
                var url = btn.data('url');

                $.ajax({
                    url: url,
                    type: 'POST',
                    data: { _token: '{{ csrf_token() }}' },
                    success: function(response) {
                        if (response.success !== false) {
                            Toastify({
                                text: response.message || '{{ __("dashboard.status_updated") }}',
                                className: "bg-success",
                                duration: 3000
                            }).showToast();

                            // Reload DataTable if exists
                            if ($.fn.DataTable) {
                                $('.dataTable').each(function() {
                                    if ($.fn.DataTable.isDataTable(this)) {
                                        $(this).DataTable().ajax.reload(null, false);
                                    }
                                });
                            }
                        } else {
                            Toastify({
                                text: response.message || '{{ __("dashboard.status_update_failed") }}',
                                className: "bg-danger",
                                duration: 3000
                            }).showToast();
                        }
                    },
                    error: function(xhr) {
                        Toastify({
                            text: xhr.responseJSON?.message || '{{ __("dashboard.status_update_failed") }}',
                            className: "bg-danger",
                            duration: 3000
                        }).showToast();
                    }
                });
            });
        });
    </script>

    @stack('scripts')
