-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.html
More file actions
561 lines (476 loc) · 24.6 KB
/
Index.html
File metadata and controls
561 lines (476 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<style>
body { font-family: Arial, sans-serif; background-color: #f0f2f5; margin: 0; }
.header { background-color: #1C2790; padding: 20px; text-align: center; color: white; }
.header img { width: 800px; margin-bottom: 10px; }
.container { max-width: 700px; margin: 20px auto; background: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border-top: 5px solid #CDAE2C; }
.form-group { margin-bottom: 20px; position: relative; }
.form-section-title { font-size: 1.2em; font-weight: bold; color: #1C2790; border-bottom: 2px solid #eee; padding-bottom: 10px; margin-bottom: 20px; margin-top: 30px; }
label { display: block; margin-bottom: 8px; font-weight: bold; color: #333; }
input[type="text"], input[type="email"], select { width: 100%; padding: 12px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
button { padding: 12px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; font-weight: bold; }
#submitButton { width: 100%; margin-top: 20px; background-color: #1C2790; color: white; }
#submitButton:disabled { background-color: #cccccc; cursor: not-allowed; }
#add-doc-btn { width: 100%; background-color: #28a745; color: white; margin-top: 10px; }
.document-entry { background-color: #f9f9f9; border: 1px solid #e0e0e0; border-radius: 6px; padding: 20px; margin-bottom: 15px; position: relative; }
.remove-btn { position: absolute; top: 10px; right: 10px; background-color: #dc3545; color: white; width: 30px; height: 30px; padding: 0; font-size: 14px; line-height: 30px; text-align: center; }
.ala-container { display: none; margin-top: 15px; background-color: #e8f0fe; padding: 15px; border-radius: 4px; border-left: 4px solid #1C2790; }
.add-leave-btn { background-color: #0067c7; color: white; width: auto; padding: 8px 15px; font-size: 14px; margin-top: 10px; }
.leave-list-table { width: 100%; margin-top: 15px; border-collapse: collapse; font-size: 13px; background: white; }
.leave-list-table th, .leave-list-table td { padding: 8px; border: 1px solid #ddd; text-align: left; }
.leave-list-table th { background-color: #f1f3f4; font-weight: bold; }
.delete-row-btn { color: red; cursor: pointer; font-weight: bold; text-align: center; }
.autocomplete-items {
position: absolute; border: 1px solid #d4d4d4; border-bottom: none; border-top: none; z-index: 99;
top: 100%; left: 0; right: 0; max-height: 200px; overflow-y: auto; background-color: #fff;
}
.autocomplete-items div { padding: 10px; cursor: pointer; background-color: #fff; border-bottom: 1px solid #d4d4d4; }
.autocomplete-items div:hover { background-color: #e9e9e9; }
.slip-content { box-sizing: border-box; padding: 4mm; color: #000; text-align: left; border: 1px solid #000; font-size: 9pt; width: 105mm; }
.slip-header { border-bottom: 1px solid #ccc; padding-bottom: 2mm; margin-bottom: 2mm; text-align: center; }
.slip-body { display: flex; align-items: flex-start; gap: 4mm; }
.slip-left-panel { flex-grow: 1; }
.slip-right-panel { flex-shrink: 0; }
.slip-details-table { width: 100%; font-size: 7pt; border-collapse: collapse; margin-top: 2mm; }
.slip-details-table th { background-color: #eee; text-align: left; font-weight: bold; padding: 2px; }
.slip-details-table td, .slip-details-table th { padding: 1mm; border: 1px solid #ccc; vertical-align: top; }
.multiline-text { white-space: pre-wrap; line-height: 1.3; }
.ref-number-cell { font-size: 12pt; font-weight: bold; margin-bottom: 2mm; display: block; }
@media print {
.header, #formContainer { display: none !important; }
#successContainer { display: flex !important; justify-content: center; padding-top: 5mm; border: none; box-shadow: none; margin: 0; max-width: 100%; }
.slip-content { width: 100%; border: none; }
}
</style>
</head>
<body>
<div class="header">
<img src="https://i.imgur.com/jaEbfAR.png" alt="DAP Logo">
<h2>HRMDD-ComBen Document Submission</h2>
</div>
<div class="container" id="formContainer">
<form id="submissionForm">
<div class="form-group autocomplete">
<label>Liaison / Admin Coordinator Name</label>
<input type="text" id="contactPerson" placeholder="Type name to search..." autocomplete="off">
<div class="autocomplete-items"></div>
</div>
<div class="form-group"><label>Center/Department</label><input type="text" id="centerDept" required></div>
<div class="form-group"><label>Office/Unit</label><input type="text" id="officeUnit"></div>
<div class="form-group"><label>Liaison Email Address</label><input type="email" id="email" required></div>
<div class="form-section-title">Documents in this Transaction</div>
<div id="documents-container"></div>
<button type="button" id="add-doc-btn">+ Add Another Document</button>
<button type="submit" id="submitButton">Submit Transaction</button>
</form>
</div>
<div class="container" id="successContainer" style="display: none;">
<div class="slip-content">
<div class="slip-header"><p class="header-text" style="font-weight:bold;margin:0;">HRMDD-ComBen Document Submission</p></div>
<div class="slip-body">
<div class="slip-left-panel">
<span style="font-size:8pt;">Transaction ID:</span>
<span class="ref-number-cell" id="slipTxId"></span>
<p style="font-size:8pt; margin: 2px 0;"><b>Liaison:</b> <span id="slipContact"></span></p>
<p style="font-size:8pt; margin: 2px 0;"><b>Date:</b> <span id="slipTimestamp"></span></p>
<table class="slip-details-table">
<thead>
<tr><th style="width:25%">Owner</th><th style="width:25%">Type</th><th style="width:30%">Title</th><th style="width:20%">Dates</th></tr>
</thead>
<tbody id="slip-documents-list"></tbody>
</table>
</div>
<div class="slip-right-panel"><div id="qrcode"></div></div>
</div>
</div>
</div>
<datalist id="leaveTypesList">
<option value="Vacation Leave">
<option value="Mandatory/Forced Leave">
<option value="Sick Leave">
<option value="Maternity Leave">
<option value="Paternity Leave">
<option value="Special Privilege Leave">
<option value="Solo Parent Leave">
<option value="Study Leave">
<option value="10-Day VAWC Leave">
<option value="Rehabilitation Privilege">
<option value="Special Leave Benefits for Women">
<option value="Special Emergency (Calamity) Leave">
<option value="Adoption Leave">
<option value="Wellness Leave">
<option value="Terminal Leave">
</datalist>
<script>
const docTypes = <?!= JSON.stringify(docTypes) ?>;
const directory = <?!= JSON.stringify(directory) ?>;
const CALENDAR_DAY_LEAVES = [
"Maternity Leave", "Rehabilitation Privilege", "Study Leave", "Adoption Leave", "Special Leave Benefits for Women"
];
// CONFIG: Annual Limits
const LEAVE_LIMITS = {
"Special Privilege Leave": 3,
"Special Emergency (Calamity) Leave": 5,
"Wellness Leave": 5
};
function createDocEntryHTML() {
let options = `<option value="" disabled selected>Select a type...</option>`;
docTypes.forEach(t => options += `<option value="${t}">${t}</option>`);
return `
<button type="button" class="remove-btn" onclick="removeEntry(this)">X</button>
<div class="form-group autocomplete">
<label>Document Owner / Principal</label>
<input type="text" class="principal-search" placeholder="Type name to search directory..." autocomplete="off">
<input type="hidden" class="principal-email">
<input type="hidden" class="server-leave-stats" value="{}"> <div class="autocomplete-items"></div>
</div>
<div class="form-group">
<label>Document Type</label>
<select class="doc-type-select" required onchange="handleDocTypeChange(this)">${options}</select>
</div>
<div class="ala-container">
<div style="font-weight:bold; margin-bottom:10px; color:#1C2790;">Add Leave Details</div>
<div class="form-group">
<label>Leave Type</label>
<input type="text" class="leave-type-input" list="leaveTypesList" placeholder="Select or type leave type..." onchange="handleLeaveTypeChange(this)">
</div>
<div class="form-group">
<label>Inclusive Dates</label>
<input type="text" class="leave-dates-input" placeholder="Select dates...">
<div style="font-size:11px; color:#666; margin-top:2px;" class="calendar-hint"></div>
</div>
<button type="button" class="add-leave-btn" onclick="addLeaveItem(this)">+ Add to List</button>
<span class="leave-error" style="color:red; font-size:12px; margin-left:10px;"></span>
<table class="leave-list-table" style="display:none;">
<thead><tr><th>Type</th><th>Dates</th><th>Days</th><th></th></tr></thead>
<tbody></tbody>
</table>
<input type="hidden" class="ala-data-json" value="[]">
</div>
<div class="form-group doc-title-group">
<label>Document Title</label>
<input type="text" class="doc-title-input" required>
</div>
`;
}
function addDocumentEntry() {
const container = document.getElementById('documents-container');
const entryDiv = document.createElement('div');
entryDiv.className = 'document-entry';
entryDiv.innerHTML = createDocEntryHTML();
container.appendChild(entryDiv);
const input = entryDiv.querySelector('.principal-search');
attachAutocomplete(input, false);
const dateInput = entryDiv.querySelector('.leave-dates-input');
entryDiv._flatpickrInstance = flatpickr(dateInput, {
mode: "multiple",
dateFormat: "Y-m-d",
disable: [ function(date) { return (date.getDay() === 0 || date.getDay() === 6); } ]
});
}
function removeEntry(btn) {
const container = document.getElementById('documents-container');
if (container.children.length > 1) { btn.parentElement.remove(); }
else { alert("You must include at least one document."); }
}
function handleDocTypeChange(selectElement) {
const entryDiv = selectElement.closest('.document-entry');
const alaContainer = entryDiv.querySelector('.ala-container');
const titleInput = entryDiv.querySelector('.doc-title-input');
if (selectElement.value === 'Application for Leave (ALA)') {
alaContainer.style.display = 'block';
titleInput.readOnly = true;
titleInput.style.backgroundColor = "#e9ecef";
titleInput.placeholder = "Auto-generated from list below...";
titleInput.value = "Application for Leave (See details)";
} else {
alaContainer.style.display = 'none';
titleInput.readOnly = false;
titleInput.style.backgroundColor = "white";
titleInput.placeholder = "Enter document title";
titleInput.value = "";
}
}
function handleLeaveTypeChange(inputElement) {
const entryDiv = inputElement.closest('.document-entry');
const leaveType = inputElement.value;
const hint = entryDiv.querySelector('.calendar-hint');
const fp = entryDiv._flatpickrInstance;
fp.clear();
if (CALENDAR_DAY_LEAVES.includes(leaveType)) {
fp.set("mode", "range");
fp.set("disable", []);
hint.textContent = "Mode: Date Range (Weekends Included)";
} else {
fp.set("mode", "multiple");
fp.set("disable", [ function(date) { return (date.getDay() === 0 || date.getDay() === 6); } ]);
hint.textContent = "Mode: Select Specific Days (Weekends Excluded)";
}
}
// 1. OVERLAP VALIDATION
function checkOverlap(newDateStr, entryDiv) {
let newDates = [];
if (newDateStr.includes(' to ')) {
const parts = newDateStr.split(' to ');
let start = new Date(parts[0]);
let end = new Date(parts[1]);
for (let d = start; d <= end; d.setDate(d.getDate() + 1)) {
newDates.push(new Date(d).toISOString().split('T')[0]);
}
} else {
newDates = newDateStr.split(', ');
}
const jsonVal = entryDiv.querySelector('.ala-data-json').value;
const existingLeaves = JSON.parse(jsonVal || "[]");
for (let leave of existingLeaves) {
let existingDates = [];
if (leave.dates.includes(' to ')) {
const parts = leave.dates.split(' to ');
let start = new Date(parts[0]);
let end = new Date(parts[1]);
for (let d = start; d <= end; d.setDate(d.getDate() + 1)) {
existingDates.push(new Date(d).toISOString().split('T')[0]);
}
} else {
existingDates = leave.dates.split(', ');
}
const overlap = newDates.filter(value => existingDates.includes(value));
if (overlap.length > 0) {
return `Overlap detected on: ${overlap.join(', ')}.`;
}
}
return null;
}
// 2. LIMIT CHECKER
function checkLimits(leaveType, newDays, entryDiv) {
if (!LEAVE_LIMITS[leaveType]) return null; // No limit for this type
const limit = LEAVE_LIMITS[leaveType];
// A. Get Historical Usage (Server)
const serverStats = JSON.parse(entryDiv.querySelector('.server-leave-stats').value || "{}");
const usedHistory = serverStats[leaveType] || 0;
// B. Get Pending Usage (Current List)
let pendingUsage = 0;
const jsonVal = entryDiv.querySelector('.ala-data-json').value;
const existingLeaves = JSON.parse(jsonVal || "[]");
existingLeaves.forEach(l => {
if (l.type === leaveType) pendingUsage += parseInt(l.days);
});
const total = usedHistory + pendingUsage + newDays;
if (total > limit) {
return `Limit Reached: ${leaveType} limit is ${limit} days. You have used ${usedHistory}, plus ${pendingUsage} pending. Cannot add ${newDays} more.`;
}
return null;
}
function addLeaveItem(btn) {
const entryDiv = btn.closest('.document-entry');
const typeInput = entryDiv.querySelector('.leave-type-input');
const datesInput = entryDiv.querySelector('.leave-dates-input');
const errorSpan = entryDiv.querySelector('.leave-error');
const table = entryDiv.querySelector('.leave-list-table');
const tbody = table.querySelector('tbody');
const type = typeInput.value;
const dates = datesInput.value;
if (!type || !dates) {
errorSpan.textContent = "Please select a Type and Dates.";
return;
}
// Calculate Days first for Limit Check
let daysCount = 0;
if (dates.includes(' to ')) {
const range = dates.split(' to ');
if (range.length === 2) {
const start = new Date(range[0]);
const end = new Date(range[1]);
const diffTime = Math.abs(end - start);
daysCount = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + 1;
} else { daysCount = 1; }
} else {
daysCount = dates.split(', ').length;
}
// CHECK 1: LIMITS
const limitError = checkLimits(type, daysCount, entryDiv);
if (limitError) {
errorSpan.textContent = limitError;
return;
}
// CHECK 2: OVERLAPS
const overlapError = checkOverlap(dates, entryDiv);
if (overlapError) {
errorSpan.textContent = overlapError;
return;
}
errorSpan.textContent = "";
const row = document.createElement('tr');
row.innerHTML = `
<td>${type}</td>
<td>${dates}</td>
<td>${daysCount}</td>
<td class="delete-row-btn" onclick="deleteLeaveItem(this)">X</td>
`;
tbody.appendChild(row);
table.style.display = "table";
updateLeaveJson(entryDiv);
typeInput.value = "";
datesInput.value = "";
entryDiv._flatpickrInstance.clear();
}
function deleteLeaveItem(btn) {
const entryDiv = btn.closest('.document-entry');
const row = btn.closest('tr');
row.remove();
const tbody = entryDiv.querySelector('.leave-list-table tbody');
if (tbody.children.length === 0) {
entryDiv.querySelector('.leave-list-table').style.display = "none";
}
updateLeaveJson(entryDiv);
}
function updateLeaveJson(entryDiv) {
const tbody = entryDiv.querySelector('.leave-list-table tbody');
const rows = tbody.querySelectorAll('tr');
const data = [];
let summaryTitle = [];
rows.forEach(row => {
const cells = row.querySelectorAll('td');
const item = {
type: cells[0].innerText,
dates: cells[1].innerText,
days: cells[2].innerText
};
data.push(item);
summaryTitle.push(`${item.type} (${item.days})`);
});
entryDiv.querySelector('.ala-data-json').value = JSON.stringify(data);
const titleInput = entryDiv.querySelector('.doc-title-input');
if (data.length > 0) {
titleInput.value = "ALA: " + summaryTitle.join(", ");
} else {
titleInput.value = "Application for Leave";
}
}
function attachAutocomplete(inp, isLiaison) {
const listContainer = inp.parentNode.querySelector('.autocomplete-items');
const targetEmailField = isLiaison
? document.getElementById('email')
: inp.parentNode.querySelector('.principal-email');
const entryDiv = isLiaison ? null : inp.closest('.document-entry');
inp.addEventListener("input", function(e) {
const val = this.value;
listContainer.innerHTML = '';
if (!val) return false;
const matches = directory.filter(d => d.name.toLowerCase().includes(val.toLowerCase()));
matches.slice(0, 8).forEach(match => {
const item = document.createElement("div");
const regex = new RegExp(`(${val})`, "gi");
const highlightedName = match.name.replace(regex, "<strong>$1</strong>");
item.innerHTML = `${highlightedName} <span style='font-size:11px;color:#888;'>(${match.email})</span>`;
item.innerHTML += `<input type='hidden' value='${match.name}' data-email='${match.email}'>`;
item.addEventListener("click", function(e) {
const selectedName = this.getElementsByTagName("input")[0].value;
const selectedEmail = this.getElementsByTagName("input")[0].getAttribute('data-email');
inp.value = selectedName;
if (targetEmailField) { targetEmailField.value = selectedEmail; }
// TRIGGER STATS FETCH FOR VALIDATION (If Owner Field)
if (entryDiv) {
google.script.run.withSuccessHandler(function(stats) {
entryDiv.querySelector('.server-leave-stats').value = JSON.stringify(stats);
console.log("Loaded Stats for " + selectedName, stats);
}).getLeaveStats(selectedName);
}
listContainer.innerHTML = '';
});
listContainer.appendChild(item);
});
});
document.addEventListener("click", function (e) { if (e.target !== inp) { listContainer.innerHTML = ''; } });
}
document.addEventListener('DOMContentLoaded', function() {
attachAutocomplete(document.getElementById('contactPerson'), true);
addDocumentEntry();
document.getElementById('add-doc-btn').addEventListener('click', addDocumentEntry);
document.getElementById('submissionForm').addEventListener('submit', function(event) {
event.preventDefault();
const submitButton = document.getElementById('submitButton');
const submissionObject = {
transactionDetails: {
contactPerson: document.getElementById('contactPerson').value,
centerDept: document.getElementById('centerDept').value,
officeUnit: document.getElementById('officeUnit').value,
email: document.getElementById('email').value
},
documents: []
};
const documentEntries = document.querySelectorAll('.document-entry');
let hasError = false;
documentEntries.forEach(entry => {
let pName = entry.querySelector('.principal-search').value;
let pEmail = entry.querySelector('.principal-email').value;
let docType = entry.querySelector('.doc-type-select').value;
let title = entry.querySelector('.doc-title-input').value;
let alaData = "[]";
if (docType === 'Application for Leave (ALA)') {
alaData = entry.querySelector('.ala-data-json').value;
if (alaData === "[]") {
alert("Please add at least one leave type for the Application for Leave.");
hasError = true;
}
}
submissionObject.documents.push({
docType: docType,
docTitle: title,
alaData: alaData,
principalName: pName,
principalEmail: pEmail
});
});
if(hasError) return;
submitButton.disabled = true;
submitButton.textContent = 'Submitting...';
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).processForm(submissionObject);
});
function onSuccess(returnData) {
const txId = returnData.transactionId;
const fullLink = `${returnData.appUrl}?action=update&id=${txId}`;
const details = returnData.details;
document.getElementById('slipTxId').textContent = txId;
document.getElementById('slipContact').textContent = details.ContactPerson;
document.getElementById('slipTimestamp').textContent = details.Timestamp;
const tableBody = document.getElementById('slip-documents-list');
tableBody.innerHTML = '';
details.documents.forEach(doc => {
let titleDisplay = doc.title;
tableBody.innerHTML += `
<tr>
<td style="font-weight:bold;">${doc.owner}</td>
<td>${doc.type}</td>
<td class="multiline-text">${doc.title}</td>
<td class="multiline-text">${doc.datesSummary || doc.dates}</td>
</tr>`;
});
const qrcodeContainer = document.getElementById('qrcode');
qrcodeContainer.innerHTML = "";
// --- QR CODE FIX ---
new QRCode(qrcodeContainer, {
text: fullLink,
width: 150,
height: 150,
correctLevel: QRCode.CorrectLevel.L
});
document.getElementById('formContainer').style.display = 'none';
document.getElementById('successContainer').style.display = 'flex';
setTimeout(() => { window.print(); }, 1000);
}
function onFailure(error) {
alert('Error: ' + error.message);
document.getElementById('submitButton').disabled = false;
document.getElementById('submitButton').textContent = 'Submit Transaction';
}
});
</script>
</body>
</html>