/**
 * Auth: Vastvikta Nishad 
 * Date: 05/03/2024
 * this js file is for handling  editing and deleting for Omnichannel configuration 
 */
jQuery(function ($){
    var Admin = {
        init: function (){
            jQuery("body").on('click','.delete_channel',this.HandleConfigDelete);
    }, 
    HandleConfigDelete: function () {
        console.log("Config Delete Function Triggered");
        if (confirm("Are you sure to delete this ?")) {
            let id = $(this).data('id');
            let channel = $(this).data('channel'); 
            console.log(id);
            console.log(channel);
            $.ajax({
                method: 'POST',
                url: 'omnichannel_config/config_function.php',
                data: { 'id': id, 'channel': channel, 'action': 'ChannelDelete' },
                success: function (response) {
                    $('#success').html('<div class="alert alert-success alert-dismissible" role="alert">Deletion Successful</div>');
                    setTimeout(function () {
                        location.reload();
                    }, 2000);
                }
            });
        }
    }
}
    Admin.init();
});


// Modified By farhan Akhtar For file Upload using ZIP on :: 31-10-2025

$(document).ready(function() {

    // Handle manual file selection
    $('#attachment').on('change', function(e) {
        const files = e.target.files;
        const fileList = $('.fileList');
        const liValuesInput = $('#liValues');

        // Remove "No attachments" message if present
        fileList.find('.no-attachment').remove();

        for (let i = 0; i < files.length; i++) {
            const fileName = files[i].name;

            const li = `
                <li class='attachment-item' data-file='${fileName}'>
                    ${fileName}
                    <button type='button' class='btn btn-sm btn-danger remove-attachment' style='margin-left:5px;'>Remove</button>
                </li>
            `;
            fileList.append(li);

            // Update hidden input
            const currentValues = liValuesInput.val() ? liValuesInput.val().split(',') : [];
            currentValues.push(fileName);
            liValuesInput.val(currentValues.join(','));
        }

        // Reset file input so the same file can be chosen again if needed
        $(this).val('');
    });

    // Handle remove button
    $(document).on('click', '.remove-attachment', function() {
        const li = $(this).closest('li');
        const file = li.data('file');
        li.remove();

        // Update hidden field
        let values = $('#liValues').val().split(',').filter(Boolean);
        values = values.filter(v => v.trim() !== file);
        $('#liValues').val(values.join(','));

        // If list becomes empty, show "No attachments"
        if ($('.fileList').children().length === 0) {
            $('.fileList').html("<li class='text-muted no-attachment'>No attachments</li>");
        }
    });

});
// Close window on cross icon click
document.getElementById("closeButton").addEventListener("click", function () {
    window.close();
});

function validate(){
    if(document.frmums.V_EmailId.value == ''){
        alert("Please Enter Email!");
        document.frmums.V_EmailId.focus();
        return false;
    }
    if(document.frmums.V_Subject.value == ''){
        alert("Please Enter Subject!");
        document.frmums.V_Subject.focus();
        return false;
    }else{
        document.frmums.submit();
    }
}