//jshint esversion:6
$.ajaxSetup({
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
});
function loadModal(code){
    $("#"+code).modal("show");
}
function closeModal(code){
    $("#"+code).modal("hide");
}
var getInitials = function (string) {
    var names = string.split(' '),
        initials = names[0].substring(0, 1).toUpperCase();
    if (names.length > 1) {
        initials += names[names.length - 1].substring(0, 1).toUpperCase();
    }
    return initials;
};
function unloadUserInfo(){
    $.ajax({
        url: "/user/profile/get",
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        type: "GET",
        cache: false,
        success: function (response) {
            // console.log(response);
            $(".firstname").html(response.user.firstname);
            $(".lastname").html(response.user.lastname);
            phone = response.user.phone_number!=""?response.user.phone_number:'Not added yet';
            $(".phone").html(phone);
            $(".address").html(response.user.address+', '+response.user.state+', '+response.country);
            $(".nextname").html( response.next.next_of_kin_name);
            $(".nextphone").html( response.next.next_of_kin_phone);
            $(".nextemail").html( response.next.next_of_kin_email);
            $(".nextaddress").html( response.next.next_of_kin_address);
            initials = getInitials(response.user.lastname+' '+response.user.firstname);
            if(response.user.profile_picture==null){
                $(".profile-picture").html(initials);
            }else{
                $(".profile-picture").html('<img src="/images/avatar/'+response.user.profile_picture+'">');
            }
            // EDITS
            $("#firstname").val(response.user.firstname);
            $("#lastname").val(response.user.lastname);
            $("#middlename").val(response.user.middlename);
            $("#phone").val(response.user.phone_number);
            $("#address").val(response.user.address);
            $("#state").val(response.user.state);
            $("#dob").val(response.user.date_of_birth);
            $("#country").val(response.user.country).change();
            $("#nextname").val(response.next.next_of_kin_name);
            $("#nextphone").val(response.next.next_of_kin_phone);
            $("#nextemail").val(response.next.next_of_kin_email);
            $("#nextaddress").val(response.next.next_of_kin_address);

        },
        error:function(response){
            // console.log(response);
        }
    });
}unloadUserInfo();
$(document).on("click",".mark-read", function(){
    id = $(this).attr('id');
    $.ajax({
        type: "POST",
        url: "dashboard/notification/mark-read",
        data: "id="+id,
        cache: false,
        success: function (response) {
            // console.log(response);
        }
    });
});

//Change user password
$(document).on("submit","#change-password-form",function(e){
    $(".pw-btn").prop("disabled",true);
    $(".pw-spinner").show();
    e.preventDefault();
    $.ajax({
        url: "/user/profile/password/change",
        type: "PATCH",
        data: $("#change-password-form").serialize(),
        cache: false,
        success: function(response){
            // console.log(response);
            if(response.status === "success"){
                NioApp.Toast("<h4>"+response.title+"</h4>"+response.message, response.status,{position:'top-right'});
                closeModal('change-password');
                $('.modal-backdrop').remove();
                $(':input','#change-password-form').not(':disabled, :button, :submit, :reset, :hidden').val('');
            }
            if(response.status === "error"){
                NioApp.Toast("<h4>"+response.title+"</h4>"+response.message, response.status,{position:'top-right'});
            }
            $(".pw-btn").prop("disabled",false);
            $(".pw-spinner").hide();
        },
        error: function(response){
            // console.log(response);
        }
    });
});

//Change pin
$(document).on("submit","#change-pin-form",function(e){
    $(".pn-btn").prop("disabled",true);
    $(".pn-spinner").show();
    e.preventDefault();
    $.ajax({
        url: "/user/profile/pin/change",
        type: "PATCH",
        data: $("#change-pin-form").serialize(),
        cache: false,
        success: function(response){
            // console.log(response);
            if(response.status === "success"){
                NioApp.Toast("<h4>"+response.title+"</h4>"+response.message, response.status,{position:'top-right'});
                closeModal('change-pin');
                $('.modal-backdrop').remove();
                $(':input','#change-pin-form').not(':disabled, :button, :submit, :reset, :hidden').val('');
            }
            if(response.status === "error"){
                NioApp.Toast("<h4>"+response.title+"</h4>"+response.message, response.status,{position:'top-right'});
            }
            $(".pn-btn").prop("disabled",false);
            $(".pn-spinner").hide();
        },
        error: function(response){
            // console.log(response);
        }
    });
});

// Datatables
NioApp.DataTable('.statement-table', {
    responsive: {details: true},
    order:[[7,'DESC']]
});
NioApp.DataTable('.loan-table', {
    responsive: {details: true},
    order:[[2,'DESC']]
});
