//jshint esversion:6
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
$(document).on("submit","#profile-formData",function(e){
    e.preventDefault();
    $.ajax({
        url: "/user/profile/update",
        type: "PATCH",
        data: $("#profile-formData").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'});
                unloadUserInfo();
                closeModal('profile-edit');
                $('.modal-backdrop').remove();
            }else{
                NioApp.Toast('<h4>'+response.title+'</h4>'+response.message, response.status,{position:'top-right'});
            }
        },
        error:function(response){
            console.log(response);
        }
    });
});

$(document).on("submit","#nextofkin-formData",function(e){
    e.preventDefault();
    $(".next-confirm").prop("disabled",true);
    $(".next-spinner").show();
    $.ajax({
        url: "/user/profile/nextofkin/update",
        type: "PATCH",
        data: $("#nextofkin-formData").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'});
                unloadUserInfo();
                closeModal('profile-edit');
                $('.modal-backdrop').remove();
            }else{
               NioApp.Toast('<h4>'+response.title+'</h4>'+response.message, response.status,{position:'top-right'});
            }
            $(".next-confirm").prop("disabled",false);
            $(".next-spinner").hide();
        },
        error:function(response){
            console.log(response);
        }
    });
});



$(document).on("click",".change-photo",function(e){
    e.preventDefault();
    $("input[id='propic']").click();
});
$(document).on("click",".change-profile-photo",function(e){
    e.preventDefault();
    $("input[id='propic']").click();
});

$("input[id='propic']").change(function(e){
    e.preventDefault();
    var file_data = $('#propic').prop('files')[0];
    var form_data = new FormData();
    form_data.append('profile_picture', file_data);
    $.ajax({
        url: "/user/profile/photo/update",
        type: "POST",
        data: form_data,
        contentType: false,
        cache: false,
        processData: false,
        success: function (response) {
            console.log(response);
            NioApp.Toast(response.message, response.status,{position: 'top-right'});
            unloadUserInfo();

        },
        error:function(response){
            console.log(response);
        }
    });
});
