diff --git a/admin/card/createCard.html b/admin/card/createCard.html index 97b632e..fb39816 100644 --- a/admin/card/createCard.html +++ b/admin/card/createCard.html @@ -3,7 +3,7 @@ checkRoleAccess(['officeAdmin', 'cardAdmin', 'superAdmin']); - + @@ -31,8 +31,9 @@
Back -   |   Home -   |   Logout +   |   + Home   | +   Logout
@@ -45,14 +46,26 @@
-
- +
- +
@@ -63,15 +76,35 @@
- +
- +
- +
@@ -83,17 +116,33 @@
- +
-
-
@@ -105,15 +154,32 @@
- +
- +
- @@ -169,7 +235,11 @@
- @@ -177,15 +247,41 @@
+ + + - diff --git a/admin/card/createCard.js b/admin/card/createCard.js index ffaa15c..b6e8374 100644 --- a/admin/card/createCard.js +++ b/admin/card/createCard.js @@ -44,7 +44,6 @@ // } // } - // const options = { // method: 'POST', // headers: { @@ -146,12 +145,77 @@ document.addEventListener('DOMContentLoaded', function () { // Load countries and setup cascading dropdowns loadLocationData(); - // Guest-specific fields toggle + // Load departments + loadDepartments(); + + // Residential status fields toggle const resStatusSelect = document.getElementById('res_status'); - resStatusSelect.addEventListener('change', () => toggleGuestFields(resStatusSelect.value)); - toggleGuestFields(resStatusSelect.value); + resStatusSelect.addEventListener('change', () => + toggleResStatusFields(resStatusSelect.value) + ); + toggleResStatusFields(resStatusSelect.value); + + // Reference phone validation listener + const refPhoneInput = document.getElementById('reference_phone'); + if (refPhoneInput) { + refPhoneInput.addEventListener('input', async (e) => { + const val = e.target.value.trim(); + const nameEl = document.getElementById('refPhoneName'); + if (val.length === 10) { + nameEl.textContent = 'Checking...'; + nameEl.style.color = '#777'; + try { + const res = await fetch( + `${CONFIG.baseUrl}/client/checkMobile/${val}` + ); + const data = await res.json(); + if (data.exists) { + if (data.res_status === 'MUMUKSHU') { + nameEl.textContent = `Name: ${data.name} (Mumukshu)`; + nameEl.style.color = '#2e7d32'; + } else { + nameEl.textContent = `Name: ${data.name} (${data.res_status}) - Warning: Not a Mumukshu`; + nameEl.style.color = '#c62828'; + } + } else { + nameEl.textContent = 'Phone number is not registered!'; + nameEl.style.color = '#c62828'; + } + } catch (err) { + nameEl.textContent = 'Error checking phone number'; + nameEl.style.color = '#c62828'; + } + } else { + nameEl.textContent = ''; + } + }); + } }); +// --- Fetch and populate departments --- +async function loadDepartments() { + try { + const res = await fetch(`${CONFIG.basePath}/location/departments`, { + headers: { + Authorization: `Bearer ${sessionStorage.getItem('token')}`, + 'Content-Type': 'application/json' + } + }); + const result = await res.json(); + const deptSelect = document.getElementById('department'); + deptSelect.innerHTML = ''; + (result.data || []).forEach((d) => { + const val = d.value || d; + const opt = document.createElement('option'); + opt.value = val; + opt.textContent = val; + deptSelect.appendChild(opt); + }); + } catch (err) { + console.error('Failed to load departments:', err); + } +} + // --- Form submit handler --- async function assignCard(event) { event.preventDefault(); @@ -181,11 +245,23 @@ async function assignCard(event) { res_status: form.res_status.value }; - if (form.res_status.value === "GUEST") { - bodyData.referenceCardno = form.reference_cardno?.value?.trim(); + if (form.res_status.value === 'GUEST') { + bodyData.referencePhone = form.reference_phone?.value?.trim(); bodyData.guestType = form.guest_type?.value?.trim(); - if (!bodyData.referenceCardno || !bodyData.guestType) { - alert("Please enter both Reference Card Number and Guest Type for GUEST users."); + if (!bodyData.referencePhone || !bodyData.guestType) { + alert( + 'Please enter both Reference Phone Number and Guest Type for GUEST users.' + ); + submitBtn.disabled = false; + submitBtn.textContent = 'Submit'; + return; + } + } + + if (form.res_status.value === 'SEVA KUTIR') { + bodyData.department = form.department?.value; + if (!bodyData.department) { + alert('Please select a department for SEVA KUTIR users.'); submitBtn.disabled = false; submitBtn.textContent = 'Submit'; return; @@ -215,31 +291,44 @@ async function assignCard(event) { } } - -// --- Toggle GUEST fields --- -function toggleGuestFields(res_status) { - document.getElementById('guestFields').style.display = res_status === 'GUEST' ? 'block' : 'none'; +// --- Toggle status fields --- +function toggleResStatusFields(res_status) { + document.getElementById('guestFields').style.display = + res_status === 'GUEST' ? 'block' : 'none'; + document.getElementById('sevaKutirFields').style.display = + res_status === 'SEVA KUTIR' ? 'block' : 'none'; } // --- Load countries, states, cities --- -async function loadLocationData(currentCountry = '', currentState = '', currentCity = '') { +async function loadLocationData( + currentCountry = '', + currentState = '', + currentCity = '' +) { const token = sessionStorage.getItem('token'); // --- Countries --- try { const countriesRes = await fetch(`${CONFIG.basePath}/location/countries`, { - headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } + headers: { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + } }); const countriesData = await countriesRes.json(); const countrySelect = document.getElementById('country'); countrySelect.innerHTML = ''; - (countriesData.data || ['India','USA','UK','UAE','Canada']).forEach(c => { - const val = c.value || c; - const selected = val === currentCountry ? 'selected' : ''; - countrySelect.innerHTML += ``; - }); - - countrySelect.addEventListener('change', () => fetchStates(countrySelect.value)); + (countriesData.data || ['India', 'USA', 'UK', 'UAE', 'Canada']).forEach( + (c) => { + const val = c.value || c; + const selected = val === currentCountry ? 'selected' : ''; + countrySelect.innerHTML += ``; + } + ); + + countrySelect.addEventListener('change', () => + fetchStates(countrySelect.value) + ); if (currentCountry) fetchStates(currentCountry, currentState, currentCity); } catch (err) { console.error('Failed to load countries:', err); @@ -249,21 +338,30 @@ async function loadLocationData(currentCountry = '', currentState = '', currentC async function fetchStates(country, selectedState = '', selectedCity = '') { const stateSelect = document.getElementById('state'); stateSelect.innerHTML = ''; - document.getElementById('city').innerHTML = ''; + document.getElementById('city').innerHTML = + ''; if (!country) return; try { - const stateRes = await fetch(`${CONFIG.basePath}/location/states/${country}`, { - headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } - }); + const stateRes = await fetch( + `${CONFIG.basePath}/location/states/${country}`, + { + headers: { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + } + } + ); const stateData = await stateRes.json(); - (stateData.data || []).forEach(s => { + (stateData.data || []).forEach((s) => { const val = s.value || s; const selected = val === selectedState ? 'selected' : ''; stateSelect.innerHTML += ``; }); - stateSelect.addEventListener('change', () => fetchCities(country, stateSelect.value)); + stateSelect.addEventListener('change', () => + fetchCities(country, stateSelect.value) + ); if (selectedState) fetchCities(country, selectedState, selectedCity); } catch (err) { console.error('Failed to load states:', err); @@ -277,11 +375,17 @@ async function loadLocationData(currentCountry = '', currentState = '', currentC if (!country || !state) return; try { - const cityRes = await fetch(`${CONFIG.basePath}/location/cities/${country}/${state}`, { - headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } - }); + const cityRes = await fetch( + `${CONFIG.basePath}/location/cities/${country}/${state}`, + { + headers: { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + } + } + ); const cityData = await cityRes.json(); - (cityData.data || []).forEach(c => { + (cityData.data || []).forEach((c) => { const val = c.value || c; const selected = val === selectedCity ? 'selected' : ''; citySelect.innerHTML += ``; diff --git a/admin/card/updateCard.html b/admin/card/updateCard.html index 01c3027..8d56685 100644 --- a/admin/card/updateCard.html +++ b/admin/card/updateCard.html @@ -3,209 +3,312 @@ checkRoleAccess(['officeAdmin', 'cardAdmin', 'superAdmin']); // customize allowed roles per page - + - - - - Update Card Details - - - - - - - - - - - - - - - - -
-
-
- Back -   |   Home   |   - Logout + + + + Update Card Details + + + + + + + + + + + + + + + + +
+
+
+ Back +   |   + Home   | +   + Logout +
-
- -
-
-
-
-
-

