/**
 * Created by Cyan on 2/27/2016.
 */
$(document).ready(function() {
    loadGenericEvents();
    getMembers();
});

function loadGenericEvents() {
    // Select tab
    $('.nav li.active').removeClass('selected');
    $('#members-tab').addClass('selected');

    // Add new member
    $('.add-member-btn').click(clearModal);

    // Add member
    $('#add-member-form').unbind('submit');
    $('#add-member-form').submit(createMember);

    $('#add-member-close').click(function() {
        $('#add-member-modal .alert').hide();
    });

    $('.alert .glyphicon').click(function() {
        $(this).parent().hide();
    });
}

function loadEvents() {
    // Delete member
    $('.delete-member').unbind('click');
    $('.delete-member').click(deleteMember);
}

function clearModal() {
    $('input#memberEmail').val('');
    $('input#memberRole').val('');
    $('#add-member-close').click();
}

function getMembers() {
    var token = $("input[name='_csrf']").val();

    $.ajax({
        type: "GET",
        headers: {
            'X-CSRF-TOKEN': token,
            'Content-Type':'application/json'
        },
        url: "/members/getMembers",
        async: true,
        success: function(data){
            $('div.members-content').html(data);
            loadEvents();
        },

        error: function(e){
            showErrorMessage('Error in getting the members.');
        }
    });
}

function createMember(event) {
    event.preventDefault();
    event.stopPropagation();

    var token = $("input[name='_csrf']").val();
    var email = $('input#memberEmail').val();
    var role = $('input#memberRole').val();

    var request = {};
    request["email"] = email;
    request["role"] = role;

    $.ajax({
        type: "POST",
        headers: {
            'X-CSRF-TOKEN': token,
            'Content-Type':'application/json'
        },
        url: "/members/createMember",
        data: JSON.stringify(request),
        dataType: "json",
        timeout: 100000,
        success: function(data) {
            if (data['state'] === "OK") {
                showSuccessMessage(data['message']);
                getMembers();
            } else {
                showErrorMessage(data['message']);
            }
        },
        error: function(data) {
            showErrorMessage('Can not add the member.');
        }
    });

    $('#add-member-close').click();
}

function deleteMember(event) {
    var token = $("input[name='_csrf']").val();
    var email = $(this).siblings('input').val();
    var memberElement = $(this).closest('li.member');

    $.ajax({
        type: "POST",
        headers: {
            'X-CSRF-TOKEN': token,
            'Content-Type':'text/plain'
        },
        url: "/members/deleteMember",
        data: email,
        dataType: "json",
        timeout: 100000,
        success: function(data) {
            if (data['state'] === "OK") {
                removeMemberElement(memberElement);
                showSuccessMessage(data['message']);
            } else {
                showErrorMessage(data['message']);
            }
        },
        error: function(data) {
            showErrorMessage('Can not remove the member.');
        }
    });
}

function removeMemberElement(member) {
    $(member).hide();
    $(member).remove();
}