</div> <!-- End app-container -->

    <!-- Bootstrap 5 Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- SweetAlert2 -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.7.12/dist/sweetalert2.all.min.js"></script>
    <!-- DataTables -->
    <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap5.min.js"></script>
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    
    <!-- Theme Toggle and Init Scripts -->
    <script>
    $(document).ready(function() {
        // Init DataTables with Thai language
        if ($('.datatable').length > 0) {
            $('.datatable').DataTable({
                language: {
                    url: 'https://cdn.datatables.net/plug-ins/1.13.4/i18n/th.json'
                }
            });
        }
        
        // Theme switcher
        const savedTheme = localStorage.getItem('theme') || 'light';
        document.documentElement.setAttribute('data-bs-theme', savedTheme);
        updateThemeToggleIcon(savedTheme);

        $('#btnThemeToggle').on('click', function(e) {
            e.preventDefault();
            const currentTheme = document.documentElement.getAttribute('data-bs-theme') || 'light';
            let newTheme = 'light';
            if (currentTheme === 'light') {
                newTheme = 'dark';
            } else if (currentTheme === 'dark') {
                newTheme = 'egg';
            } else {
                newTheme = 'light';
            }
            document.documentElement.setAttribute('data-bs-theme', newTheme);
            localStorage.setItem('theme', newTheme);
            updateThemeToggleIcon(newTheme);
        });

        function updateThemeToggleIcon(theme) {
            const icon = $('#btnThemeToggle i');
            icon.removeClass('bi-moon-fill bi-sun-fill bi-egg-fill');
            if (theme === 'dark') {
                icon.addClass('bi-egg-fill');
            } else if (theme === 'egg') {
                icon.addClass('bi-sun-fill');
            } else {
                icon.addClass('bi-moon-fill');
            }
        }
    });
    </script>

    <?php 
    $alert_success = $success ?? '';
    if (empty($alert_success) && isset($_GET['success'])) {
        $alert_success = "ดำเนินการสำเร็จเรียบร้อย";
    }
    
    $alert_error = $error ?? '';
    if (empty($alert_error) && isset($_GET['error'])) {
        $alert_error = $_GET['error'];
    }
    ?>

    <?php if (!empty($alert_success)): ?>
    <script>
        Swal.fire({
            icon: 'success',
            title: 'สำเร็จ',
            text: '<?php echo addslashes($alert_success); ?>',
            confirmButtonColor: '#3085d6',
            confirmButtonText: 'ตกลง'
        });
    </script>
    <?php endif; ?>

    <?php if (!empty($alert_error)): ?>
    <script>
        Swal.fire({
            icon: 'error',
            title: 'เกิดข้อผิดพลาด',
            text: '<?php echo addslashes($alert_error); ?>',
            confirmButtonColor: '#3085d6',
            confirmButtonText: 'ตกลง'
        });
    </script>
    <?php endif; ?>
</body>
</html>
