$(document).ready(function() {
var business = {};
getBusinessInfo(business);
getAppointments();
$("#add_service").click(function () {
var input_service = $("#input_service").val();
// clear the input
$("#input_service").val('');
$("#predefined_services_admin_panel").append(
'
');
event.preventDefault();
});
$("#update_services_button").click(function () {
businesses = [];
console.log("Gjoko");
servicesObj = [];
$.each($('#predefined_services_admin_panel input:checked').siblings(), function (index, label) {
let service = {};
var id = $(label).prop('for');
var text = $(label).text();
var time = $($($(label).parent()).siblings()[0]).children()[0].value;
var price = $($($(label).parent()).siblings()[1]).children()[0].value;
var serviceType = {}
if (parseInt(id) != -1) {
service['id'] = parseInt(id);
serviceType['id'] = business['services'].find(obj => obj.id === parseInt(id))['serviceType']['id'];
} else {
}
serviceType['name'] = text.trim();
service['serviceType'] = serviceType;
service['duration'] = parseInt(time);
service['price'] = parseInt(price);
servicesObj.push(service);
});
business['services'] = servicesObj;
businesses.push(business);
console.log(JSON.stringify(businesses));
$.ajax({
url: "http://localhost:8080/api/business",
type:"PATCH",
data: JSON.stringify(businesses),
contentType:"application/json; charset=utf-8",
dataType: 'text',
success: function(succ){
alert( "Updates applied successfully" );
},
error: function(err) {
alert(err);
}
});
event.preventDefault();
});
$("#update_owner_button").click(function() {
businesses = [];
business['owner']['firstName'] = $('#firstName').val();
business['owner']['lastName'] = $('#lastName').val();
business['owner']['email'] = $('#email').val();
business['owner']['phoneNumber'] = $('#phoneNumber').val();
business['owner']['username'] = $('#username').val();
businesses.push(business);
console.log(JSON.stringify(businesses));
$.ajax({
url: "http://localhost:8080/api/business",
type:"PATCH",
data: JSON.stringify(businesses),
contentType:"application/json; charset=utf-8",
dataType: 'text',
success: function(succ){
alert( "Updates applied successfully" );
},
error: function(err) {
alert(err);
}
});
});
event.preventDefault();
});
function cancelAppointment(appointmentId) {
if (confirm("Are you sure you want to cancel the appointment?")) {
$.ajax({
url: "http://localhost:8080/api/appointment/" + appointmentId,
type:"DELETE"
}).success(function (data) {
alert("Appointment successfully canceled.")
getAppointments();
}).error(function (error) {
alert("Something went wrong.");
});
}
}
function getAppointments() {
$.ajax({
url: "http://localhost:8080/api/appointment/future/me"
}).then(function (data) {
console.log(data);
var $el = $("#bookings-table-body");
$el.empty();
$.each(data, function (index, obj) {
var element =
'' +
' ' + (parseInt(index) + 1) + ' | ' +
' ' + obj['fullName'] + ' | ' +
' ' + obj['email'] + ' | ' +
' ' + obj['phoneNumber'] + ' | ' +
' ' + obj['timePeriod'] + ' | ' +
' ' + obj['serviceName'] + ' | ';
switch (obj['status']) {
case 'NEW':
element += ' | ';
break;
case 'CANCELLED_BY_CUSTOMER':
element += ' | ';
break;
case 'CANCELLED_BY_BUSINESS_OWNER':
element += ' | ';
break;
}
element+='
';
$el.append(element);
});
});
}
function getBusinessInfo(business) {
$.ajax({
url: "http://localhost:8080/api/business/me"
}).then(function (data) {
business = data;
var $header = $("#header");
// header
$header.text($header.text() + " " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
// business info
if(business['businessStatus'] == 'NEW') {
$('#new_business_warning').removeAttr("hidden");
}
$('#business_status').val(business['businessStatus']);
$('#business_type').val(business['businessType']['text']);
// owner info
$('#firstName').val(business['owner']['firstName']);
$('#lastName').val(business['owner']['lastName']);
$('#phoneNumber').val(business['owner']['phoneNumber']);
$('#email').val(business['owner']['email']);
$('#username').val(business['owner']['username']);
// services info
var $el = $("#predefined_services_admin_panel");
$el.empty();
$.each(business['services'], function (index, obj) {
$el.append(
'');
});
});
}