Update Card Details

-
-
-
-
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- - -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- - - - - -
- -
-
-
+
+
+
+
+
+

Update Card Details

+
+ +
+
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + + + + + + + + +
+ +
+
+
+
-
- + diff --git a/admin/card/updateCard.js b/admin/card/updateCard.js index 4c3150c..4092df3 100644 --- a/admin/card/updateCard.js +++ b/admin/card/updateCard.js @@ -2,10 +2,15 @@ document.addEventListener('DOMContentLoaded', async () => { const cardno = sessionStorage.getItem('cardno'); if (!cardno) return alert('No card number found in session'); + // Load departments dropdown list + await loadDepartments(); + await fetchPersonDetails(cardno); // Attach submit listener - document.getElementById('updateForm').addEventListener('submit', handleUpdate); + document + .getElementById('updateForm') + .addEventListener('submit', handleUpdate); // Attach change listeners once document.getElementById('country').addEventListener('change', (e) => { @@ -18,8 +23,60 @@ document.addEventListener('DOMContentLoaded', async () => { const state = e.target.value; fetchCities(country, state); }); + + document.getElementById('res_status').addEventListener('change', (e) => { + toggleResStatusFields(e.target.value); + }); + + // Reference phone validation listener + const refPhoneInput = document.getElementById('referencePhone'); + if (refPhoneInput) { + refPhoneInput.addEventListener('input', (e) => { + checkReferencePhone(e.target.value.trim()); + }); + } }); +// --- Fetch and populate departments --- +async function loadDepartments() { + try { + const token = sessionStorage.getItem('token'); + const res = await fetch(`${CONFIG.basePath}/location/departments`, { + headers: { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + } + }); + const result = await res.json(); + const deptSelect = document.getElementById('department'); + deptSelect.innerHTML = ''; + (result.data || []).forEach((d) => { + const val = d.value || d; + const opt = document.createElement('option'); + opt.value = val; + opt.textContent = val; + deptSelect.appendChild(opt); + }); + } catch (err) { + console.error('Failed to load departments:', err); + } +} + +// --- Toggle status fields --- +function toggleResStatusFields(res_status) { + // Toggle guest-only fields + const guestElements = document.querySelectorAll('.guest-only'); + guestElements.forEach((el) => { + el.style.display = res_status === 'GUEST' ? 'block' : 'none'; + }); + + // Toggle seva-kutir-only fields + const sevaKutirElements = document.querySelectorAll('.seva-kutir-only'); + sevaKutirElements.forEach((el) => { + el.style.display = res_status === 'SEVA KUTIR' ? 'block' : 'none'; + }); +} + // --- Handle form submit --- async function handleUpdate(e) { e.preventDefault(); @@ -39,15 +96,35 @@ async function handleUpdate(e) { pin: document.getElementById('pin').value, center: document.getElementById('center').value, res_status: document.getElementById('res_status').value, - referenceCardno: document.getElementById('referenceCardno')?.value || null, - guestType: document.getElementById('guestType')?.value || null + referencePhone: document.getElementById('referencePhone')?.value || null, + guestType: document.getElementById('guestType')?.value || null, + department: document.getElementById('department')?.value || null }; + if (updatedData.res_status === 'GUEST') { + if (!updatedData.referencePhone || !updatedData.guestType) { + alert( + 'Please enter both Reference Phone Number and Guest Type for GUEST users.' + ); + return; + } + } + + if (updatedData.res_status === 'SEVA KUTIR') { + if (!updatedData.department) { + alert('Please select a department for SEVA KUTIR users.'); + return; + } + } + try { const token = sessionStorage.getItem('token'); const response = await fetch(`${CONFIG.basePath}/card/update`, { method: 'PUT', - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + }, body: JSON.stringify(updatedData) }); if (!response.ok) throw new Error('Failed to update card'); @@ -64,7 +141,10 @@ async function fetchPersonDetails(cardno) { try { const token = sessionStorage.getItem('token'); const res = await fetch(`${CONFIG.basePath}/card/search/${cardno}`, { - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } }); if (!res.ok) throw new Error('Failed to fetch person details'); const result = await res.json(); @@ -78,10 +158,35 @@ async function fetchPersonDetails(cardno) { // --- Populate form --- function populateForm(data) { - ['cardno','issuedto','gender','dob','mobno','email','idType','idNo','address','pin','res_status'].forEach(field => { - document.getElementById(field).value = data[field] || ''; + [ + 'cardno', + 'issuedto', + 'gender', + 'dob', + 'mobno', + 'email', + 'idType', + 'idNo', + 'address', + 'pin', + 'res_status', + 'referencePhone', + 'guestType', + 'department' + ].forEach((field) => { + const element = document.getElementById(field); + if (element) { + element.value = data[field] || ''; + } }); + // Toggle fields based on populated status + toggleResStatusFields(data.res_status); + + if (data.res_status === 'GUEST' && data.referencePhone) { + checkReferencePhone(data.referencePhone); + } + fetchCountries(data.country, data.state, data.city); fetchCenters(data.center); // <-- call this to populate center dropdown correctly } @@ -94,16 +199,27 @@ async function fetchCountries(currentCountry, currentState, currentCity) { try { const token = sessionStorage.getItem('token'); const res = await fetch(`${CONFIG.basePath}/location/countries`, { - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } }); - const data = (await res.json()).data || ['India','USA','UK','UAE','Canada']; - data.forEach(c => { + const data = (await res.json()).data || [ + 'India', + 'USA', + 'UK', + 'UAE', + 'Canada' + ]; + data.forEach((c) => { const val = c.value || c; const selected = val === currentCountry ? 'selected' : ''; countryDropdown.innerHTML += ``; }); if (currentCountry) fetchStates(currentCountry, currentState, currentCity); - } catch (err) { console.warn(err); } + } catch (err) { + console.warn(err); + } } // --- Fetch states --- @@ -115,16 +231,21 @@ async function fetchStates(country, currentState, currentCity) { try { const token = sessionStorage.getItem('token'); const res = await fetch(`${CONFIG.basePath}/location/states/${country}`, { - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } }); const data = (await res.json()).data || []; - data.forEach(s => { + data.forEach((s) => { const val = s.value || s; const selected = val === currentState ? 'selected' : ''; stateDropdown.innerHTML += ``; }); if (currentState) fetchCities(country, currentState, currentCity); - } catch (err) { console.error(err); } + } catch (err) { + console.error(err); + } } // --- Fetch cities --- @@ -135,16 +256,24 @@ async function fetchCities(country, state, currentCity) { try { const token = sessionStorage.getItem('token'); - const res = await fetch(`${CONFIG.basePath}/location/cities/${country}/${state}`, { - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } - }); + const res = await fetch( + `${CONFIG.basePath}/location/cities/${country}/${state}`, + { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } + } + ); const data = (await res.json()).data || []; - data.forEach(c => { + data.forEach((c) => { const val = c.value || c; const selected = val === currentCity ? 'selected' : ''; cityDropdown.innerHTML += ``; }); - } catch (err) { console.error(err); } + } catch (err) { + console.error(err); + } } const fetchCenters = async (currentCenter) => { @@ -154,21 +283,58 @@ const fetchCenters = async (currentCenter) => { try { const token = sessionStorage.getItem('token'); const res = await fetch(`${CONFIG.basePath}/location/centres`, { - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } }); const centersData = (await res.json()).data || []; - centersData.forEach(c => { + centersData.forEach((c) => { const val = c.value || c; const selected = val === currentCenter ? 'selected' : ''; centerDropdown.innerHTML += ``; }); // If currentCenter is not in the fetched list, add it - if (currentCenter && !centersData.find(c => (c.value || c) === currentCenter)) { + if ( + currentCenter && + !centersData.find((c) => (c.value || c) === currentCenter) + ) { centerDropdown.innerHTML += ``; } } catch (err) { console.error('Error fetching centers:', err); } }; + +// --- Check and display reference phone Mumukshu name --- +async function checkReferencePhone(val) { + const nameEl = document.getElementById('refPhoneName'); + if (!nameEl) return; + if (!val || val.trim().length !== 10) { + nameEl.textContent = ''; + return; + } + nameEl.textContent = 'Checking...'; + nameEl.style.color = '#777'; + try { + const res = await fetch(`${CONFIG.baseUrl}/client/checkMobile/${val}`); + const data = await res.json(); + if (data.exists) { + if (data.res_status === 'MUMUKSHU') { + nameEl.textContent = `Name: ${data.name} (Mumukshu)`; + nameEl.style.color = '#2e7d32'; + } else { + nameEl.textContent = `Name: ${data.name} (${data.res_status}) - Warning: Not a Mumukshu`; + nameEl.style.color = '#c62828'; + } + } else { + nameEl.textContent = 'Phone number is not registered!'; + nameEl.style.color = '#c62828'; + } + } catch (err) { + nameEl.textContent = 'Error checking phone number'; + nameEl.style.color = '#c62828'; + } +}