//jshint esversion:6
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});


let table = $('#transfer-settings-init').DataTable({
    processing: true,
    serverSide: true,
    ajax: "/admin/transfer/settings/load",

    columns: [{
            data: 'username',
            name: 'username',
            orderable: false,
            searchable: true,
            className: 'nk-tb-col'
        },
        {
            data: 'current_transfer_count',
            name: 'Current TF Count',
            className: 'nk-tb-col',
            orderable: false,
            searchable: false
        },
        {
            data: 'max_transfer_count',
            name: 'Max. TF Count',
            searchable: false,
            orderable: false,
            className: 'nk-tb-col'
        },
        {
            data: 'max_transfer_amount',
            name: 'Max. Tf Amount',
            searchable: false,
            orderable: false,
            className: 'nk-tb-col'
        },
        {
            data: 'made_first_transfer',
            name: 'Activated',
            className: 'nk-tb-col',
            orderable: false,
            searchable: false
        },
        {
            data: 'imf_code',
            name: 'Imf Code',
            className: 'nk-tb-col',
            orderable: false,
            searchable: false
        },
    ],
    // responsive: true,
    autoWidth: false,
    dom: '<"row justify-between g-2"<"col-7 col-sm-6 text-left"f><"col-5 col-sm-6 text-right"<"datatable-filter"l>>><"datatable-wrap my-3"t><"row align-items-center"<"col-7 col-sm-12 col-md-9"p><"col-5 col-sm-12 col-md-3 text-left text-md-right"i>>',
    language: {
        search: "",
        searchPlaceholder: "Search TXID or Usernames",
        lengthMenu: "<span class='d-none d-sm-inline-block'>Show</span><div class='form-control-select'> _MENU_ </div>",
        info: "_START_ -_END_ of _TOTAL_",
        infoEmpty: "No records found",
        infoFiltered: "( Total _MAX_  )",
        paginate: {
            "first": "First",
            "last": "Last",
            "next": "Next",
            "previous": "Prev"
        }
    }
});



$(document).on("click", ".set-count", function (e) {
    $("#stc-uid").val($(this).attr("id"));
    $("#set-tf-count").modal("show");
});
$(document).on("click", ".set-amount", function (e) {
    $("#sta-uid").val($(this).attr("id"));
    $("#set-tf-amount").modal("show");
});
$(document).on("click", ".update-count", function (e) {
    $("#utc-uid").val($(this).attr("id"));
    $("#count").val($(this).data('count'));
    $("#update-tf-count").modal("show");
});
$(document).on("click", ".update-amount", function (e) {
    $("#uta-uid").val($(this).attr("id"));
    $("#amount").val($(this).data('amount'));
    $("#update-tf-amount").modal("show");
});



$(document).on("submit", "#set-tfCount-formData", function (e) {
    e.preventDefault();
    if ($("#set-tfCount-formData")[0].checkValidity()) {
        $.ajax({
            url: '/admin/transfer/set-count',
            type: 'POST',
            data: $("#set-tfCount-formData").serialize(),
            success: function (response) {
                // console.log(response);
                NioApp.Toast("<h6>" + response.title + "</h6>" + response.message, response.status, {
                    position: 'top-right'
                });
                table.draw();
                $("#set-tf-count").modal("hide");
                $(':input', '#set-tfCount-formData').not(':disabled, :button, :submit, :reset, :hidden').val('');
            },
            error: function (response) {
                // console.log(response);
            }
        });
    }
});

$(document).on("submit", "#update-tfCount-formData", function (e) {
    e.preventDefault();
    if ($("#update-tfCount-formData")[0].checkValidity()) {
        $.ajax({
            url: '/admin/transfer/update-count',
            type: 'PATCH',
            data: $("#update-tfCount-formData").serialize(),
            success: function (response) {
                // console.log(response);
                NioApp.Toast("<h6>" + response.title + "</h6>" + response.message, response.status, {
                    position: 'top-right'
                });
                $("#update-tf-count").modal("hide");
                table.draw();
            },
            error: function (response) {
                // console.log(response);
            }
        });
    }
});

$(document).on("submit", "#set-tfAmount-formData", function (e) {
    e.preventDefault();
    if ($("#set-tfAmount-formData")[0].checkValidity()) {
        $.ajax({
            url: '/admin/transfer/set-amount',
            type: 'POST',
            data: $("#set-tfAmount-formData").serialize(),
            success: function (response) {
                // console.log(response);
                $(':input', '#set-tfAmount-formData').not(':disabled, :button, :submit, :reset, :hidden').val('');
                NioApp.Toast("<h6>" + response.title + "</h6>" + response.message, response.status, {
                    position: 'top-right'
                });
                $("#set-tf-amount").modal("hide");
                table.draw();
            },
            error: function (response) {
                // console.log(response);
            }
        });
    }
});

$(document).on("submit", "#update-tfAmount-formData", function (e) {
    e.preventDefault();
    if ($("#update-tfAmount-formData")[0].checkValidity()) {
        $.ajax({
            url: '/admin/transfer/update-amount',
            type: 'PATCH',
            data: $("#update-tfAmount-formData").serialize(),
            success: function (response) {
                // console.log(response);
                NioApp.Toast("<h6>" + response.title + "</h6>" + response.message, response.status, {
                    position: 'top-right'
                });
                table.draw();
                $("#update-tf-amount").modal("hide");
            },
            error: function (response) {
                // console.log(response);
            }
        });
    }
});



$(document).on("click", ".btn-toggle", function (e) {
    let btn = $(this).attr("id");
    e.preventDefault();
    Swal.fire({
        title: $(this).data('status').toUpperCase() + ' USER?',
        html: 'You are about to ' + $(this).data('status') + ' this user\'s with respect to making payment during first international transfers',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes, ' + $(this).data('status').charAt(0).toUpperCase() + $(this).data('status').slice(1) + '!'
    }).then((result) => {
        if (result.value) {

            $(".dropdown-"+btn).hide();
            $(".spinner-"+btn).show();

            $.ajax({
                url: "/admin/transfer/first-transfer",
                type: "PATCH",
                data: "id=" + $(this).attr("id") + "&status=" + $(this).data('status'),
                cache: false,
                success: function (response) {

                    // console.log(response);

                    $(".spinner-"+btn).hide();
                    $(".dropdown-"+btn).show();

                    NioApp.Toast("<h6>" + response.title + "</h6>" + response.message, response.status, {
                        position: 'top-right'
                    });
                    table.draw();
                },
                error: function (response) {
                    // console.log(response);
                }
            });
        }
    });

});


$(document).on("click", ".btn-generate", function (e) {
    let btn = $(this).data("id");
    e.preventDefault();
    Swal.fire({
        title: 'GENERATE IMF CODE',
        html: 'You are about to generate IMF Code for this user.',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes, Generate',
    }).then((result) => {
        if (result.value) {

            $(".generate-"+btn).hide();
            $(".spinnerg-"+btn).show();

            $.ajax({
                url: "/admin/imf/generate",
                type: "PATCH",
                data: "id=" + $(this).data("id"),
                cache: false,
                success: function (response) {
                    // console.log(response);

                    $(".spinnerg-"+btn).hide();
                    $(".generate-"+btn).show();

                    NioApp.Toast("<h6>" + response.title + "</h6>" + response.message, response.status, {
                        position: 'top-right'
                    });
                    table.draw();
                },
                error: function (response) {
                    // console.log(response);
                }
            });
        }
    });

});
