'use strict';
if($('#users_page').length) {
	$('#modalChangePassword').on('show.bs.modal', function (e) {
		let button = $(e.relatedTarget);
		let user = button.data('user');
		let modal = $(this);
		modal.find('.m-cp-span-user').text(user);
		modal.find('input#change_username').val(user);
		//modal.find('.modal-body input').val(recipient);
	});
	$('#modalDeleteUser').on('show.bs.modal', function (e) {
		let button = $(e.relatedTarget);
		let user = button.data('user');
		let modal = $(this);
		modal.find('.m-du-span-user').text(user);
		modal.find('input#del_username').val(user);
		//modal.find('.modal-body input').val(recipient);
	});
	$('#m_cp_form').submit((e) => {
		e.preventDefault();
		console.log($(this));
	});

	$('#m_au_form').submit((e) => {
		e.preventDefault();
		let form = $('#m_au_form');
		let formData = FormToObject(form);
		request({
		  	url: "/sven/api/auth",
		  	data: {
				"type": "login",
		  		"user": formData.username,
		  		"pass": formData.password
			}
	  	}, function(data) {
			console.log(data.status, data);
			document.location = "./";
		}, function(data) {
			console.log(data.status, data.responseJSON);
		});
	});
	var FormToObject = (elem) => {
		let data = elem.serializeArray();
		let output = {};
		for (var i = 0; i < data.length; i++) {
			output[data[i]['name']] = data[i]['value'];
		}
		return output;
	};
}
