/* Korea Tourism Agency Admin Panel Custom Scripts */ $(document).ready(function() { // Initialize tooltips $('[data-toggle="tooltip"]').tooltip(); // Initialize popovers $('[data-toggle="popover"]').popover(); // Auto-hide alerts after 5 seconds setTimeout(function() { $('.alert').fadeOut('slow'); }, 5000); // Confirm delete actions $('.btn-delete').on('click', function(e) { e.preventDefault(); const item = $(this).data('item') || 'item'; const url = $(this).attr('href') || $(this).data('url'); if (confirm(`Are you sure you want to delete this ${item}?`)) { if ($(this).data('method') === 'DELETE') { // AJAX delete $.ajax({ url: url, method: 'DELETE', success: function(response) { if (response.success) { location.reload(); } else { alert('Error: ' + response.message); } }, error: function() { alert('An error occurred while deleting.'); } }); } else { // Regular form submission or redirect window.location.href = url; } } }); // Form validation enhancement $('form').on('submit', function() { const submitBtn = $(this).find('button[type="submit"]'); submitBtn.prop('disabled', true); submitBtn.html(' Processing...'); }); // Image preview functionality function readURL(input, target) { if (input.files && input.files[0]) { const reader = new FileReader(); reader.onload = function(e) { $(target).attr('src', e.target.result).show(); }; reader.readAsDataURL(input.files[0]); } } $('input[type="file"]').on('change', function() { const targetImg = $(this).closest('.form-group').find('.img-preview'); if (targetImg.length) { readURL(this, targetImg); } }); // Auto-save draft functionality for forms let autoSaveTimer; $('textarea, input[type="text"]').on('input', function() { clearTimeout(autoSaveTimer); autoSaveTimer = setTimeout(function() { // Auto-save logic here console.log('Auto-saving draft...'); }, 2000); }); // Enhanced DataTables configuration if (typeof $.fn.dataTable !== 'undefined') { $('.data-table').each(function() { $(this).DataTable({ responsive: true, lengthChange: false, autoWidth: false, pageLength: 25, language: { search: "Search:", lengthMenu: "Show _MENU_ entries", info: "Showing _START_ to _END_ of _TOTAL_ entries", paginate: { first: "First", last: "Last", next: "Next", previous: "Previous" } }, dom: '<"row"<"col-sm-6"l><"col-sm-6"f>>' + '<"row"<"col-sm-12"tr>>' + '<"row"<"col-sm-5"i><"col-sm-7"p>>', }); }); } // Status toggle functionality $('.status-toggle').on('change', function() { const checkbox = $(this); const id = checkbox.data('id'); const type = checkbox.data('type'); const field = checkbox.data('field'); const isChecked = checkbox.is(':checked'); $.ajax({ url: `/admin/${type}/${id}/toggle`, method: 'POST', data: { field: field, value: isChecked }, success: function(response) { if (response.success) { showNotification('Status updated successfully!', 'success'); } else { checkbox.prop('checked', !isChecked); showNotification('Error updating status: ' + response.message, 'error'); } }, error: function() { checkbox.prop('checked', !isChecked); showNotification('Error updating status', 'error'); } }); }); // Notification system function showNotification(message, type = 'info') { const alertClass = type === 'success' ? 'alert-success' : type === 'error' ? 'alert-danger' : type === 'warning' ? 'alert-warning' : 'alert-info'; const notification = `