|
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + const form = document.getElementById('adhyayanRegistrationForm'); |
| 3 | + const adhyayanSelect = document.getElementById('adhyayanSelect'); |
| 4 | + const tableBody = document.getElementById('tableBody'); |
| 5 | + const addRowBtn = document.getElementById('addRowBtn'); |
| 6 | + |
| 7 | + let rowCounter = 0; |
| 8 | + |
| 9 | + init(); |
| 10 | + |
| 11 | + async function init() { |
| 12 | + addRowBtn.disabled = true; |
| 13 | + addRowBtn.addEventListener('click', addRow); |
| 14 | + adhyayanSelect.addEventListener('change', onAdhyayanChange); |
| 15 | + form.addEventListener('submit', onSubmit); |
| 16 | + |
| 17 | + await populateAdhyayanSelect(); |
| 18 | + } |
| 19 | + |
| 20 | + async function populateAdhyayanSelect() { |
| 21 | + const res = await fetch(`${CONFIG.basePath}/adhyayan/fetchALLAdhyayan`, { |
| 22 | + headers: authHeader() |
| 23 | + }); |
| 24 | + const json = await res.json(); |
| 25 | + |
| 26 | + adhyayanSelect.innerHTML = |
| 27 | + `<option value="">-- Select Adhyayan --</option>`; |
| 28 | + |
| 29 | + json.data.forEach(a => { |
| 30 | + const opt = document.createElement('option'); |
| 31 | + opt.value = a.id; |
| 32 | + opt.textContent = a.name; |
| 33 | + adhyayanSelect.appendChild(opt); |
| 34 | + }); |
| 35 | + |
| 36 | + // ✅ AUTO-SELECT FROM QUERY PARAM |
| 37 | + const params = new URLSearchParams(window.location.search); |
| 38 | + const shibirIdFromUrl = params.get('shibir_id'); |
| 39 | + |
| 40 | + if (shibirIdFromUrl) { |
| 41 | + adhyayanSelect.value = shibirIdFromUrl; |
| 42 | + |
| 43 | + // if ID exists in dropdown, trigger change |
| 44 | + if (adhyayanSelect.value === shibirIdFromUrl) { |
| 45 | + onAdhyayanChange(); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + function onAdhyayanChange() { |
| 51 | + addRowBtn.disabled = !adhyayanSelect.value; |
| 52 | + tableBody.innerHTML = ''; |
| 53 | + rowCounter = 0; |
| 54 | + } |
| 55 | + |
| 56 | + function addRow() { |
| 57 | + rowCounter++; |
| 58 | + |
| 59 | + const tr = document.createElement('tr'); |
| 60 | + tr.innerHTML = ` |
| 61 | + <td>${rowCounter}</td> |
| 62 | + <td><input data-f="mobno" class="form-control" placeholder="Mobile"></td> |
| 63 | + <td><input data-f="name" class="form-control" disabled></td> |
| 64 | + <td><input data-f="cardno" class="form-control" disabled></td> |
| 65 | + <td><input data-f="center" class="form-control" disabled></td> |
| 66 | + <td> |
| 67 | + <button type="button" class="btn btn-danger">Remove</button> |
| 68 | + </td> |
| 69 | + `; |
| 70 | + |
| 71 | + tr.querySelector('.btn-danger').onclick = () => { |
| 72 | + tr.remove(); |
| 73 | + renumber(); |
| 74 | + }; |
| 75 | + |
| 76 | + attachMobileLookup(tr); |
| 77 | + tableBody.appendChild(tr); |
| 78 | + } |
| 79 | + |
| 80 | + function attachMobileLookup(tr) { |
| 81 | + tr.querySelector('[data-f="mobno"]').addEventListener('blur', async e => { |
| 82 | + const mob = e.target.value.trim(); |
| 83 | + if (mob.length < 10) return; |
| 84 | + |
| 85 | + try { |
| 86 | + const res = await fetch( |
| 87 | + `${CONFIG.basePath}/card/by-mobile/${mob}`, |
| 88 | + { headers: authHeader() } |
| 89 | + ); |
| 90 | + const json = await res.json(); |
| 91 | + |
| 92 | + if (json.data) { |
| 93 | + tr.querySelector('[data-f="name"]').value = json.data.issuedto || ''; |
| 94 | + tr.querySelector('[data-f="cardno"]').value = json.data.cardno || ''; |
| 95 | + tr.querySelector('[data-f="center"]').value = json.data.center || ''; |
| 96 | + } |
| 97 | + } catch { |
| 98 | + console.warn('Mobile lookup failed'); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + function renumber() { |
| 104 | + [...tableBody.children].forEach( |
| 105 | + (tr, i) => (tr.children[0].textContent = i + 1) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + function collectRows() { |
| 110 | + const rows = []; |
| 111 | + |
| 112 | + tableBody.querySelectorAll('tr').forEach(tr => { |
| 113 | + const cardno = tr.querySelector('[data-f="cardno"]').value; |
| 114 | + if (cardno) rows.push(cardno); |
| 115 | + }); |
| 116 | + |
| 117 | + return rows; |
| 118 | + } |
| 119 | + |
| 120 | + async function onSubmit(e) { |
| 121 | + e.preventDefault(); |
| 122 | + |
| 123 | + const mumukshus = collectRows(); |
| 124 | + if (!mumukshus.length) { |
| 125 | + alert('Please add at least one valid Mumukshu'); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + const res = await fetch(`${CONFIG.basePath}/adhyayan/booking/admin`, { |
| 130 | + method: 'POST', |
| 131 | + headers: authHeader(), |
| 132 | + body: JSON.stringify({ |
| 133 | + shibir_ids: [adhyayanSelect.value], |
| 134 | + mumukshus |
| 135 | + }) |
| 136 | +}); |
| 137 | + |
| 138 | + |
| 139 | + const json = await res.json(); |
| 140 | + |
| 141 | + if (res.ok) { |
| 142 | + alert('Adhyayan registrations completed'); |
| 143 | + window.location.href = |
| 144 | + `/admin/adhyayan/adhyayanBookingslist.html?shibir_id=${adhyayanSelect.value}`; |
| 145 | + } else { |
| 146 | + alert(json.message || 'Failed to register'); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + function authHeader() { |
| 151 | + return { |
| 152 | + 'Content-Type': 'application/json', |
| 153 | + Authorization: `Bearer ${sessionStorage.getItem('token')}` |
| 154 | + }; |
| 155 | + } |
| 156 | +}); |
0 commit comments