Files

264 lines
5.7 KiB
JavaScript
Raw Permalink Normal View History

2026-03-24 11:28:14 +05:30
2025-11-25 13:33:49 +05:30
window.onload = function () {
2026-03-23 11:37:15 +05:30
document.getElementById('Village_Name').focus();
};
2026-03-24 11:28:14 +05:30
2025-11-25 13:33:49 +05:30
$(document).ready(function () {
2026-03-24 11:28:14 +05:30
// 🔥 RESTORE VIEW MODE AFTER RELOAD
var viewMode = localStorage.getItem("viewMode");
if (viewMode === "table") {
$('#addForm').hide();
$('#addTable').show();
} else {
$('#addForm').show();
$('#addTable').hide();
}
// 🔥 BUTTON TOGGLE LOGIC
$('#addButton').click(function () {
$('#addForm').show();
$('#addTable').hide();
localStorage.setItem("viewMode", "form");
});
$('#displayButton').click(function () {
$('#addForm').hide();
$('#addTable').show();
localStorage.setItem("viewMode", "table");
});
2026-03-23 11:37:15 +05:30
// STATE → DISTRICT
$('#state_Id').change(function () {
2025-11-25 13:33:49 +05:30
2026-03-23 11:37:15 +05:30
var stateId = $(this).val();
2025-11-25 13:33:49 +05:30
2026-03-23 11:37:15 +05:30
if (stateId) {
2025-11-25 13:33:49 +05:30
2026-03-23 11:37:15 +05:30
$.ajax({
url: '/get_districts/' + stateId,
type: 'GET',
success: function (data) {
var districtDropdown = $('#district_Id');
districtDropdown.empty();
districtDropdown.append('<option value="" disabled selected>Select District</option>');
data.forEach(function (district) {
districtDropdown.append(
'<option value="' + district.id + '">' + district.name + '</option>'
);
});
districtDropdown.prop('disabled', false);
}
});
2025-11-25 13:33:49 +05:30
2026-03-23 11:37:15 +05:30
}
});
// DISTRICT → BLOCK
$('#district_Id').change(function () {
var districtId = $(this).val();
if (districtId) {
2025-11-25 13:33:49 +05:30
$.ajax({
2026-03-23 11:37:15 +05:30
url: '/get_blocks/' + districtId,
type: 'GET',
success: function (data) {
var blockDropdown = $('#block_Id');
blockDropdown.empty();
blockDropdown.append('<option value="" disabled selected>Select Block</option>');
data.forEach(function (block) {
blockDropdown.append(
'<option value="' + block.id + '">' + block.name + '</option>'
);
});
blockDropdown.prop('disabled', false);
}
});
}
});
// VILLAGE NAME VALIDATION
$('#Village_Name').on('input', function () {
var villageName = $(this).val();
var validPattern = /^[A-Za-z ]*$/;
if (!validPattern.test(villageName)) {
$('#villageMessage')
.text('Only letters and spaces are allowed!')
.css('color', 'red');
$('#submitVillage').prop('disabled', true);
} else {
$('#villageMessage').text('');
$('#submitVillage').prop('disabled', false);
}
});
// CHECK DUPLICATE VILLAGE
$('#Village_Name, #block_Id').on('change keyup', function () {
var blockId = $('#block_Id').val();
var villageName = $('#Village_Name').val().trim();
if (blockId && villageName) {
$.ajax({
url: '/check_village',
2025-11-25 13:33:49 +05:30
type: 'POST',
2026-03-23 11:37:15 +05:30
data: {
block_Id: blockId,
Village_Name: villageName
},
2025-11-25 13:33:49 +05:30
success: function (response) {
2026-03-23 11:37:15 +05:30
if (response.status === 'exists') {
$('#villageMessage')
.text(response.message)
.css('color', 'red');
$('#submitVillage').prop('disabled', true);
2025-11-25 13:33:49 +05:30
} else {
2026-03-23 11:37:15 +05:30
$('#villageMessage')
.text(response.message)
.css('color', 'green');
$('#submitVillage').prop('disabled', false);
2025-11-25 13:33:49 +05:30
}
2026-03-23 11:37:15 +05:30
2025-11-25 13:33:49 +05:30
},
2026-03-23 11:37:15 +05:30
2025-11-25 13:33:49 +05:30
error: function () {
2026-03-23 11:37:15 +05:30
$('#villageMessage')
.text('Error checking village name')
.css('color', 'red');
$('#submitVillage').prop('disabled', true);
2025-11-25 13:33:49 +05:30
}
2026-03-23 11:37:15 +05:30
2025-11-25 13:33:49 +05:30
});
2026-03-23 11:37:15 +05:30
}
});
// ADD VILLAGE
$('#villageForm').submit(function (event) {
event.preventDefault();
$.ajax({
url: '/add_village',
type: 'POST',
data: $(this).serialize(),
success: function (response) {
if (response.status === 'success') {
alert('Village added successfully!');
location.reload();
} else {
2026-03-24 11:28:14 +05:30
alert(response.message || 'Error adding village. Please try again.');
2026-03-23 11:37:15 +05:30
}
},
error: function () {
alert('An error occurred. Please try again.');
}
2025-11-25 13:33:49 +05:30
});
2026-03-23 11:37:15 +05:30
});
2026-03-24 11:28:14 +05:30
});
// 🔥 DELETE FUNCTION (UPDATED)
function deleteVillage(villageId) {
if (!confirm("Are you sure you want to delete this village?")) {
return;
}
// ✅ save that user is on table
localStorage.setItem("viewMode", "table");
$.ajax({
url: '/delete_village/' + villageId,
type: 'GET',
success: function () {
setTimeout(function () {
alert("Village deleted successfully!");
// reload but stay on table
location.reload();
}, 1000);
},
error: function () {
alert("Error deleting village. Please try again.");
}
});
}