{"size": 73365, "ext": "js", "max_stars_repo_path": "maliapp/js/custom.js", "max_stars_repo_name": "jaymohmwangi/property-managment-software-full-source-code", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "maliapp/js/custom.js", "max_issues_repo_name": "jaymohmwangi/property-managment-software-full-source-code", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "maliapp/js/custom.js", "max_forks_repo_name": "jaymohmwangi/property-managment-software-full-source-code", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.5360250609, "max_line_length": 148, "alphanum_fraction": 0.5244053704}

/*
    auth ajax here created by james mwangi
*/


/*
*login -james mwangi
 */
var onResetPass = function(token) {

	$('#reset').text('');
	$('.alert').empty();
	$('#reset').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/auth/reset/',
		type: "POST",
		data: $('#form-reset').serialize(),
		dataType: "JSON",
		success: function (res) {

			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Reset link has been sent to your email.</div>');
				//$('#login').text('Success');

				setTimeout(function () {
					$('#reset').empty();
					$('#reset').text('Resend');
				},4000)


			}else{
				$('#reset').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#reset').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#reset').empty();
			$('#reset').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
};

/*
*login -james mwangi
 */
var onSignin = function(token) {

	$('#login').text('');
	$('.alert').empty();
	$('#login').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/auth/login/',
		type: "POST",
		data: $('#form-login').serialize(),
		dataType: "JSON",
		success: function (res) {

			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Welcome back </div>');
				//$('#login').text('Success');

				setTimeout(function () {
                    $(location).attr('href','/backend/auth/otp');
				},2000)


			}else{
				$('#login').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#login').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#login').empty();
			$('#login').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
};
//edit profile -by james mwangi
$('#form-edit-profile').submit(function(e){
	e.preventDefault();

	$('#profile-edit').text('');
	$('.alert').empty();
	$('#profile-edit').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/profile/update_profile/',
		type: "POST",
		data: $('#form-edit-profile').serialize(),
		dataType: "JSON",
		success: function (res) {
           console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Updated successfully.</div>');
				$('#profile-edit').text('Save changes');

				setTimeout(function () {
					location.reload();
				},2000)


			}else{
				$('#profile-edit').empty();
				$('.alert').append(' <div class="alert-error">Unable to complete this action,please try again.</div>');
				$('#profile-edit').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#profile-edit').empty();
			$('#profile-edit').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
})//edit profile -by james mwangi
$('#form-change-pass').submit(function(e){
	e.preventDefault();

	$('#change-pass').text('');
	$('.alert').empty();
	$('#change-pass').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/auth/change_password/',
		type: "POST",
		data: $('#form-change-pass').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">'+res.error+'</div>');
				$('#change-pass').text('Save changes');

				setTimeout(function () {
					$(location).attr('href','/backend/auth/');
				},2000)


			}else{
				$('#change-pass').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#change-pass').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#change-pass').empty();
			$('#change-pass').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
})
//change password -by james mwangi
$('#form-edit-password').submit(function(e){
	e.preventDefault();

	$('#profile-edit-password').text('');
	$('.alert').empty();
	$('#profile-edit-password').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/profile/change_password/',
		type: "POST",
		data: $('#form-edit-password').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Updated successfully.</div>');
				$('#profile-edit-password').text('Save changes');

				setTimeout(function () {
					location.reload();
				},5000)


			}else{
				$('#profile-edit-password').empty();
				$('.alert').append(' <div class="alert-error">Old password is incorrect.</div>');
				$('#profile-edit-password').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#profile-edit-password').empty();
			$('#profile-edit-password').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});

$('#form-otp').submit(function(e){
	e.preventDefault();

	$('#otp').text('');
	$('.alert').empty();
	$('#otp').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/auth/otp',
		type: "POST",
		data: $('#form-otp').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">'+res.error+'</div>');
				$('#otp').text('Done');
				$("html, body").animate({ scrollTop: 0 }, "slow");
				setTimeout(function () {
					$(location).attr('href','/backend/dashboard');
				},2000)


			}else{
				$('#otp').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#otp').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#otp').empty();
			$('#otp').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});

//add property - by james mwangi
$('#form-add-property').submit(function(e){
    e.preventDefault();

    $('#add-property').text('');
    $('.alert').empty();
    $('#add-property').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/property/add',
        type: "POST",
        data: $('#form-add-property').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert').append(' <div class="alert-success">Added successfully.</div>');
                $('#add-property').text('Save');
                $("html, body").animate({ scrollTop: 0 }, "slow");
                setTimeout(function () {
                    document.getElementById('form-add-property').reset();
                    $(location).attr('href','/backend/properties');
                },2000)


            }else{
                $('#add-property').empty();
                $('.alert').append(' <div class="alert-error">'+res.error+'</div>');
                $('#add-property').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#add-property').empty();
            $('#add-property').text('try again');
            $('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});
//edit property - by james mwangi
$('#form-edit-property').submit(function(e){
	e.preventDefault();

	$('#edit-property').text('');
	$('.alert').empty();
	$('#edit-property').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/property/edit',
		type: "POST",
		data: $('#form-edit-property').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Updated successfully.</div>');
				$('#edit-property').text('Save');

				$("html, body").animate({ scrollTop: 0 }, "slow");

			}else{
				$('#edit-property').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-property').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-property').empty();
			$('#edit-property').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//delete property - by james mwangi
$('.delete-property').click(function(e){
	e.preventDefault();


	var property_id=$(this).data("id");

	console.log(property_id);

	swal({
			title: "Are you sure ?",
			text:"You will not be able to recover this document.",
			type: "warning",
			showCancelButton: true,
			confirmButtonClass: "btn-danger",
			confirmButtonText: "Yes, delete it!",
			cancelButtonText: "No, cancel plx!",
			closeOnConfirm: false,
			closeOnCancel: false
		},
		function(isConfirm) {
			if (isConfirm) {
				$.ajax({
					url:baseURL+'backend/ajax/property/delete',
					type: "POST",
					data: {property_id:property_id},
					dataType: "JSON",
					success: function (res) {
						console.log(res);
						if(res.response==true){
							//success alert

							swal("Deleted!", "Record has been deleted successfully.", "success");


							setTimeout(function () {
								location.reload();
							},3000)

						}else {
							//error alert
							swal("Cancelled!", "Unable to finish this action::"+res.error, "error");
						}


					},
					error: function (jqXHR, textStatus, errorThrown) {
						//error alert

					}
				});


			} else {

				swal("Cancelled", "Error deleting your record ::"+errorThrown, "error");

			}
		});
});
//add unit - by james mwangi
$('#form-add-unit').submit(function(e){
	e.preventDefault();

	$('#add-unit').text('');
	$('.alert').empty();
	$('#add-unit').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/unit/add',
		type: "POST",
		data: $('#form-add-unit').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Added successfully.</div>');
				$('#add-unit').text('Save');
				$("html, body").animate({ scrollTop: 0 }, "slow");
				setTimeout(function () {
					document.getElementById('form-add-unit').reset();
					$(location).attr('href','/backend/units');
				},2000)


			}else{
				$('#add-unit').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#add-unit').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#add-unit').empty();
			$('#add-unit').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//edit unit - by james mwangi
$('#form-edit-unit').submit(function(e){
	e.preventDefault();

	$('#edit-unit').text('');
	$('.alert').empty();
	$('#edit-unit').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/unit/edit',
		type: "POST",
		data: $('#form-edit-unit').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Update successfully.</div>');
				$('#edit-unit').text('Save');

				$("html, body").animate({ scrollTop: 0 }, "slow");

			}else{
				$('#edit-unit').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-unit').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-unit').empty();
			$('#edit-unit').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//delete unit - by james mwangi
$('.delete-unit').click(function(e){
	e.preventDefault();


	var unit_id=$(this).data("id");

	console.log(unit_id);

	swal({
			title: "Are you sure ?",
			text:"You will not be able to recover this document.",
			type: "warning",
			showCancelButton: true,
			confirmButtonClass: "btn-danger",
			confirmButtonText: "Yes, delete it!",
			cancelButtonText: "No, cancel plx!",
			closeOnConfirm: false,
			closeOnCancel: false
		},
		function(isConfirm) {
			if (isConfirm) {
				$.ajax({
					url:baseURL+'backend/ajax/unit/delete',
					type: "POST",
					data: {unit_id:unit_id},
					dataType: "JSON",
					success: function (res) {
						console.log(res);
						if(res.response==true){
							//success alert

							swal("Deleted!", "Record has been deleted successfully.", "success");


							setTimeout(function () {
								location.reload();
							},3000)

						}else {
							//error alert
							swal("Cancelled!", "Unable to finish this action::"+res.error, "error");
						}


					},
					error: function (jqXHR, textStatus, errorThrown) {
						//error alert

					}
				});


			} else {

				swal("Cancelled", "Error deleting your record ::"+errorThrown, "error");

			}
		});
});
//add tenant - by james mwangi
$('#form-add-tenant').submit(function(e){
	e.preventDefault();

	$('#add-tenant').text('');
	$('.alert').empty();
	$('#add-tenant').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/tenants/add',
		type: "POST",
		data: $('#form-add-tenant').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$("html, body").animate({ scrollTop: 0 }, "slow");

				$('.alert').append(' <div class="alert-success">Added successfully.</div>');
				$('#add-tenant').text('Save');

				setTimeout(function () {
					document.getElementById('form-add-tenant').reset();
					$(location).attr('href','/backend/tenants');
				},2000)


			}else{
				$('#add-tenant').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#add-tenant').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#add-tenant').empty();
			$('#add-tenant').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});

//edit tenant - by james mwangi
$('#form-edit-tenant').submit(function(e){
	e.preventDefault();

	$('#edit-tenant').text('');
	$('.alert').empty();
	$('#edit-tenant').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/tenants/edit',
		type: "POST",
		data: $('#form-edit-tenant').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Update successfully.</div>');
				$('#edit-tenant').text('Save');

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#edit-tenant').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-tenant').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-tenant').empty();
			$('#edit-tenant').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//delete tenant - by james mwangi
$('.delete-tenant').click(function(e){
	e.preventDefault();


	var tenant_id=$(this).data("id");

	console.log(tenant_id);

	swal({
			title: "Are you sure ?",
			text:"You will not be able to recover this document.",
			type: "warning",
			showCancelButton: true,
			confirmButtonClass: "btn-danger",
			confirmButtonText: "Yes, delete it!",
			cancelButtonText: "No, cancel plx!",
			closeOnConfirm: false,
			closeOnCancel: false
		},
		function(isConfirm) {
			if (isConfirm) {
				$.ajax({
					url:baseURL+'backend/ajax/tenants/delete',
					type: "POST",
					data: {tenant_id:tenant_id},
					dataType: "JSON",
					success: function (res) {
						console.log(res);
						if(res.response==true){
							//success alert

							swal("Deleted!", "Record has been deleted successfully.", "success");


							setTimeout(function () {
								location.reload();
							},3000)

						}else {
							//error alert
							swal("Cancelled!", "Unable to finish this action::"+res.error, "error");
						}


					},
					error: function (jqXHR, textStatus, errorThrown) {
						//error alert

					}
				});


			} else {

				swal("Cancelled", "Error deleting your record ::"+errorThrown, "error");

			}
		});
});
//add lease - by james mwangi
$('#form-add-lease').submit(function(e){
	e.preventDefault();

	$('#add-lease').text('');
	$('.alert').empty();
	$('#add-lease').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/lease/add',
		type: "POST",
		data: $('#form-add-lease').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Added successfully.</div>');
				$('#add-lease').text('Save');

				$("html, body").animate({ scrollTop: 0 }, "slow");

				setTimeout(function () {
					document.getElementById('form-add-lease').reset();
					$(location).attr('href','/backend/leases');
				},2000);

			}else{
				$('#add-lease').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#add-lease').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#add-lease').empty();
			$('#add-lease').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});

//edit lease - by james mwangi
$('#form-edit-lease').submit(function(e){
	e.preventDefault();

	$('#edit-lease').text('');
	$('.alert').empty();
	$('#edit-lease').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/lease/edit',
		type: "POST",
		data: $('#form-edit-lease').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Update successfully.</div>');
				$('#edit-lease').text('Save');

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#edit-lease').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-lease').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-lease').empty();
			$('#edit-lease').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//delete lease - by james mwangi
$('.delete-lease').click(function(e){
	e.preventDefault();


	var lease_id=$(this).data("id");

	console.log(lease_id);

	swal({
			title: "Are you sure ?",
			text:"You will not be able to recover this document.",
			type: "warning",
			showCancelButton: true,
			confirmButtonClass: "btn-danger",
			confirmButtonText: "Yes, delete it!",
			cancelButtonText: "No, cancel plx!",
			closeOnConfirm: false,
			closeOnCancel: false
		},
		function(isConfirm) {
			if (isConfirm) {
				$.ajax({
					url:baseURL+'backend/ajax/lease/delete',
					type: "POST",
					data: {lease_id:lease_id},
					dataType: "JSON",
					success: function (res) {
						console.log(res);
						if(res.response==true){
							//success alert

							swal("Deleted!", "Record has been deleted successfully.", "success");


							setTimeout(function () {
								location.reload();
							},3000)

						}else {
							//error alert
							swal("Cancelled!", "Unable to finish this action::"+res.error, "error");
						}


					},
					error: function (jqXHR, textStatus, errorThrown) {
						//error alert

					}
				});


			} else {

				swal("Cancelled", "Error deleting your record ::"+errorThrown, "error");

			}
		});
});
//edit settings - by james mwangi
function update_setting(id){
    event.preventDefault()
	$('#edit-settings-'+id).text('');
	$('#edit-settings-'+id).append('<div class="circle-sm"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/settings/edit_bills',
		type: "POST",
		data: $('#form-edit-settings-'+id).serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$.niftyNoty({
					type: 'success',
					container : 'floating',
					message : res.error,
					closeBtn : true,
					focus: true,
					timer: 10000

				});
				$('#edit-settings-'+id).text('Save changes');

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#edit-settings-'+id).empty();
				$('#edit-settings-'+id).text('try again');
				$.niftyNoty({
					type: 'danger',
					container : 'floating',
					message : res.error,
					closeBtn : true,
					focus: true,
					timer: 10000

				});
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-settings-'+id).empty();
			$('#edit-settings-'+id).text('try again');
			$.niftyNoty({
				type: 'danger',
				container : 'floating',
				message : "Error:{"+errorThrown+"}",
				closeBtn : true,
				focus: true,
				timer: 10000

			});

		}
	});
}

//add settings - by james mwangi
$('#form-add-settings').submit(function(e){
	e.preventDefault();

	$('#add-settings').text('');
	$('#add-settings').append('<div class="circle-sm"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/settings/add_bills',
		type: "POST",
		data: $('#form-add-settings').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$.niftyNoty({
					type: 'success',
					container : 'floating',
					message : res.error,
					closeBtn : true,
					focus: true,
					timer: 10000

				});
				$('#add-settings').text('Save changes');

				$("html, body").animate({ scrollTop: 0 }, "slow");
				location.reload();


			}else{
				$('#add-settings').empty();
				$('#add-settings').text('try again');
				$.niftyNoty({
					type: 'danger',
					container : 'floating',
					message : res.error,
					closeBtn : true,
					focus: true,
					timer: 10000

				});
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#add-settings').empty();
			$('#add-settings').text('try again');
			$.niftyNoty({
				type: 'danger',
				container : 'floating',
				message : "Error:{"+errorThrown+"}",
				closeBtn : true,
				focus: true,
				timer: 10000

			});

		}
	});
});
//edit sms api - by james mwangi
$('#form-edit-smtp-email-client').submit(function(e){
	e.preventDefault();

	$('#edit-smtp-email-client').text('');
	$('.alert').empty();
	$('#edit-smtp-email-client').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/settings/edit_smt_email_client',
		type: "POST",
		data: $('#form-edit-smtp-email-client').serialize(),
		dataType: "JSON",
		success: function (res) {
			//console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Update successfully.</div>');
				$('#edit-smtp-email-client').text('Save changes');

			}else{
				$('#edit-smtp-email-client').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-smtp-email-client').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-smtp-email-client').empty();
			$('#edit-smtp-email-client').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//edit sms api - by james mwangi
$('#form-edit-sms-api').submit(function(e){
	e.preventDefault();

	$('#edit-sms-api').text('');
	$('.alert').empty();
	$('#edit-sms-api').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/settings/edit_sms_api',
		type: "POST",
		data: $('#form-edit-sms-api').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Update successfully.</div>');
				$('#edit-sms-api').text('Save changes');

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#edit-sms-api').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-sms-api').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-sms-api').empty();
			$('#edit-sms-api').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});

//edit system settings - by james mwangi
$('#form-edit-sys-setting').submit(function(e){
	e.preventDefault();
     console.log( $('#form-edit-sys-setting').serialize())
	$('#edit-sys').text('');
	$('.alert').empty();
	$('#edit-sys').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/settings/sys_settings',
		type: "POST",
		data: $('#form-edit-sys-setting').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Update successfully.</div>');
				$('#edit-sys').text('Save changes');

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#edit-sys').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-sys').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-sys').empty();
			$('#edit-sys').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//edit email template - by james mwangi
$('#form-edit-email-template').submit(function(e){
	e.preventDefault();

	$('#edit-email-template').text('');
	$('.alert').empty();
	$('#edit-email-template').append('<div class="circle"></div>');

    console.log($('.rent-statement-template').summernote('code'));
	console.log($('.reset-pass-template').summernote('code'));

	$.ajax({
		url:baseURL+'backend/ajax/settings/edit_email_templates',
		type: "POST",
		data: {reset_pass_template:$('.reset-pass-template').summernote('code'),rent_statement_template:$('.rent-statement-template').summernote('code')},
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Update successfully.</div>');
				$('#edit-email-template').text('Save changes');

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#edit-email-template').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-email-template').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-email-template').empty();
			$('#edit-email-template').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});

//add payment - by james mwangi
$('#form-add-payment').submit(function(e){
	e.preventDefault();

	$('#add-payment').text('');
	$('.alert').empty();
	$('#add-payment').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/payments/add',
		type: "POST",
		data: $('#form-add-payment').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Added successfully.</div>');
				$('#add-payment').text('Save');

				setTimeout(function () {
					document.getElementById('form-add-payment').reset();
					$(location).attr('href','/backend/payments');
				},2000);

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#add-payment').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#add-payment').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#add-payment').empty();
			$('#add-payment').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//edit payment - by james mwangi
$('#form-edit-payment').submit(function(e){
	e.preventDefault();

	$('#edit-payment').text('');
	$('.alert').empty();
	$('#edit-payment').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/payments/edit',
		type: "POST",
		data: $('#form-edit-payment').serialize(),
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){

				$('.alert').append(' <div class="alert-success">Added successfully.</div>');
				$('#edit-payment').text('Save');

				$("html, body").animate({ scrollTop: 0 }, "slow");


			}else{
				$('#edit-payment').empty();
				$('.alert').append(' <div class="alert-error">'+res.error+'</div>');
				$('#edit-payment').text('try again');
			}


		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#edit-payment').empty();
			$('#edit-payment').text('try again');
			$('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

		}
	});
});
//delete payment - by james mwangi
	$('.delete-payment').click(function(e){
		e.preventDefault();


		var payment_id=$(this).data("id");

		console.log(payment_id);

		swal({
				title: "Are you sure ?",
				text:"You will not be able to recover this document.",
				type: "warning",
				showCancelButton: true,
				confirmButtonClass: "btn-danger",
				confirmButtonText: "Yes, delete it!",
				cancelButtonText: "No, cancel plx!",
				closeOnConfirm: false,
				closeOnCancel: false
			},
			function(isConfirm) {
				if (isConfirm) {
					$.ajax({
						url:baseURL+'backend/ajax/payments/delete',
						type: "POST",
						data: {payment_id:payment_id},
						dataType: "JSON",
						success: function (res) {
							console.log(res);
							if(res.response==true){
								//success alert

								swal("Deleted!", "Record has been deleted successfully.", "success");


								setTimeout(function () {
									location.reload();
								},3000)

							}else {
								//error alert
								swal("Cancelled!", "Unable to finish this action::"+res.error, "error");
							}


						},
						error: function (jqXHR, textStatus, errorThrown) {
							//error alert

						}
					});


				} else {

					swal("Cancelled", "Error deleting your record ::"+errorThrown, "error");

				}
			});
	});

//allocate unknown payment - by james mwangi
$('#form-allocate-payment').submit(function(e){
    e.preventDefault();

    $('#allocate-payment').text('');
    $('.alert').empty();
    $('#allocate-payment').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/payments/allocate',
        type: "POST",
        data: $('#form-allocate-payment').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert').append(' <div class="alert-success">Allocate successfully.</div>');
                $('#allocate-payment').text('Save');

                setTimeout(function () {
                    document.getElementById('form-allocate-payment').reset();
                    $(location).attr('href','/backend/payments/unknown');
                },2000);

                $("html, body").animate({ scrollTop: 0 }, "slow");


            }else{
                $('#allocate-payment').empty();
                $('.alert').append(' <div class="alert-error">'+res.error+'</div>');
                $('#allocate-payment').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#allocate-payment').empty();
            $('#allocate-payment').text('try again');
            $('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});


//get account balance - by james mwangi
	$('.get-account-balance').click(function(e){
		e.preventDefault();


		$.ajax({
					url:baseURL+'backend/ajax/payments/get_account_balance',
					type: "POST",
					data: {account_number:1},
					dataType: "JSON",
					success: function (res) {
						console.log(res);
						if(res.response==true){
							//success alert
							$.niftyNoty({
								type: 'success',
								container : 'floating',
								message : 'Current balance '+res.bank_balance.balances[0].amount+'<br> Available balance '+res.bank_balance.balances[0].amount,
								closeBtn : true,
								focus: true,
								timer: 10000

							});

						}else {
							$.niftyNoty({
								type: 'danger',
								container : 'floating',
								message : 'Unable to complete your request.',
								closeBtn : true,
								focus: true,
								timer: 10000

							});
						}
					},
					error: function (jqXHR, textStatus, errorThrown) {
						//error alert
						$.niftyNoty({
							type: 'danger',
							container : 'floating',
							message : errorThrown,
							closeBtn : true,
							focus: true,
							timer: 10000

						});
					}
				});



});
//get account details - by james mwangi
$('.get-account-details').click(function(e){
	e.preventDefault();


	$.ajax({
		url:baseURL+'backend/ajax/payments/get_account_details',
		type: "POST",
		data: {account_number:1},
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){
				//success alert
				$.niftyNoty({
					type: 'success',
					container : 'floating',
					message : res.info.account.branchCode,
					closeBtn : true,
					focus: true,
					timer: 10000

				});

			}else {
				$.niftyNoty({
					type: 'danger',
					container : 'floating',
					message : 'Unable to complete your request.',
					closeBtn : true,
					focus: true,
					timer: 10000

				});
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			//error alert
			$.niftyNoty({
				type: 'danger',
				container : 'floating',
				message : errorThrown,
				closeBtn : true,
				focus: true,
				timer: 10000

			});
		}
	});



});
//get payment done - by james mwangi
$('.get-account-payment').click(function(e){
	e.preventDefault();

	$.ajax({
		url:baseURL+'backend/ajax/payments/get_account_payment_done',
		type: "POST",
		data: {bill_reference:1},
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){
				//success alert
				$.niftyNoty({
					type: 'success',
					container : 'floating',
					message : res.payment.orderRef,
					closeBtn : true,
					focus: true,
					timer: 10000

				});

			}else {
				$.niftyNoty({
					type: 'danger',
					container : 'floating',
					message : 'Unable to complete your request.',
					closeBtn : true,
					focus: true,
					timer: 10000

				});
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			//error alert
			$.niftyNoty({
				type: 'danger',
				container : 'floating',
				message : errorThrown,
				closeBtn : true,
				focus: true,
				timer: 10000

			});
		}
	});



});
//safaricom test - by james mwangi
$('.register-saf-callbacks').click(function(e){
	e.preventDefault();

	$.ajax({
		url:baseURL+'backend/ajax/payments/validate_saf_payment',
		type: "POST",
		data: {id:1},
		dataType: "JSON",
		success: function (res) {
			console.log(res);
			if(res.response==true){
				//success alert
				$.niftyNoty({
					type: 'success',
					container : 'floating',
					message : res.success.ResponseDescription,
					closeBtn : true,
					focus: true,
					timer: 10000

				});

			}else {
				$.niftyNoty({
					type: 'danger',
					container : 'floating',
					message : 'Unable to complete your request.',
					closeBtn : true,
					focus: true,
					timer: 10000

				});
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			//error alert
			$.niftyNoty({
				type: 'danger',
				container : 'floating',
				message : errorThrown,
				closeBtn : true,
				focus: true,
				timer: 10000

			});
		}
	});



});

/*
*Add block
 */
$('#add-block').on('click', function(){
	bootbox.dialog({
		title: '<div class="text-center">Add new block</div>',
		message:'<div class="row"> ' +
		'<div class="col-md-12"> ' +
		'<form id="form-add-block" class="form-horizontal"> ' +
		'<div class="form-group"> ' +
		'<label class="col-md-4 control-label" for="name">Block name</label> ' +
		'<div class="col-md-4"> ' +
		'<input id="name" name="block_name"  type="text" placeholder="block name" class="form-control input-md"> ' +
		'</div> ' +
		'<div class="form-group"> ' +
		'</div>' +
		'</form> ' +
		'</div> </div>',
		buttons: {
			success: {
				label: "Save",
				className: " btn-lg btn-primary",
				callback: function() {
					var name = $('#name').val();
					$.ajax({
						url:baseURL+'backend/ajax/blocks/add',
						type: "POST",
						data: {block_name:name},
						dataType: "JSON",
						success: function (res) {
							console.log(res);
							if(res.response==true){
								//success alert
								$.niftyNoty({
									type: 'success',
									container : 'floating',
									message : res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
								setTimeout(function () {
									location.reload();
								},2000)


							}else {
								$.niftyNoty({
									type: 'danger',
									container : 'floating',
									message :res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
							}
						},
						error: function (jqXHR, textStatus, errorThrown) {
							//error alert
							$.niftyNoty({
								type: 'danger',
								container : 'floating',
								message : errorThrown,
								closeBtn : true,
								focus: true,
								timer: 10000

							});
						}
					});

				}
			}
		}
	});

});

/*
*Add floor
 */
$('#add-floor').on('click', function(){
	bootbox.dialog({
		title: '<div class="text-center">Add new floor</div>',
		message:'<div class="row"> ' +
		'<div class="col-md-12"> ' +
		'<form id="form-add-block" class="form-horizontal"> ' +
		'<div class="form-group"> ' +
		'<label class="col-md-4 control-label" for="name">Floor</label> ' +
		'<div class="col-md-4"> ' +
		'<input id="name" name="floor_name"  type="text" placeholder="floor" class="form-control input-md"> ' +
		'</div> ' +
		'<div class="form-group"> '+
		'</div>' +
		'</form> ' +
		'</div> </div>',
		buttons: {
			success: {
				label: "Save",
				className: " btn-lg btn-primary",
				callback: function() {
					var name = $('#name').val();
					$.ajax({
						url:baseURL+'backend/ajax/floors/add',
						type: "POST",
						data: {floor_name:name},
						dataType: "JSON",
						success: function (res) {
							console.log(res);
							if(res.response==true){
								//success alert
								$.niftyNoty({
									type: 'success',
									container : 'floating',
									message : res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
								setTimeout(function () {
									location.reload();
								},2000)


							}else {
								$.niftyNoty({
									type: 'danger',
									container : 'floating',
									message :res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
							}
						},
						error: function (jqXHR, textStatus, errorThrown) {
							//error alert
							$.niftyNoty({
								type: 'danger',
								container : 'floating',
								message : errorThrown,
								closeBtn : true,
								focus: true,
								timer: 10000

							});
						}
					});

				}
			}
		}
	});

});

/*
*Add bedroom
 */
$('#add-bedroom').on('click', function(){
	bootbox.dialog({
		title: '<div class="text-center">Add new bedroom</div>',
		message:'<div class="row"> ' +
		'<div class="col-md-12"> ' +
		'<form id="form-add-block" class="form-horizontal"> ' +
		'<div class="form-group"> ' +
		'<label class="col-md-4 control-label" for="name">Bedroom</label> ' +
		'<div class="col-md-4"> ' +
		'<input id="name" name="bed_room"  type="text" placeholder="Bed room" class="form-control input-md"> ' +
		'</div> ' +
		'<div class="form-group"> '+
		'</div>' +
		'</form> ' +
		'</div> </div>',
		buttons: {
			success: {
				label: "Save",
				className: " btn-lg btn-primary",
				callback: function() {
					var name = $('#name').val();
					$.ajax({
						url:baseURL+'backend/ajax/bedrooms/add',
						type: "POST",
						data: {bed_room:name},
						dataType: "JSON",
						success: function (res) {
							console.log(res);
							if(res.response==true){
								//success alert
								$.niftyNoty({
									type: 'success',
									container : 'floating',
									message : res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
								setTimeout(function () {
									location.reload();
								},2000)


							}else {
								$.niftyNoty({
									type: 'danger',
									container : 'floating',
									message :res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
							}
						},
						error: function (jqXHR, textStatus, errorThrown) {
							//error alert
							$.niftyNoty({
								type: 'danger',
								container : 'floating',
								message : errorThrown,
								closeBtn : true,
								focus: true,
								timer: 10000

							});
						}
					});

				}
			}
		}
	});

});

/*
*Add bedroom
 */
$('#add-bill').on('click', function(){
	bootbox.dialog({
		title: '<div class="text-center">New bill</div>',
		message:'<div class="row"> ' +
		'<div class="col-md-12"> ' +
		'<form id="form-add-bill" class="form-horizontal"> ' +
		'<div class="form-group"> ' +
		'<label class="col-md-4 control-label" for="name">Bill name</label> ' +
		'<div class="col-md-4"> ' +
		'<input id="name" name="name"  type="text" placeholder="e.g. security" class="form-control input-md"> ' +
		'</div> ' +
		'</div> ' +
		'<div class="form-group">'+
		'<label class="col-md-4 control-label" for="name">Bill amount</label> ' +
		'<div class="col-md-4"> ' +
		'<input id="amount" name="amount"  type="text" placeholder="0.00" class="form-control input-md"> ' +
		'</div> ' +
		'</div>' +
		'</form> ' +
		'</div> </div>',
		buttons: {
			success: {
				label: "Save",
				className: " btn-lg btn-primary",
				callback: function() {
					var name = $('#name').val();
					var amount = $('#amount').val();
					$.ajax({
						url:baseURL+'backend/ajax/settings/add_bills',
						type: "POST",
						data: {name:name,amount:amount},
						dataType: "JSON",
						success: function (res) {
							console.log(res);
							if(res.response==true){
								//success alert
								$.niftyNoty({
									type: 'success',
									container : 'floating',
									message : res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
								setTimeout(function () {
									location.reload();
								},2000)


							}else {
								$.niftyNoty({
									type: 'danger',
									container : 'floating',
									message :res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
							}
						},
						error: function (jqXHR, textStatus, errorThrown) {
							//error alert
							$.niftyNoty({
								type: 'danger',
								container : 'floating',
								message : errorThrown,
								closeBtn : true,
								focus: true,
								timer: 10000

							});
						}
					});

				}
			}
		}
	});

});
/*
*Deactivate tenant
 */
$('.deactivate-tenant').on('click', function(){
	var tenant_id=$(this).data("tenant_id");
	//console.log(tenant_id);
	bootbox.dialog({
		title: '<div class="text-center">You are about to deactivate this tenant</div>',
		message:'<div class="row"> ' +
		'<div class="col-md-12"> ' +
		'<form id="form-add-block" class="form-horizontal"> ' +
		'<div class="form-group"> ' +
		'<label class="col-md-4 control-label" for="comment">Reason for deactivating:</label> ' +
		'<div class="col-md-6"> ' +
		'<textarea id="comment" name="comment" rows="5"  placeholder="Reason" class="form-control input-md"></textarea> ' +
		'</div> ' +
		'<div class="form-group"> '+
		'</div>' +
		'</form> ' +
		'</div> </div>',
		buttons: {
			success: {
				label: "Save",
				className: "btn-danger btn-lg",
				callback: function() {
					var comment = $('#comment').val();
					$.ajax({
						url:baseURL+'backend/ajax/tenants/deactivate',
						type: "POST",
						data: {tenant_id:tenant_id,comment:comment},
						dataType: "JSON",
						success: function (res) {
							console.log(res);
							if(res.response==true){
								//success alert
								$.niftyNoty({
									type: 'success',
									container : 'floating',
									message : res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
								setTimeout(function () {
									location.reload();
								},2000)


							}else {
								$.niftyNoty({
									type: 'danger',
									container : 'floating',
									message :res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
							}
						},
						error: function (jqXHR, textStatus, errorThrown) {
							//error alert
							$.niftyNoty({
								type: 'danger',
								container : 'floating',
								message : errorThrown,
								closeBtn : true,
								focus: true,
								timer: 10000

							});
						}
					});

				}
			}
		}
	});

});

//terminate-lease - by james mwangi
$('.terminate-lease').click(function(e){
	e.preventDefault();


	var lease_id=$(this).data("lease_id");
	var unit_id=$(this).data("unit_id");
	console.log(lease_id);

	swal({
			title: "",
			text:"You are about to terminate this lease.",
			type: "warning",
			showCancelButton: true,
			confirmButtonClass: "btn-danger",
			confirmButtonText: "Yes, terminate it!",
			cancelButtonText: "No, cancel plx!",
			closeOnConfirm: false,
			closeOnCancel: false
		},
		function(isConfirm) {
			if (isConfirm) {
				$.ajax({
					url:baseURL+'backend/ajax/lease/terminate',
					type: "POST",
					data: {unit_id:unit_id,lease_id:lease_id},
					dataType: "JSON",
					success: function (res) {
						console.log(res);
						if(res.response==true){
							//success alert

							swal("Terminated!", "Lease successfully terminated.", "success");


							setTimeout(function () {
								location.reload();
							},3000)

						}else {
							//error alert
							swal("Cancelled!", "Unable to finish this action::"+res.error, "error");
						}


					},
					error: function (jqXHR, textStatus, errorThrown) {
						//error alert
						swal("Cancelled", "Error terminating lease ::"+errorThrown, "error");
					}
				});


			} else {

				swal("Cancelled", "You selected cancel.", "error");

			}
		});
});

//get-account-statement- by james mwangi
$('#form-get-property-report').submit(function(e) {
	e.preventDefault();
	var property_id=$('#property-chosen-select').val();
	console.log(property_id);
	getPropertyDetails(property_id);
});
//get-property details- by james mwangi
function getPropertyDetails(id) {
	var property_id=id;
	$('#property-chosen-select').val(property_id).trigger('chosen:updated');
   // console.log(property_id);
	$('#property-details').slideUp();
	$('.get-property-details').text('');
	$('.get-property-details').append('<div class="circle"></div>');

	$.ajax({
		url:baseURL+'backend/ajax/reports/get_property_summary',
		type: "POST",
		data: {property_id:property_id},
		dataType: "JSON",
		success: function (res) {
		console.log(res);
			if(res.response==true) {

				$('.get-property-details').empty();
				$('.get-property-details').text('Generate');

				$('#property-details').slideDown();
				//set the all units
				$('.all-units').text(res.all_units);
				//set the all units
				$('.occupied').text(res.occupied_units);
				//set the vacant units
				$('.vacant').text(res.vacant_units);
				//set the total rent
				$('.total_rent_o').text(default_currency+res.total_rent_expected_o);
				//set the total paid
				$('.total_paid_o').text(default_currency+res.total_rent_paid_o);
				//set the total balance
				$('.total_balance_o').text(default_currency+res.total_rent_balance_o);
				//set the all leases
				$('.all_leases').text(res.all_leases);
				//set the all active
				$('.active_leases').text(res.active_leases);
				//set the all expired
				$('.expired_leases').text(res.expired_leases);
				//set the all tenants
				$('.all_tenants').text(res.all_tenants);
				//set the active tenants
				$('.active_tenants').text(res.active_tenants);
				//set the inactive tenants
				$('.inactive_tenants').text(res.inactive_tenants);

			}else{
				$('#property-details').slideUp();
                $('.get-property-details').empty();
                $('.get-property-details').text('Generate');
                $.niftyNoty({
                    type: 'danger',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 10000

                });
			}
			},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#property-details').slideUp();
			$('.get-property-details').empty();
			$('.get-property-details').text('try again');
			$.niftyNoty({
				type: 'danger',
				container : 'floating',
				message : errorThrown,
				closeBtn : true,
				focus: true,
				timer: 10000

			});

		}
	});
}

/*
*make refund
 */
$('#make-refund').on('click', function(){
	var lease_id=$(this).data("lease_id");
	bootbox.dialog({
		title: '<div class="text-center">Make refund</div>',
		message:'<div class="row"> ' +
		'<div class="col-md-12"> ' +
		'<form id="form-make-refund" class="form-horizontal"> ' +
		'<div class="form-group"> ' +
		'<label class="col-md-4 control-label" for="name">Amount</label> ' +
		'<div class="col-md-4"> ' +
		'<input id="amount" name="amount"  type="text" disabled placeholder="0.00" value="'+$(this).data("refund")+'" class="form-control input-md"> ' +
		'</div> ' +
		'</div> ' +
		'<div class="form-group"> ' +
		'<label class="col-md-4 control-label" for="name">Note</label> ' +
		'<div class="col-md-4"> ' +
		'<textarea id="note" name="note" placeholder="comment.." class="form-control input-md"></textarea> ' +
		'</div> ' +
		'</div> ' +
		'</form> ' +'</div> ' +
		'</div> </div>',
		buttons: {
			success: {
				label: "Save",
				className: "btn-sm btn-primary",
				callback: function() {
					var amount = $('#amount').val();
					var note = $('#note').val();
					$.ajax({
						url:baseURL+'backend/ajax/payments/refund',
						type: "POST",
						data: {amount:amount,note:note,lease_id:lease_id},
						dataType: "JSON",
						success: function (res) {
							console.log(res);
							if(res.response==true){
								//success alert
								$.niftyNoty({
									type: 'success',
									container : 'floating',
									message : res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
								setTimeout(function () {
									location.reload();
								},2000)


							}else {
								$.niftyNoty({
									type: 'danger',
									container : 'floating',
									message :res.error,
									closeBtn : true,
									focus: true,
									timer: 10000

								});
							}
						},
						error: function (jqXHR, textStatus, errorThrown) {
							//error alert
							$.niftyNoty({
								type: 'danger',
								container : 'floating',
								message : errorThrown,
								closeBtn : true,
								focus: true,
								timer: 10000

							});
						}
					});

				}
			}
		}
	});

});

//add owner - by james mwangi
$('#form-add-owner').submit(function(e){
    e.preventDefault();

    $('#add-owner').text('');
    $('.alert').empty();
    $('#add-owner').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/owner/add',
        type: "POST",
        data: $('#form-add-owner').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert').append(' <div class="alert-success">'+res.error+'.</div>');
                $('#add-owner').text('Save');

                $("html, body").animate({ scrollTop: 0 }, "slow");
                setTimeout(function () {
                    $(location).attr('href','/backend/owners/');
                },3000)

            }else{
                $('#add-owner').empty();
                $('.alert').append(' <div class="alert-error">'+res.error+'</div>');
                $('#add-owner').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#add-owner').empty();
            $('#add-owner').text('try again');
            $('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});

//edit owner - by james mwangi
$('#form-edit-owner').submit(function(e){
    e.preventDefault();

    $('#edit-owner').text('');
    $('.alert').empty();
    $('#edit-owner').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/owner/edit',
        type: "POST",
        data: $('#form-edit-owner').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert').append(' <div class="alert-success">'+res.error+'.</div>');
                $('#edit-owner').text('Save');

                $("html, body").animate({ scrollTop: 0 }, "slow");
                setTimeout(function () {
                    location.reload();
                },3000)

            }else{
                $('#edit-owner').empty();
                $('.alert').append(' <div class="alert-error">'+res.error+'</div>');
                $('#edit-owner').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#edit-owner').empty();
            $('#edit-owner').text('try again');
            $('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});

//add users - by james mwangi
$('#form-add-users').submit(function(e){
    e.preventDefault();

    $('#add-users').text('');
    $('.alert').empty();
    $('#add-users').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/users/add',
        type: "POST",
        data: $('#form-add-users').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert').append(' <div class="alert-success">'+res.error+'.</div>');
                $('#add-users').text('Save');

                $("html, body").animate({ scrollTop: 0 }, "slow");
                setTimeout(function () {
                    $(location).attr('href','/backend/users/');
                },3000)

            }else{
                $('#add-users').empty();
                $('.alert').append(' <div class="alert-error">'+res.error+'</div>');
                $('#add-users').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#add-users').empty();
            $('#add-users').text('try again');
            $('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});

//edit users - by james mwangi
$('#form-edit-users').submit(function(e){
    e.preventDefault();

    $('#edit-users').text('');
    $('.alert').empty();
    $('#edit-users').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/users/edit',
        type: "POST",
        data: $('#form-edit-users').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert').append(' <div class="alert-success">'+res.error+'.</div>');
                $('#edit-users').text('Save');

                $("html, body").animate({ scrollTop: 0 }, "slow");
                setTimeout(function () {
                    location.reload();
                },3000)

            }else{
                $('#edit-users').empty();
                $('.alert').append(' <div class="alert-error">'+res.error+'</div>');
                $('#edit-users').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#edit-users').empty();
            $('#edit-users').text('try again');
            $('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});

//edit users - by james mwangi
$('#form-add-per').submit(function(e){
    e.preventDefault();

    $('#add-per').text('');
    $('.alert').empty();
    $('#add-per').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/users/add_perm',
        type: "POST",
        data: $('#form-add-per').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert').append(' <div class="alert-success">'+res.error+'.</div>');
                $('#add-per').text('Save');

                $("html, body").animate({ scrollTop: 0 }, "slow");
                setTimeout(function () {
                    $(location).attr('href','/backend/roles/');
                },3000)

            }else{
                $('#add-per').empty();
                $('.alert').append(' <div class="alert-error">'+res.error+'</div>');
                $('#add-per').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#add-per').empty();
            $('#add-per').text('try again');
            $('.alert').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});
function update_permission(id) {
    event.preventDefault()
    $('#edit-roles-'+id).text('');
    $('#edit-roles-'+id).append('<div class="circle-sm"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/users/edit_perm',
        type: "POST",
        data: $('#form-edit-roles-'+id).serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $.niftyNoty({
                    type: 'success',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 10000

                });
                $('#edit-roles-'+id).text('Save changes');

                $("html, body").animate({ scrollTop: 0 }, "slow");


            }else{
                $('#edit-roles-'+id).empty();
                $('#edit-roles-'+id).text('try again');
                $.niftyNoty({
                    type: 'danger',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 10000

                });
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#edit-roles-'+id).empty();
            $('#edit-roles-'+id).text('try again');
            $.niftyNoty({
                type: 'danger',
                container : 'floating',
                message : "Error:{"+errorThrown+"}",
                closeBtn : true,
                focus: true,
                timer: 10000

            });

        }
    });


}

//mpesa pay annully- by james mwangi
$('#form-pay-mpesa-annually').submit(function(e){
    e.preventDefault();

    $('#pay-mpesa-annually').text('');
    $('.alert-annually').empty();
    $('#pay-mpesa-annually').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/payments/lipa_api_with_billing',
        type: "POST",
        data: $('#form-pay-mpesa-annually').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert-annually').append(' <div class="alert-success">'+res.error+'</div>');


                window.setInterval(function(){
                    check_payment_lipa_status(res.check_id);

                },5000)


                $("html, body").animate({ scrollTop: 0 }, "slow");

            }else{
                $('#pay-mpesa-annually').empty();
                $('.alert-annually').append(' <div class="alert-error">'+res.error+'</div>');
                $('#pay-mpesa-annually').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#pay-mpesa-annually').empty();
            $('#pay-mpesa-annually').text('try again');
            $('.alert-annually').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});

//mpesa pay quarterly- by james mwangi
$('#form-pay-mpesa-quarterly').submit(function(e){
    e.preventDefault();

    $('#pay-mpesa-quarterly').text('');
    $('.alert-quarterly').empty();
    $('#pay-mpesa-quarterly').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/payments/lipa_api_with_billing',
        type: "POST",
        data: $('#form-pay-mpesa-quarterly').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert-quarterly').append(' <div class="alert-success">'+res.error+'</div>');


                window.setInterval(function(){
                    check_payment_lipa_status(res.check_id);

                },5000)


                $("html, body").animate({ scrollTop: 0 }, "slow");

            }else{
                $('#pay-mpesa-quarterly').empty();
                $('.alert-quarterly').append(' <div class="alert-error">'+res.error+'</div>');
                $('#pay-mpesa-quarterly').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#pay-mpesa-quarterly').empty();
            $('#pay-mpesa-quarterly').text('try again');
            $('.alert-quarterly').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});
//mpesa pay monthly- by james mwangi
$('#form-pay-mpesa-monthly').submit(function(e){
    e.preventDefault();

    $('#pay-mpesa-monthly').text('');
    $('.alert-monthly').empty();
    $('#pay-mpesa-monthly').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/payments/lipa_api_with_billing',
        type: "POST",
        data: $('#form-pay-mpesa-monthly').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert-monthly').append(' <div class="alert-success">'+res.error+'</div>');


                window.setInterval(function(){
                    check_payment_lipa_status(res.check_id);

                },5000)


                $("html, body").animate({ scrollTop: 0 }, "slow");

            }else{
                $('#pay-mpesa-monthly').empty();
                $('.alert-monthly').append(' <div class="alert-error">'+res.error+'</div>');
                $('#pay-mpesa-monthly').text('try again');
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#pay-mpesa-quarterly').empty();
            $('#pay-mpesa-quarterly').text('try again');
            $('.alert-quarterly').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});
function check_credit_payment_lipa_status(checkout_request_id) {

    $.ajax({
        url: baseURL + 'backend/ajax/payments/lipa_check_status_credit_payment',
        type: "POST",
        data: {checkout_request_id: checkout_request_id},
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true) {

                clearInterval()

                $('.alert-credits').append(' <div class="alert-success">'+res.error+'</div>');


                setTimeout(function () {
                    $(location).attr('href', '/backend/communication');
                }, 3000)
            }else{
                clearInterval();
                console.log(res.error);
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });
}
function check_payment_lipa_status(checkout_request_id) {

    $.ajax({
        url: baseURL + 'backend/ajax/payments/lipa_check_status',
        type: "POST",
        data: {checkout_request_id: checkout_request_id},
        dataType: "JSON",
        success: function (res) {
            //console.log(res);
            if(res.response==true) {

                clearInterval();

                $.niftyNoty({
                    type: 'success',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 10000

                });
                setTimeout(function () {
                    $(location).attr('href', '/backend/biling');
                }, 3000)
            }else{
                clearInterval();
               console.log(res.error);
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });
}
//mpesa pay monthly- by james mwangi
$('#form-pay-mpesa-monthly').submit(function(e){
    e.preventDefault();

    $('#pay-mpesa-monthly').text('');
    $('.alert-monthly').empty();
    $('#pay-mpesa-monthly').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/payments/lipa_api_with_billing',
        type: "POST",
        data: $('#form-pay-mpesa-monthly').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $('.alert-monthly').append(' <div class="alert-success">'+res.error+'</div>');


                window.setInterval(function(){
                    check_payment_lipa_status(res.check_id);

                },5000)


                $("html, body").animate({ scrollTop: 0 }, "slow");

            }else{
                $('#pay-mpesa-monthly').empty();
                $('.alert-monthly').append(' <div class="alert-error">'+res.error+'</div>');
                $('#pay-mpesa-monthly').text('try again');
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#pay-mpesa-quarterly').empty();
            $('#pay-mpesa-quarterly').text('try again');
            $('.alert-quarterly').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});

//get-transaction- by james mwangi
$('#form-get-settlements').submit(function(e){
    e.preventDefault();
    $('#get-transaction-section').slideUp();
    $('#get-transaction').text('');
    $('.alert').empty();
    $('#get-transaction').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/reports/get_settlements',
        type: "POST",
        data: $('#form-get-settlements').serialize(),
        dataType: "JSON",
        success: function (res) {

            if(res.response==true) {
                //set last
                $('.last_settlements').text(default_currency+res.last_settlements);
                //set next
                $('.next_settlements').text(default_currency+res.next_settlements);
                //set total transfers
                $('.total_transfers').text(default_currency+res.total_transfers);

                $.fn.DataTable.ext.pager.numbers_length = 5;
                // Row selection (single row)
                // -----------------------------------------------------------------

                $('#transaction-table').DataTable({
                    destroy: true,
                    searching: true,
                    //	"order": [[ 0, "desc" ]],
                    "columnDefs": [{
                        "defaultContent": "-",
                        "targets": "_all"
                    }],
                    "aaData": res.settlements,
                    "aoColumns": [{
                        "mDataProp": "from_date"
                    },{
                        "mDataProp": "to_date"
                    },{
                        "mDataProp": "total_transactions"
                    }, {
                        "mDataProp": "commission"
                    },{
                        "mDataProp": "transfer_charges"
                    },{
                        "mDataProp": "refunds"
                    },{
                        "mDataProp": "total_transferred"
                    }],
                    "responsive": true,
                    "language": {
                        "paginate": {
                            "previous": '<i class="demo-psi-arrow-left"></i>',
                            "next": '<i class="demo-psi-arrow-right"></i>'
                        }
                    }
                });


                $.niftyNoty({
                    type: 'success',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 10000

                });
                $('#get-transaction-section').slideDown();
                $('#get-transaction').empty();
                $('#get-transaction').text('Generate');
            }else{
                $('#get-transaction').empty();
                $('#get-transaction-section').slideUp();
                $.niftyNoty({
                    type: 'danger',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 10000

                });
                $('#get-transaction').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#statement-section-section').slideUp();
            $('#get-transaction').empty();
            $('#get-transaction').text('try again');
            $.niftyNoty({
                type: 'danger',
                container : 'floating',
                message : errorThrown,
                closeBtn : true,
                focus: true,
                timer: 10000

            });

        }
    });
});

//new sms
$('#form-send-sms').submit(function(e){
    e.preventDefault();

    $('#send-sms').text('');
    $('#send-sms').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/communication/sendsms',
        type: "POST",
        data: $('#form-send-sms').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res);
            if(res.response==true){

                $.niftyNoty({
                    type: 'success',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 10000
                });

                $("html, body").animate({scrollTop: 0}, "slow");
                $('#send-sms').empty();
                $('#send-sms').text('Send');
            }else{
               $('#send-sms').empty();
               $('#send-sms').text('try again');
                $.niftyNoty({
                    type: 'danger',
                    container : 'floating',
                    message : res.error,
                    closeBtn : true,
                    focus: true,
                    timer: 20000

                });
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#send-sms').empty();
            $('#send-sms').text('try again');
            $('.alert-quarterly').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');
            $.niftyNoty({
                type: 'danger',
                container : 'floating',
                message : 'Error:{'+errorThrown+'}',
                closeBtn : true,
                focus: true,
                timer: 20000

            });

        }
    });
});

$('#form-pay-mpesa-credits').submit(function(e){
    e.preventDefault();

    $('#pay-mpesa').text('');
    $('.alert-credits').empty();
    $('#pay-mpesa').append('<div class="circle"></div>');

    $.ajax({
        url:baseURL+'backend/ajax/payments/lipa_api_buy_credits',
        type: "POST",
        data: $('#form-pay-mpesa-credits').serialize(),
        dataType: "JSON",
        success: function (res) {
            console.log(res.check_id)
            if(res.response==true){

                $('.alert-credits').append(' <div class="alert-success">'+res.error+'</div>');
                window.setInterval(function(){
                    check_credit_payment_lipa_status(res.check_id);
                },5000)


                $("html, body").animate({ scrollTop: 0 }, "slow");

            }else{
                $('#pay-mpesa').empty();
                $('.alert-credits').append(' <div class="alert-error">'+res.error+'</div>');
                $('#pay-mpesa').text('try again');
            }


        },
        error: function (jqXHR, textStatus, errorThrown) {
            $('#pay-mpesa').empty();
            $('#pay-mpesa').text('try again');
            $('.alert-credits').append(' <div class="alert-error">Error:{'+errorThrown+'}</div>');

        }
    });
});
