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

let table = $('#loans-init').DataTable({
    processing: true,
    serverSide: true,
    ajax: "/admin/loans/load",
    columns: [
        {
            data: 'DT_RowIndex',
            name: 'DT_RowIndex',
            orderable: false,
            searchable: false,
            className: 'nk-tb-col'
        },
        {
            data: 'username',
            name: 'Username',
            searchable: true,
            orderable: false,
            className: 'nk-tb-col'
        },
        {
            data: 'amount',
            name: 'Amount',
            searchable: true,
            orderable: false,
            className: 'nk-tb-col'
        },
        {
            data: 'status',
            name: 'status',
            className: 'nk-tb-col',
            orderable: false,
            searchable: false
        },
        {
            data: 'created_at',
            name: 'date/time',
            className: 'nk-tb-col',
            orderable: false,
            searchable: false
        },
    ],
    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", ".btn-toggle",function(e){
    e.preventDefault();
    Swal.fire({
        title: $(this).data('status').toUpperCase()+' USER LOAN?',
        html: 'You are about to '+$(this).data('status')+' this user\'s loan request',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes, '+$(this).data('status').charAt(0).toUpperCase()+$(this).data('status').slice(1)+'!'
        }).then((result) => {
        if (result.value) {
            $.ajax({
                url: "/admin/loan/toggle",
                type: "PATCH",
                data: "id="+$(this).attr("id")+"&status="+$(this).data('status'),
                cache: false,
                success:function(response){
                    console.log(response);
                    NioApp.Toast("<h6>"+response.title+"</h6>"+response.message,response.status,{position: 'top-right'});
                    table.draw();
                },
                error: function(response){
                    console.log(response);
                }
            });
        }
    });

});

