Skip to content
Snippets Groups Projects

Add and delete hospital

Merged Burhan Akbar requested to merge ba into main
2 files
+ 67
242
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -95,7 +95,47 @@ $(document).ready(function () {
});
// Form Submit Handler
$("#hospitalForm").submit(handleFormSubmit);
$("#hospitalForm").submit(function(e) {
e.preventDefault();
const formData = {
name: $("#hospitalName").val(),
location: $("#hospitalLocation").val(),
address: $("#hospitalLocation").val(), // Match DB schema
phone: $("#hospitalPhone").val(),
totalBeds: parseInt($("#totalBeds").val()),
availableBeds: parseInt($("#availableBeds").val()),
type: 'hospital',
status: 1,
roleId: 3,
email: '',
password: '$2a$10$wvAZoj4V51MH/MLhVIrnG.NrY07/.Gn9Ar6JsyzRAubWtqbWzgKie'
};
if (!validateFormData(formData)) {
showError('Please fill all required fields');
return;
}
$.ajax({
url: API_BASE_URL,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(formData),
success: function(response) {
if(response && response.code === 1) {
$("#hospitalModal").fadeOut();
loadHospitals();
showSuccess('Hospital added successfully');
} else {
showError(response.msg || DEFAULT_ERROR);
}
},
error: function(xhr, status, error) {
console.error('API Error:', error);
showError(DEFAULT_ERROR);
}
});
});
// Delete Handler
$(document).on('click', '.delete-btn', function() {
@@ -105,15 +145,16 @@ $(document).ready(function () {
url: `${API_BASE_URL}/${id}`,
method: 'DELETE',
success: function(response) {
if(response.code === 200) {
if(response && response.code === 1) {
loadHospitals();
showSuccess('Hospital deleted successfully');
} else {
alert(response.msg || 'Delete failed');
showError(response.msg || 'Delete failed');
}
},
error: function(xhr) {
alert('Delete failed');
console.error('Delete Error:', xhr);
showError('Failed to delete hospital');
}
});
}
@@ -191,6 +232,27 @@ $(document).ready(function () {
$('#hospitalTable tbody').html(`<tr><td colspan="8" class="text-center text-danger">${message}</td></tr>`);
}
function validateFormData(data) {
return data.name &&
data.location &&
data.phone &&
data.totalBeds > 0 &&
data.availableBeds >= 0 &&
data.availableBeds <= data.totalBeds;
}
function showSuccess(message) {
const alert = `
<div class="alert alert-success">
${message}
</div>
`;
$('.table-container').before(alert);
setTimeout(() => {
$('.alert').fadeOut().remove();
}, 3000);
}
// Add CSS for new components
const styles = `
.top-nav {
@@ -273,5 +335,5 @@ $(document).ready(function () {
function handleSignOut() {
localStorage.clear();
window.location.href = '/login';
window.location.href = '/login.html';
}
Loading