// Global AJAX setup
$.ajaxSetup({
    beforeSend: function(xhr) {
        if ($('meta[name="csrf-token"]').length > 0) {
            xhr.setRequestHeader('X-CSRF-TOKEN', $('meta[name="csrf-token"]').attr('content'));
        }
    },
    error: function(xhr, status, error) {
        if (xhr.status === 401) {
            window.location.href = '/auth/login.php';
        } else if (xhr.responseJSON && xhr.responseJSON.message) {
            alert('Error: ' + xhr.responseJSON.message);
        } else {
            alert('An error occurred. Please try again.');
        }
    }
});

// Initialize tooltips
$(function () {
    $('[data-bs-toggle="tooltip"]').tooltip();
});

// Handle form submissions with loading state
$(document).on('submit', 'form[data-ajax-form]', function(e) {
    e.preventDefault();
    const form = $(this);
    const submitBtn = form.find('button[type="submit"]');
    const originalText = submitBtn.html();
    
    submitBtn.prop('disabled', true);
    submitBtn.html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Processing...');
    
    $.ajax({
        url: form.attr('action'),
        method: form.attr('method'),
        data: form.serialize(),
        success: function(response) {
            if (response.redirect) {
                window.location.href = response.redirect;
            } else if (response.message) {
                alert(response.message);
                if (response.reload) {
                    window.location.reload();
                }
            }
        },
        complete: function() {
            submitBtn.prop('disabled', false);
            submitBtn.html(originalText);
        }
    });
});

// Toggle sidebar on mobile
$('#sidebarToggle').on('click', function() {
    $('.sidebar').toggleClass('active');
});

// Auto-hide alerts after 5 seconds
$(document).ready(function() {
    setTimeout(function() {
        $('.alert').fadeTo(500, 0).slideUp(500, function() {
            $(this).remove(); 
        });
    }, 5000);
});

// Initialize all DataTables with default options
$(document).ready(function() {
    $('.datatable').DataTable({
        responsive: true,
        dom: '<"top"f>rt<"bottom"ip><"clear">',
        pageLength: 25
    });
});

/* Add these to your existing CSS */
.main-header {
    background: white;
    padding: 15px 20px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    margin-bottom: 20px;
}

.date-display {
    font-weight: 500;
    color: #495057;
}

.content-container {
    padding: 0 20px;
}

/* Error color coding */
.error-badge {
    font-size: 0.75rem;
    padding: 0.35em 0.65em;
}

.bg-error-light {
    background-color: #ffebee;
}

.bg-warning-light {
    background-color: #fff8e1;
}

.bg-success-light {
    background-color: #e8f5e9;
}

/* Sidebar fixes */
.wrapper {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 250px;
    position: fixed;
    height: 100vh;
    overflow-y: auto;
}


/* Responsive fixes */
@media (max-width: 992px) {
    .sidebar {
        margin-left: -250px;
    }
    .main-content {
        width: 100%;
        margin-left: 0;
    }
    .sidebar.active {
        margin-left: 0;
    }
}
/* Main Layout Structure */
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f8f9fa;
    margin: 0;
    padding: 0;
    display: flex;
    min-height: 100vh;
}

.wrapper {
    display: flex;
    width: 100%;
}

/* Sidebar Styles */
.sidebar {
    width: 250px;
    background-color: #2c3e50;
    color: white;
    position: fixed;
    height: 100vh;
    overflow-y: auto;
    transition: all 0.3s;
    z-index: 1000;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-menu {
    list-style: none;
    padding: 0;
    margin: 20px 0;
}

.sidebar-menu li a {
    display: flex;
    align-items: center;
    padding: 10px 20px;
    color: white;
    text-decoration: none;
    transition: all 0.3s;
}

.sidebar-menu li a:hover {
    background-color: #34495e;
}

.sidebar-menu li a.active {
    background-color: #0d6efd;
}

.sidebar-menu li a i {
    margin-right: 10px;
}

.sidebar-footer {
    padding: 20px;
    position: absolute;
    bottom: 0;
    width: 100%;
}

/* Main Content Area */
.main-content {
    margin-left: 250px;
    width: calc(100% - 250px);
    min-height: 100vh;
    display: inline;
   
}

/* Header Styles */
.main-header {
    background: white;
    padding: 15px 30px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 100;
}

.date-display {
    font-weight: 500;
    color: #495057;
    font-size: 1rem;
}

.user-menu .dropdown-toggle {
    display: flex;
    align-items: center;
}

.user-menu .dropdown-toggle i {
    margin-right: 8px;
}

/* Content Container */
.content-container {
    flex: 1;
    padding: 30px;
    background-color: #f8f9fa;
}

/* Responsive Adjustments */
@media (max-width: 992px) {
    .sidebar {
        margin-left: -250px;
    }
    
    .sidebar.active {
        margin-left: 0;
    }
    
    .main-content {
        width: 100%;
        margin-left: 0;
    }
    
    .main-header {
        padding: 15px 20px;
    }
    
    .content-container {
        padding: 20px;
    }
}

/* Toggle Button for Mobile */
#sidebarToggle {
    display: none;
    background: none;
    border: none;
    color: #495057;
    font-size: 1.5rem;
    margin-right: 15px;
}

@media (max-width: 992px) {
    #sidebarToggle {
        display: block;
    }
}

/* Error color coding */
.error-badge {
    font-size: 0.75rem;
    padding: 0.35em 0.65em;
}

.bg-error-light {
    background-color: #ffebee;
}

.bg-warning-light {
    background-color: #fff8e1;
}

.bg-success-light {
    background-color: #e8f5e9;
}

/* Card hover effect */
.card-hover:hover {
    transform: translateY(-2px);
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
}
#errorsTable_wrapper .dataTables_length,
#errorsTable_wrapper .dataTables_filter {
    margin-bottom: 1rem;
}

#errorsTable_wrapper .dataTables_paginate .paginate_button {
    border: 1px solid #dee2e6;
    padding: 0.3rem 0.75rem;
    margin: 0 0.15rem;
    border-radius: 0.25rem;
}

#errorsTable_wrapper .dataTables_paginate .paginate_button.current {
    background: #0d6efd;
    color: white !important;
    border-color: #0d6efd;
}
.assignment-container {
    min-height: 60px;
}

.exception-employee {
    color: #0d6efd;
    font-weight: 500;
}
/* RTL Layout Support */
[dir="rtl"] .sidebar-menu li a i {
    margin-right: 0;
    margin-left: 10px;
}

[dir="rtl"] .sidebar-footer .logout-btn i {
    margin-right: 0;
    margin-left: 8px;
}

[dir="rtl"] .main-header .user-menu .dropdown-toggle i {
    margin-right: 0;
    margin-left: 8px;
}

[dir="rtl"] .dropdown-menu {
    text-align: right;
}

/* Ensure proper text alignment based on direction */
.rtl-text {
    text-align: right;
}

.ltr-text {
    text-align: left;
}

/* Dynamic alignment helper */
.dynamic-align {
    text-align: var(--text-align, left);
}

:root[dir="rtl"] {
    --text-align: right;
}

:root[dir="ltr"] {
    --text-align: left;
}
/* Exception employee styling */
.exception-employee {
    color: #0d6efd; /* Dark blue color */
    font-weight: 600;
    background-color: rgba(13, 110, 253, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    border: 1px solid rgba(13, 110, 253, 0.3);
}

/* For Excel export - add this style */
.exception-excel {
    color: #0d6efd;
    font-weight: bold;
}