(function ($) {
    "use strict";

    // Initial state check
    toggleFields($('#type').val());

    // Handle change using event delegation
    $(document).on('change', '#type', function () {
        let selectedType = $(this).val();
        toggleFields(selectedType);
    });

    function toggleFields(type) {
        if (type == '2') {
            // From Alumni
            $('.old-type-input').show();
            $('.new-type-input').hide();
        } else {
            // New
            $('.old-type-input').hide();
            $('.new-type-input').show();
        }
    }

    $('#roles').select2({
        dropdownCssClass: "sf-select-dropdown",
        selectionCssClass: "sf-select-section",
        dropdownParent: $('#add-modal')
    });

    $("#moderatorDataTable").DataTable({
        pageLength: 10,
        ordering: false,
        serverSide: true,
        responsive: true,
        processing: true,
        searching: true,
        ajax: $('#moderator-list-route').val(),
        language: {
            paginate: {
                previous: "<i class='fa-solid fa-angles-left'></i>",
                next: "<i class='fa-solid fa-angles-right'></i>",
            },
            searchPlaceholder: "Search Here",
            search: "<span class='searchIcon'><i class='fa-solid fa-magnifying-glass'></i></span>",
        },
        dom: '<"tableTop"<"row align-items-center"<"col-sm-6"<"tableSearch float-start"f>><"col-sm-6"<"tableLengthInput float-end"l>>>>tr<"tableBottom"<"row align-items-center"<"col-sm-6"<"tableInfo"i>><"col-sm-6"<"tablePagi"p>>>><"clear">',
        columns: [
            {"data": "name", "name": "name", responsivePriority: 1},
            {"data": "email", "name": "email"},
            {"data": "status", "name": "status", searchable: false, responsivePriority: 3},
            {"data": "roles", "name": "roles.display_name"},
            {"data": "action", searchable: false, responsivePriority: 2},
        ],
    });

    window.editReponse = function () {
        $('#roles_edit').select2({
            dropdownCssClass: "sf-select-dropdown",
            selectionCssClass: "sf-select-section",
            dropdownParent: $('#edit-modal')
        });
    }

    window.revokeModerator = function (url, id) {
        Swal.fire({
            title: 'Sure! You want to revoke?',
            text: "You won't be able to revert this!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, Revoke It!'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    type: 'POST',
                    url: url,
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function (data) {

                        toastr.success(data.message);
                        if (typeof id != 'undefined') {
                            $('#' + id).DataTable().ajax.reload();
                        } else {
                            location.reload();
                        }
                    },
                    error: function (error) {
                        toastr.error(error.responseJSON.message)
                    }
                })
            }
        })
    }

    window.addResponseForModal = function (response) {
        console.log("This is commonResponseForModal");
        $('.error-message').remove();
        $('.is-invalid').removeClass('is-invalid');
        if (response['status'] === true) {
            toastr.success(response['message'])

            setTimeout(function () {
                location.reload()
            }, 500)

        } else {
            commonHandler(response)
        }
    }

})(jQuery)
