This commit is contained in:
2025-10-08 10:13:07 +02:00
parent 70e055d20b
commit b74cf45c5c
10 changed files with 813 additions and 91 deletions

View File

@@ -316,7 +316,7 @@
$('#save-add-two').on('click', function() {
let formData = {
'client_id': $('#add-client-id').val(),
'date': $('#add-date').val(),
'date': $('#add-date').val(), // Ensure this is in YYYY-MM-DD format
'is_warm': $('#add-is-warm').is(':checked'),
'lhe_delivery': $('#add-lhe-delivery').val(),
'lhe_output': $('#add-lhe-output').val(),
@@ -324,6 +324,12 @@
'csrfmiddlewaretoken': '{{ csrf_token }}'
};
// Validate date format
if (!formData.date) {
alert('Please select a date');
return;
}
// Validate LHe Output is a number
if (isNaN(parseFloat(formData.lhe_output))) {
alert('LHe Output must be a valid number');
@@ -335,16 +341,32 @@
method: 'POST',
data: formData,
success: function(response) {
// Clear the form
$('#add-popup-two').find('input, textarea, select').val('');
$('#add-is-warm').prop('checked', false);
$('#add-popup-two').fadeOut();
// Reload the table data
loadTableData();
if (response.status === 'success') {
// Add the new row to the table
let newRow = `
<tr data-id="${response.id}">
<td>${$('#table-two tbody tr').length + 1}</td>
<td>${response.id}</td>
<td>${response.client_name}</td>
<td>${response.date || ''}</td>
<td>${response.is_warm ? 'Yes' : 'No'}</td>
<td>${response.lhe_delivery}</td>
<td>${response.lhe_output || ''}</td>
<td>${response.notes || ''}</td>
<td class="actions">
<button class="edit-btn-two">Edit</button>
<button class="delete-btn-two">Delete</button>
</td>
</tr>
`;
$('#table-two tbody').append(newRow);
$('#add-popup-two').fadeOut();
} else {
alert('Error: ' + (response.message || 'Failed to add entry'));
}
},
error: function(xhr) {
alert('Error: ' + xhr.responseJSON?.message || 'Failed to add entry');
alert('Error: ' + (xhr.responseJSON?.message || 'Server error'));
}
});
});
@@ -366,18 +388,22 @@
}
// Open Edit Popup
$(document).on('click', '.edit-btn-two', function () {
$(document).on('click', '.edit-btn-two', function() {
let row = $(this).closest('tr');
currentTableId = row.closest('table').attr('id');
currentModelName = 'SecondTableEntry';
// Get data from the row
let is_warm = row.find('td:eq(4)').text().trim() === 'Yes';
$('#edit-id').val(row.data('id'));
$('#edit-client-id').val(row.find('td:eq(2)').text().trim());
$('#edit-date').val(row.find('td:eq(3)').text().trim());
$('#edit-is-warm').prop('checked', is_warm);
// Set client - find by name since we're showing names in the table
let clientName = row.find('td:eq(2)').text().trim();
$(`#edit-client-id option:contains("${clientName}")`).prop('selected', true);
// Set date - ensure proper format
let dateText = row.find('td:eq(3)').text().trim();
if (dateText) {
$('#edit-date').val(dateText);
}
// Set other fields
$('#edit-is-warm').prop('checked', row.find('td:eq(4)').text().trim() === 'Yes');
$('#edit-lhe-delivery').val(row.find('td:eq(5)').text().trim());
$('#edit-lhe-output').val(row.find('td:eq(6)').text().trim());
$('#edit-notes').val(row.find('td:eq(7)').text().trim());
@@ -386,45 +412,48 @@
});
// Save Edit Entry
$('#save-edit-two').on('click', function () {
let id = $('#edit-id').val();
let client_id = $('#edit-client-id').val();
let date = $('#edit-date').val();
let is_warm = $('#edit-is-warm').is(':checked');
let lhe_delivery = $('#edit-lhe-delivery').val();
let lhe_output = $('#edit-lhe-output').val();
let notes = $('#edit-notes').val();
$('#save-edit-two').on('click', function() {
let formData = {
'id': $('#edit-id').val(),
'client_id': $('#edit-client-id').val(),
'date': $('#edit-date').val(), // Already in YYYY-MM-DD format
'is_warm': $('#edit-is-warm').is(':checked'),
'lhe_delivery': $('#edit-lhe-delivery').val(),
'lhe_output': $('#edit-lhe-output').val(),
'notes': $('#edit-notes').val(),
'csrfmiddlewaretoken': '{{ csrf_token }}'
};
// Validate inputs
if (!formData.date) {
alert('Please select a date');
return;
}
if (isNaN(parseFloat(formData.lhe_output))) {
alert('Please enter a valid LHe Output value');
return;
}
$.ajax({
url: `/update-entry/${currentModelName}/`,
url: '/update-entry/SecondTableEntry/',
method: 'POST',
data: {
'id': id,
'client_id': client_id,
'date': date,
'is_warm': is_warm,
'lhe_delivery': lhe_delivery,
'lhe_output': lhe_output,
'notes': notes,
'csrfmiddlewaretoken': '{{ csrf_token }}'
},
success: function (response) {
data: formData,
success: function(response) {
if (response.status === 'success') {
let row = $(`tr[data-id="${response.id}"]`);
row.find('td:eq(2)').text(response.client_name);
row.find('td:eq(3)').text(response.date);
row.find('td:eq(3)').text(response.date || '');
row.find('td:eq(4)').text(response.is_warm ? 'Yes' : 'No');
row.find('td:eq(5)').text(response.lhe_delivery);
row.find('td:eq(6)').text(response.lhe_output);
row.find('td:eq(7)').text(response.notes);
row.find('td:eq(6)').text(response.lhe_output || '');
row.find('td:eq(7)').text(response.notes || '');
$('#edit-popup-two').fadeOut();
} else {
alert('Update failed: ' + (response.message || 'Please try again later'));
alert('Update failed: ' + (response.message || 'Unknown error'));
}
},
error: function (xhr, status, error) {
alert('Update failed: ' + (xhr.responseJSON?.message || 'Please try again later'));
console.error('Error:', error);
error: function(xhr) {
alert('Error: ' + (xhr.responseJSON?.message || 'Server error'));
}
});
});