<script>
// Toggle CSV/API columns based on source type
function toggleSourceColumns() {
    let sourceType = $('#source_type').val();

    if (sourceType === 'CSV') {
        $('.csv-col').show();
        $('.api-col').hide();
        $('#csv-settings').show();
    } else if (sourceType === 'API') {
        $('.csv-col').hide();
        $('.api-col').show();
        $('#csv-settings').hide();
    } else { // BOTH
        $('.csv-col').show();
        $('.api-col').show();
        $('#csv-settings').show();
    }
}

// Toggle IN/OUT mapping based on punch mode
function toggleInOutMapping() {
    let punchMode = $('#punch_mode').val();

    if (punchMode === 'SINGLE') {
        $('#in-out-mapping-card').hide();
    } else {
        $('#in-out-mapping-card').show();
        toggleInOutStructure();
    }
}

// Toggle IN/OUT structure options
function toggleInOutStructure() {
    let structure = $('input[name="in_out_structure"]:checked').val();

    // Hide all
    $('#separate-columns-info').hide();
    $('#same-column-values').hide();
    $('#time-based-info').hide();

    // Remove active class from all cards
    $('.structure-option').removeClass('bg-light');

    // Show relevant section
    if (structure === 'SEPARATE_COLUMNS') {
        $('#separate-columns-info').show();
        $('.structure-option[data-structure="SEPARATE_COLUMNS"]').addClass('bg-light');
    } else if (structure === 'SAME_COLUMN') {
        $('#same-column-values').show();
        $('.structure-option[data-structure="SAME_COLUMN"]').addClass('bg-light');
    } else if (structure === 'TIME_BASED') {
        $('#time-based-info').show();
        $('.structure-option[data-structure="TIME_BASED"]').addClass('bg-light');
    }
}

// Collect field mappings into hidden fields
function collectFieldMappings() {
    // Field mappings are already in form inputs with proper names
    // Just ensure empty string inputs are set to null
    $('#mappingTable input[type="number"]').each(function() {
        if ($(this).val() === '') {
            $(this).val(null);
        }
    });
}

// Collect IN/OUT mappings
function collectInOutMappings() {
    let structure = $('input[name="in_out_structure"]:checked').val() || 'SAME_COLUMN';
    let inValues = $('#in_values').val() ? $('#in_values').val().split(',').map(v => v.trim()).filter(v => v) : [];
    let outValues = $('#out_values').val() ? $('#out_values').val().split(',').map(v => v.trim()).filter(v => v) : [];
    let caseSensitive = $('#case_sensitive').is(':checked');

    // Remove any existing in_out_mapping hidden inputs
    $('input[name^="in_out_mapping"]').remove();

    // Add hidden inputs for the mapping
    let form = $('#settingsForm');

    // Add structure
    form.append('<input type="hidden" name="in_out_mapping[structure]" value="' + structure + '">');

    // Add values only for SAME_COLUMN mode
    if (structure === 'SAME_COLUMN') {
        inValues.forEach((v, i) => {
            form.append('<input type="hidden" name="in_out_mapping[in_values][' + i + ']" value="' + v + '">');
        });

        outValues.forEach((v, i) => {
            form.append('<input type="hidden" name="in_out_mapping[out_values][' + i + ']" value="' + v + '">');
        });

        form.append('<input type="hidden" name="in_out_mapping[case_sensitive]" value="' + (caseSensitive ? '1' : '0') + '">');
    }
}

// Initialize on page load
$(document).ready(function() {
    toggleSourceColumns();
    toggleInOutMapping();
    toggleInOutStructure();

    $('#source_type').on('change', toggleSourceColumns);
    $('#punch_mode').on('change', toggleInOutMapping);
    $('input[name="in_out_structure"]').on('change', toggleInOutStructure);

    // Make structure cards clickable
    $('.structure-option').on('click', function() {
        $(this).find('input[type="radio"]').prop('checked', true).trigger('change');
    });
});
</script>
<?php /**PATH /home/codeproh/hr.vadistilleries.lk/app/Modules/HR/Attendance/Providers/../Views/settings/partials/scripts.blade.php ENDPATH**/ ?>