| 36152 |
aman |
1 |
// Sidebar menu click handlers
|
|
|
2 |
$(document).on('click', '.wod-request-panel', function () {
|
|
|
3 |
doGetAjaxRequestHandler(context + '/wod-request-panel', function (response) {
|
|
|
4 |
$('#main-content').html(response);
|
|
|
5 |
initFinServicePanel();
|
|
|
6 |
});
|
|
|
7 |
});
|
|
|
8 |
|
|
|
9 |
$(document).on('click', '.wod-request-list', function () {
|
|
|
10 |
doGetAjaxRequestHandler(context + '/wod-request-list', function (response) {
|
|
|
11 |
$('#main-content').html(response);
|
|
|
12 |
});
|
|
|
13 |
});
|
|
|
14 |
|
|
|
15 |
// Template Admin handlers
|
|
|
16 |
$(document).on('click', '.wod-template-admin', function () {
|
|
|
17 |
doGetAjaxRequestHandler(context + '/wod-template-admin', function (response) {
|
|
|
18 |
$('#main-content').html(response);
|
|
|
19 |
});
|
|
|
20 |
});
|
|
|
21 |
|
|
|
22 |
$(document).on('click', '.wod-template-new', function () {
|
|
|
23 |
doGetAjaxRequestHandler(context + '/wod-template/edit?id=0', function (response) {
|
|
|
24 |
$('#main-content').html(response);
|
|
|
25 |
});
|
|
|
26 |
});
|
|
|
27 |
|
|
|
28 |
$(document).on('click', '.wod-template-edit', function () {
|
|
|
29 |
var id = $(this).data('id');
|
|
|
30 |
doGetAjaxRequestHandler(context + '/wod-template/edit?id=' + id, function (response) {
|
|
|
31 |
$('#main-content').html(response);
|
|
|
32 |
});
|
|
|
33 |
});
|
|
|
34 |
|
|
|
35 |
$(document).on('click', '.wod-template-delete', function () {
|
|
|
36 |
var id = $(this).data('id');
|
|
|
37 |
if (!confirm('Are you sure you want to deactivate this template?')) return;
|
|
|
38 |
$.post(context + '/wod-template/delete', {id: id}, function (resp) {
|
|
|
39 |
var result = resp;
|
|
|
40 |
try {
|
|
|
41 |
if (typeof resp === 'string') result = JSON.parse(resp);
|
|
|
42 |
} catch (e) {
|
|
|
43 |
}
|
|
|
44 |
if (result && result.success) {
|
|
|
45 |
doGetAjaxRequestHandler(context + '/wod-template-admin', function (response) {
|
|
|
46 |
$('#main-content').html(response);
|
|
|
47 |
});
|
|
|
48 |
}
|
|
|
49 |
});
|
|
|
50 |
});
|
|
|
51 |
|
|
|
52 |
var finServiceTemplateId = null;
|
|
|
53 |
var finServiceFormData = null;
|
|
|
54 |
|
|
|
55 |
function initFinServicePanel() {
|
|
|
56 |
$('#partnerSelect').select2({
|
|
|
57 |
placeholder: '-- Search Partner --',
|
|
|
58 |
allowClear: true,
|
|
|
59 |
width: '100%'
|
|
|
60 |
});
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// Partner selected -> enable brand, reset downstream
|
|
|
64 |
$(document).on('change', '#partnerSelect', function () {
|
|
|
65 |
finServiceTemplateId = null;
|
|
|
66 |
if ($(this).val()) {
|
|
|
67 |
$('#brandSelect').prop('disabled', false);
|
|
|
68 |
} else {
|
|
|
69 |
$('#brandSelect').prop('disabled', true).val('');
|
|
|
70 |
$('#stateSelect').prop('disabled', true).html('<option value="">-- Select State --</option>');
|
|
|
71 |
$('#distributorSelect').prop('disabled', true).val('');
|
|
|
72 |
$('#loadFormBtn').prop('disabled', true);
|
|
|
73 |
$('#dynamicFormContainer').hide();
|
|
|
74 |
}
|
|
|
75 |
});
|
|
|
76 |
|
|
|
77 |
// Brand selected -> fetch states
|
|
|
78 |
$(document).on('change', '#brandSelect', function () {
|
|
|
79 |
var brand = $(this).val();
|
|
|
80 |
finServiceTemplateId = null;
|
|
|
81 |
if (!brand) {
|
|
|
82 |
$('#stateSelect').prop('disabled', true).html('<option value="">-- Select State --</option>');
|
|
|
83 |
$('#distributorSelect').prop('disabled', true).val('');
|
|
|
84 |
$('#loadFormBtn').prop('disabled', true);
|
|
|
85 |
return;
|
|
|
86 |
}
|
|
|
87 |
doGetAjaxRequestHandler(context + '/wod-request/templates?brand=' + encodeURIComponent(brand), function (response) {
|
|
|
88 |
var templates = typeof response === 'string' ? JSON.parse(response) : response;
|
|
|
89 |
var html = '<option value="">-- Select State --</option>';
|
|
|
90 |
templates.forEach(function (t) {
|
|
|
91 |
html += '<option value="' + t.id + '">' + t.state + '</option>';
|
|
|
92 |
});
|
|
|
93 |
$('#stateSelect').html(html).prop('disabled', false);
|
|
|
94 |
});
|
|
|
95 |
});
|
|
|
96 |
|
|
|
97 |
// State selected -> enable distributor dropdown
|
|
|
98 |
$(document).on('change', '#stateSelect', function () {
|
|
|
99 |
finServiceTemplateId = $(this).val();
|
|
|
100 |
if (finServiceTemplateId) {
|
|
|
101 |
$('#distributorSelect').prop('disabled', false);
|
|
|
102 |
} else {
|
|
|
103 |
$('#distributorSelect').prop('disabled', true).val('');
|
|
|
104 |
$('#loadFormBtn').prop('disabled', true);
|
|
|
105 |
}
|
|
|
106 |
});
|
|
|
107 |
|
|
|
108 |
// Distributor selected -> enable load button
|
|
|
109 |
$(document).on('change', '#distributorSelect', function () {
|
|
|
110 |
var val = $(this).val();
|
|
|
111 |
$('#loadFormBtn').prop('disabled', !val || !finServiceTemplateId);
|
|
|
112 |
});
|
|
|
113 |
|
|
|
114 |
// Load form with error handling
|
|
|
115 |
$(document).on('click', '#loadFormBtn', function () {
|
|
|
116 |
var fofoId = $('#partnerSelect').val();
|
|
|
117 |
var distType = $('#distributorSelect').val();
|
|
|
118 |
if (!fofoId || !finServiceTemplateId || !distType) {
|
|
|
119 |
alert('Please select Partner, Brand, State and Distributor type');
|
|
|
120 |
return;
|
|
|
121 |
}
|
|
|
122 |
var btn = $(this);
|
|
|
123 |
btn.prop('disabled', true).text('Loading...');
|
|
|
124 |
|
|
|
125 |
$.ajax({
|
|
|
126 |
url: context + '/wod-request/form?templateId=' + finServiceTemplateId + '&fofoId=' + fofoId,
|
|
|
127 |
type: 'GET',
|
|
|
128 |
success: function (response) {
|
|
|
129 |
var resp = typeof response === 'string' ? JSON.parse(response) : response;
|
|
|
130 |
finServiceFormData = resp;
|
|
|
131 |
renderFinServiceForm(resp, distType);
|
|
|
132 |
btn.prop('disabled', false).text('Load Form');
|
|
|
133 |
},
|
|
|
134 |
error: function () {
|
|
|
135 |
alert('Failed to load form. Please try again.');
|
|
|
136 |
btn.prop('disabled', false).text('Load Form');
|
|
|
137 |
}
|
|
|
138 |
});
|
|
|
139 |
});
|
|
|
140 |
|
|
|
141 |
function renderFinServiceForm(resp, distType) {
|
|
|
142 |
var columns = resp.columns;
|
|
|
143 |
var prefill = resp.prefillValues;
|
|
|
144 |
var existingDocs = resp.existingDocs || {};
|
|
|
145 |
$('#templateId').val(resp.templateId);
|
|
|
146 |
$('#fofoId').val(resp.fofoId);
|
|
|
147 |
$('#distributorType').val(distType);
|
|
|
148 |
|
|
|
149 |
var distLabel = distType === 'yes' ? 'Working with Distributor' : 'Non-Distributor / New Partner';
|
|
|
150 |
$('#formTitle').text('WOD Request: ' + resp.brand + ' - ' + resp.state +
|
|
|
151 |
' | ' + resp.storeName + ' (' + resp.storeCode + ') | ' + distLabel);
|
|
|
152 |
|
|
|
153 |
var html = '';
|
|
|
154 |
columns.sort(function (a, b) {
|
|
|
155 |
return (a.order || 0) - (b.order || 0);
|
|
|
156 |
});
|
|
|
157 |
|
|
|
158 |
columns.forEach(function (col) {
|
|
|
159 |
var condition = col.condition || 'always';
|
|
|
160 |
if (condition === 'distributor' && distType !== 'yes') return;
|
|
|
161 |
if (condition === 'non_distributor' && distType !== 'no') return;
|
|
|
162 |
|
|
|
163 |
var val = prefill[col.key] || '';
|
|
|
164 |
var required = col.required ? 'required' : '';
|
|
|
165 |
var reqStar = col.required ? ' <span style="color:red;">*</span>' : '';
|
|
|
166 |
|
|
|
167 |
html += '<div class="col-md-4 fin-form-field" style="margin-bottom:12px;">';
|
|
|
168 |
html += '<label>' + col.label + reqStar + '</label>';
|
|
|
169 |
|
|
|
170 |
if (col.type === 'file') {
|
|
|
171 |
var existingDocId = existingDocs[col.key];
|
|
|
172 |
if (existingDocId) {
|
|
|
173 |
html += '<input type="hidden" class="fin-doc-id" data-key="' + col.key + '" value="' + existingDocId + '">';
|
|
|
174 |
html += '<div style="margin-bottom:4px;">';
|
|
|
175 |
html += '<span class="label label-success"><i class="fa fa-check"></i> Already Uploaded</span> ';
|
|
|
176 |
html += '<a href="' + context + '/open-attachment?documentId=' + existingDocId + '" target="_blank" class="btn btn-xs btn-info">View</a>';
|
|
|
177 |
html += '</div>';
|
|
|
178 |
html += '<input type="file" class="form-control fin-doc-upload" data-key="' + col.key + '">';
|
|
|
179 |
html += '<small class="fin-upload-status" data-key="' + col.key + '" style="color:green;">Using existing. Upload new to replace.</small>';
|
|
|
180 |
} else {
|
|
|
181 |
html += '<input type="file" class="form-control fin-doc-upload" data-key="' + col.key + '" ' + required + '>';
|
|
|
182 |
html += '<input type="hidden" class="fin-doc-id" data-key="' + col.key + '">';
|
|
|
183 |
html += '<small class="fin-upload-status" data-key="' + col.key + '"></small>';
|
|
|
184 |
}
|
|
|
185 |
} else if (col.type === 'textarea') {
|
|
|
186 |
html += '<textarea class="form-control fin-field" data-key="' + col.key + '" rows="3" ' + required + '>' + finEscapeHtml(val) + '</textarea>';
|
|
|
187 |
} else {
|
|
|
188 |
html += '<input type="' + (col.type || 'text') + '" class="form-control fin-field" data-key="' + col.key + '" value="' + finEscapeAttr(val) + '" ' + required + '>';
|
|
|
189 |
}
|
|
|
190 |
html += '</div>';
|
|
|
191 |
});
|
|
|
192 |
|
|
|
193 |
$('#formFieldsContainer').html(html);
|
|
|
194 |
$('#dynamicFormContainer').show();
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
// File upload using same pattern as existing codebase (doAjaxUploadRequestHandler)
|
|
|
198 |
$(document).on('change', '.fin-doc-upload', function () {
|
|
|
199 |
var input = this;
|
|
|
200 |
var key = $(input).data('key');
|
|
|
201 |
if (!input.files || !input.files[0]) return;
|
|
|
202 |
|
|
|
203 |
var statusEl = $('small.fin-upload-status[data-key="' + key + '"]');
|
|
|
204 |
statusEl.text('Uploading...').css('color', 'orange');
|
|
|
205 |
|
|
|
206 |
doAjaxUploadRequestHandler(
|
|
|
207 |
context + '/document-upload',
|
|
|
208 |
'POST',
|
|
|
209 |
input.files[0],
|
|
|
210 |
function (response) {
|
|
|
211 |
var docId = null;
|
|
|
212 |
if (response && response.response && response.response.document_id) {
|
|
|
213 |
docId = response.response.document_id;
|
|
|
214 |
} else if (response && response.document_id) {
|
|
|
215 |
docId = response.document_id;
|
|
|
216 |
}
|
|
|
217 |
if (docId) {
|
|
|
218 |
$('input.fin-doc-id[data-key="' + key + '"]').val(docId);
|
|
|
219 |
statusEl.text('Uploaded').css('color', 'green');
|
|
|
220 |
} else {
|
|
|
221 |
var msg = (response && response.response && response.response.message) || 'Unknown error';
|
|
|
222 |
statusEl.text('Upload failed: ' + msg).css('color', 'red');
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
);
|
|
|
226 |
});
|
|
|
227 |
|
|
|
228 |
// Form submit via button click (not form submit event — avoids page navigation)
|
|
|
229 |
$(document).on('click', '#submitBtn', function (e) {
|
|
|
230 |
e.preventDefault();
|
|
|
231 |
e.stopPropagation();
|
|
|
232 |
|
|
|
233 |
// Validate required fields
|
|
|
234 |
var missingFields = [];
|
|
|
235 |
$('#formFieldsContainer .fin-field[required]').each(function () {
|
|
|
236 |
if (!$(this).val() || !$(this).val().trim()) {
|
|
|
237 |
var label = $(this).closest('.fin-form-field').find('label').text().replace(' *', '').trim();
|
|
|
238 |
missingFields.push(label);
|
|
|
239 |
$(this).css('border-color', 'red');
|
|
|
240 |
} else {
|
|
|
241 |
$(this).css('border-color', '');
|
|
|
242 |
}
|
|
|
243 |
});
|
|
|
244 |
|
|
|
245 |
// Validate required file uploads
|
|
|
246 |
$('#formFieldsContainer .fin-doc-upload[required]').each(function () {
|
|
|
247 |
var key = $(this).data('key');
|
|
|
248 |
var docId = $('input.fin-doc-id[data-key="' + key + '"]').val();
|
|
|
249 |
if (!docId) {
|
|
|
250 |
var label = $(this).closest('.fin-form-field').find('label').text().replace(' *', '').trim();
|
|
|
251 |
missingFields.push(label);
|
|
|
252 |
$(this).css('border-color', 'red');
|
|
|
253 |
} else {
|
|
|
254 |
$(this).css('border-color', '');
|
|
|
255 |
}
|
|
|
256 |
});
|
|
|
257 |
|
|
|
258 |
if (missingFields.length > 0) {
|
|
|
259 |
alert('Please fill the following required fields:\n\n- ' + missingFields.join('\n- '));
|
|
|
260 |
return false;
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
var formData = {};
|
|
|
264 |
$('#formFieldsContainer .fin-field').each(function () {
|
|
|
265 |
formData[$(this).data('key')] = $(this).val();
|
|
|
266 |
});
|
|
|
267 |
formData['distributor_type'] = $('#distributorType').val();
|
|
|
268 |
|
|
|
269 |
var docTypes = [];
|
|
|
270 |
var docIds = [];
|
|
|
271 |
$('#formFieldsContainer .fin-doc-id').each(function () {
|
|
|
272 |
if ($(this).val()) {
|
|
|
273 |
docTypes.push($(this).data('key'));
|
|
|
274 |
docIds.push(parseInt($(this).val()));
|
|
|
275 |
}
|
|
|
276 |
});
|
|
|
277 |
|
|
|
278 |
if (!confirm('Are you sure you want to submit this WOD request?')) {
|
|
|
279 |
return false;
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
$('#submitBtn').prop('disabled', true).text('Submitting...');
|
|
|
283 |
|
|
|
284 |
$.ajax({
|
|
|
285 |
url: context + '/wod-request/submit',
|
|
|
286 |
type: 'POST',
|
|
|
287 |
data: {
|
|
|
288 |
templateId: $('#templateId').val(),
|
|
|
289 |
fofoId: $('#fofoId').val(),
|
|
|
290 |
formData: JSON.stringify(formData),
|
|
|
291 |
docTypes: JSON.stringify(docTypes),
|
|
|
292 |
docIds: JSON.stringify(docIds)
|
|
|
293 |
},
|
|
|
294 |
success: function (resp) {
|
|
|
295 |
var result = resp;
|
|
|
296 |
try {
|
|
|
297 |
if (typeof resp === 'string') result = JSON.parse(resp);
|
|
|
298 |
} catch (e) {
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
// response.vm returns the JSON string directly
|
|
|
302 |
var parsed = result;
|
|
|
303 |
if (typeof result === 'string') {
|
|
|
304 |
try {
|
|
|
305 |
parsed = JSON.parse(result);
|
|
|
306 |
} catch (e) {
|
|
|
307 |
}
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
if (parsed && parsed.success) {
|
|
|
311 |
alert('WOD request submitted successfully! Request ID: ' + parsed.requestId);
|
|
|
312 |
doGetAjaxRequestHandler(context + '/wod-request-list', function (response) {
|
|
|
313 |
$('#main-content').html(response);
|
|
|
314 |
});
|
|
|
315 |
} else {
|
|
|
316 |
alert('Submission failed: ' + (parsed.error || 'Unknown error'));
|
|
|
317 |
$('#submitBtn').prop('disabled', false).html('<i class="fa fa-paper-plane"></i> Submit WOD Request');
|
|
|
318 |
}
|
|
|
319 |
},
|
|
|
320 |
error: function (xhr) {
|
|
|
321 |
alert('Submission failed. Server error.');
|
|
|
322 |
$('#submitBtn').prop('disabled', false).html('<i class="fa fa-paper-plane"></i> Submit WOD Request');
|
|
|
323 |
}
|
|
|
324 |
});
|
|
|
325 |
|
|
|
326 |
return false;
|
|
|
327 |
});
|
|
|
328 |
|
|
|
329 |
function finEscapeHtml(str) {
|
|
|
330 |
if (!str) return '';
|
|
|
331 |
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
function finEscapeAttr(str) {
|
|
|
335 |
if (!str) return '';
|
|
|
336 |
return String(str).replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
|
|
|
337 |
}
|