$(".btn#add").click(function (){
	$('#full_name_err').html("");
	$('#login_name_err').html("");
	$('#pwd1_err').html("");
	$('#pwd2_err').html("");
	$('#email_err').html("");
	$('#phone_err').html("");
    $.ajax({
		url: "quan_tri_vien/them",
		type: "POST",
		data: $("#add_frm").serialize(),
		success: function (data, textStatus, jqXHR)
		{
			result = jQuery.parseJSON(data);
			if (result.success == "1")
			{   
				filter();
				$('#addAdmin').modal('hide');
			}
			else
			{
				for (var key in result.errors)
				{
					$('#'+key).html(result.errors[key]);
				}
			}
			
		},
               
	});
        
});

$('#editAdmin').on('show.bs.modal', function(e) {
	var id = $(e.relatedTarget).data('admin-id');
	$.ajax({
		url: "quan_tri_vien/id/" + id,
		type: "POST",
		success: function (data, textStatus, jqXHR)
		{
			result = jQuery.parseJSON(data);
			if (result.success=="1")
			{
				item = result.item;
				$("#edit_full_name").val(item['full_name']);
				$("#edit_login_name").val(item['login_name']);
				$("#edit_email").val(item['email']);
				$("#edit_phone").val(item['phone']);
			}else
			{
				console.log("Item not found !");
			}
		}
	});
	
	$(".btn#edit").click(function(){
		$.ajax({
			url: "quan_tri_vien/sua/" + id,
			type: "POST",
			data: $("#edit_frm").serialize(),
			success: function (data, textStatus, jqXHR)
			{
				result = jQuery.parseJSON(data)
				if (result.success=="1"){
					filter();
					$('#editAdmin').modal('hide');
				}else
				{
					console.log("Edit error !");
				}
			}
		});
	});
});

function updateAdmin(id){
	$.ajax({
		url: 'quan_tri_vien/cap_nhat',
		type: "POST",
		data: $("#editAdmin_"+id).serialize(),
		success: function (data, textStatus, jqXHR)
		{
			$result = jQuery.parseJSON(data);
			if ($result.success == "1")
			{   
				filter();
				$('#close_'+id).click();
			}
			else
			{	
				console.log("Update error");
			}
		},
		error: function (jqXHR, textStatus, errorThrown)
		{
			$(".ajaxloading").hide();
		}
	});
	return false;
}

$('#deleteAdmin').on('show.bs.modal', function(e) {
	var id = $(e.relatedTarget).data('admin-id');
	var us = $(e.relatedTarget).data('admin-us');
	//$(e.currentTarget).find('input[name="id"]').val(id);
	$("#us").html(us);
	$(".btn#delete").click(function(){
		$.ajax({
			url: "quan_tri_vien/xoa/" + id,
			type: "POST",
			success: function (data, textStatus, jqXHR)
			{
				result = jQuery.parseJSON(data)
				if (result.success=="1"){
					filter();
					$('#deleteAdmin').modal('hide');
				}else
				{
					console.log("Delete error !");
				}
			}
		});
	});
});

(function($){
    $.fn.setCursorToTextEnd = function() {
        $initialVal = this.val();
        this.val($initialVal + ' ');
        this.val($initialVal);
    };
})(jQuery);

function clearFormFields(id){
        $(id).find('input:text, input:hidden, input:password, input:file, select, textarea').val('');
        $(id).find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
		$(id).find('.help-block').html("");
}

$('body').on('hidden.bs.modal', '.modal', function () {
	clearFormFields('#add_frm');
	clearFormFields('#edit_frm');
	clearFormFields('#delete_frm');
});

function updateInfo()
{
	$('#email_err').html("");
	$('#phone_err').html("");
	$.ajax({
		url: "cap_nhat_thong_tin_ca_nhan",
		type: "POST",
		data: $("#frm1").serialize(),
		success: function (data, textStatus, jqXHR)
		{
			result = jQuery.parseJSON(data);
			if (result.success == "1")
			{   
				window.location.reload(true);
			}
			else
			{
				for (var key in result.errors)
				{
					//console.log(result.errors[key]);
					$('#'+key).html(result.errors[key]);
				}
			}
			
		},
               
	});
}

function changePassword()
{
	$('#passwd_old_err').html("");
	$('#passwd_new_err').html("");
	$('#confirm_passwd_new_err').html("");
	$.ajax({
		url: "doi_mat_khau",
		type: "POST",
		data: $("#frm2").serialize(),
		success: function (data, textStatus, jqXHR)
		{
			result = jQuery.parseJSON(data);
			if (result.success == "1")
			{   
				window.location.reload(true);
			}
			else
			{
				for (var key in result.errors)
				{
					//console.log(result.errors[key]);
					$('#'+key).html(result.errors[key]);
				}
			}
			
		},
               
	});
}