File size: 31,191 Bytes
6ea020b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{% extends "base.html" %}

{% block title %}HR - Employees{% endblock %}
{% block page_title %}HR & Staff{% endblock %}

{% block content %}
<div class="p-6">
    <!-- Header -->
    <div class="flex justify-between items-center mb-6">
        <div>
            <h2 class="text-2xl font-bold text-slate-800">Employees</h2>
            <p class="text-sm text-slate-500">Manage lab staff and their details</p>
        </div>
        <button onclick="openEmployeeModal()" class="bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors flex items-center shadow-sm">
            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
            Add Employee
        </button>
    </div>

    <!-- Alert Messages -->
    {% if message_success %}
    <div class="mb-6 p-4 rounded-lg bg-emerald-50 text-emerald-800 border border-emerald-200 flex items-center shadow-sm">
        <svg class="w-5 h-5 mr-3 text-emerald-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
        {{ message_success }}
    </div>
    {% endif %}
    {% if message_error %}
    <div class="mb-6 p-4 rounded-lg bg-red-50 text-red-800 border border-red-200 flex items-center shadow-sm">
        <svg class="w-5 h-5 mr-3 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
        {{ message_error }}
    </div>
    {% endif %}

    <!-- Employee Cards Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
        {% for emp in employees %}
        <div class="bg-white rounded-2xl border border-slate-200 shadow-sm overflow-hidden flex flex-col transition-shadow hover:shadow-md">
            <!-- Card Header -->
            <div class="p-6 flex flex-col items-center border-b border-slate-100 bg-slate-50/50">
                <div class="w-24 h-24 rounded-full overflow-hidden mb-4 border-4 border-white shadow-sm bg-slate-200 flex items-center justify-center">
                    {% if emp.photo_path %}
                        <img src="{{ emp.photo_path }}" alt="{{ emp.full_name }}" class="w-full h-full object-cover">
                    {% else %}
                        <span class="text-3xl text-slate-400 font-bold">{{ emp.full_name[0] }}</span>
                    {% endif %}
                </div>
                <h3 class="text-xl font-bold text-slate-800 text-center">{{ emp.full_name }}</h3>
                <p class="text-sm text-slate-500 font-medium mt-1">{% if emp.username %}@{{ emp.username }}{% else %}Employee{% endif %}</p>
            </div>

            <!-- Card Body -->
            <div class="p-5 flex-1 flex flex-col">
                <div class="space-y-3 text-sm flex-1">
                    <div class="flex justify-between items-center py-1 border-b border-slate-50">
                        <span class="text-slate-500">Phone</span>
                        <span class="text-slate-800 font-medium">{{ emp.phone or '-' }}</span>
                    </div>
                    <div class="flex justify-between items-center py-1 border-b border-slate-50">
                        <span class="text-slate-500">Working Life</span>
                        <span class="text-slate-800 font-medium">
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-semibold bg-indigo-100 text-indigo-800">
                                {{ emp.working_life_months }} Months
                            </span>
                        </span>
                    </div>
                    <div class="flex justify-between items-center py-1 border-b border-slate-50">
                        <span class="text-slate-500">Start Date</span>
                        <span class="text-slate-800 font-medium">{{ emp.start_date.strftime('%d %b, %Y') }}</span>
                    </div>
                </div>

                <!-- Actions -->
                <div class="mt-6 flex gap-2 justify-center">
                    <button onclick='viewEmployee(employeesData[{{ emp.id }}])' class="p-2 text-indigo-600 bg-indigo-50 hover:bg-indigo-100 rounded-lg transition-colors" title="View Details">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
                    </button>
                    <button onclick='openAttachmentsModal({{ emp.id }}, "{{ emp.full_name }}")' class="p-2 text-blue-600 bg-blue-50 hover:bg-blue-100 rounded-lg transition-colors" title="Attachments">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"></path></svg>
                    </button>
                    <button onclick='editEmployee(employeesData[{{ emp.id }}])' class="p-2 text-amber-600 bg-amber-50 hover:bg-amber-100 rounded-lg transition-colors" title="Edit">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path></svg>
                    </button>
                    <form action="/api/hr/employees/{{ emp.id }}/delete" method="POST" class="inline" onsubmit="return confirm('Are you sure you want to delete {{ emp.full_name }}?');">
                        <button type="submit" class="p-2 text-red-600 bg-red-50 hover:bg-red-100 rounded-lg transition-colors" title="Delete">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                        </button>
                    </form>
                </div>
            </div>
        </div>
        {% else %}
        <div class="col-span-full py-12 flex flex-col items-center justify-center text-slate-400 bg-white rounded-2xl border border-slate-200 border-dashed">
            <svg class="w-16 h-16 mb-4 text-slate-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
            <p class="text-lg font-medium">No employees found.</p>
            <button onclick="openEmployeeModal()" class="mt-4 text-indigo-600 font-medium hover:text-indigo-700">Add an employee</button>
        </div>
        {% endfor %}
    </div>
</div>

<!-- Add/Edit Employee Modal -->
<div id="employeeModal" class="fixed inset-0 bg-slate-900/50 backdrop-blur-sm z-50 hidden flex items-center justify-center">
    <div class="bg-white rounded-xl shadow-xl w-full max-w-3xl max-h-[90vh] flex flex-col m-4 animate-[modalSlideIn_0.2s_ease-out]">
        <div class="flex justify-between items-center p-6 border-b border-slate-100">
            <h3 id="employeeModalTitle" class="text-xl font-bold text-slate-800">Add Employee</h3>
            <button onclick="closeEmployeeModal()" class="text-slate-400 hover:text-slate-600 transition-colors">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
            </button>
        </div>
        
        <div class="p-6 overflow-y-auto custom-scrollbar">
            <form id="employeeForm" method="POST" action="/api/hr/employees" enctype="multipart/form-data" class="space-y-6">
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <!-- Column 1 -->
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-1">Full Name <span class="text-red-500">*</span></label>
                            <input type="text" name="full_name" id="full_name" required class="w-full border border-slate-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                        </div>
                        
                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-1">Username</label>
                            <input type="text" name="username" id="username" class="w-full border border-slate-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                        </div>
                        
                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">Age</label>
                                <input type="number" name="age" id="age" min="16" max="100" class="w-full border border-slate-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-slate-700 mb-1">Phone</label>
                                <input type="text" name="phone" id="phone" class="w-full border border-slate-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                            </div>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-1">Address</label>
                            <input type="text" name="address" id="address" class="w-full border border-slate-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-1">Photo Profile</label>
                            <input type="file" name="photo" id="photo" accept="image/*" class="w-full border border-slate-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100">
                            <p class="text-xs text-slate-500 mt-1">Upload a square image for best results.</p>
                        </div>
                    </div>

                    <!-- Column 2 -->
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-1">Start Date <span class="text-red-500">*</span></label>
                            <input type="date" name="start_date" id="start_date" required class="w-full border border-slate-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-slate-700 mb-1">Salary</label>
                            <div class="relative">
                                <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                    <span class="text-slate-500">$</span>
                                </div>
                                <input type="number" step="0.01" name="salary" id="salary" class="w-full border border-slate-300 rounded-lg pl-8 pr-4 py-2 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                            </div>
                        </div>

                        <div class="bg-slate-50 p-4 rounded-lg border border-slate-200 mt-6">
                            <h4 class="text-sm font-bold text-slate-800 mb-3 flex items-center">
                                <svg class="w-4 h-4 mr-1 text-slate-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                                Working Time
                            </h4>
                            
                            <div class="mb-3">
                                <label class="block text-sm font-medium text-slate-700 mb-1">Working Days</label>
                                <input type="text" name="working_days" id="working_days" placeholder="e.g. Mon, Tue, Wed, Thu, Fri" class="w-full border border-slate-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                            </div>
                            
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-medium text-slate-700 mb-1">From</label>
                                    <input type="time" name="working_hours_start" id="working_hours_start" class="w-full border border-slate-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-slate-700 mb-1">To</label>
                                    <input type="time" name="working_hours_end" id="working_hours_end" class="w-full border border-slate-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="pt-4 border-t border-slate-100 flex justify-end gap-3 mt-8">
                    <button type="button" onclick="closeEmployeeModal()" class="px-5 py-2 border border-slate-300 text-slate-700 rounded-lg hover:bg-slate-50 font-medium transition-colors">Cancel</button>
                    <button type="submit" id="saveBtn" class="px-5 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 font-medium shadow-sm transition-colors">Save Employee</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- View Employee Modal (Full Details) -->
<div id="viewModal" class="fixed inset-0 bg-slate-900/50 backdrop-blur-sm z-50 hidden flex items-center justify-center">
    <div class="bg-white rounded-xl shadow-xl w-full max-w-2xl max-h-[90vh] flex flex-col m-4 animate-[modalSlideIn_0.2s_ease-out]">
        <div class="flex justify-between items-center p-6 border-b border-slate-100">
            <h3 class="text-xl font-bold text-slate-800">Employee Details</h3>
            <button onclick="closeViewModal()" class="text-slate-400 hover:text-slate-600 transition-colors">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
            </button>
        </div>
        <div class="p-0 overflow-y-auto custom-scrollbar flex-1">
            <div class="bg-slate-50 p-8 flex flex-col items-center border-b border-slate-200">
                <div class="w-28 h-28 rounded-full overflow-hidden mb-4 border-4 border-white shadow-md bg-slate-200 flex items-center justify-center" id="view_photo_container">
                    <!-- injected dynamically -->
                </div>
                <h2 id="view_name" class="text-2xl font-bold text-slate-800"></h2>
                <p id="view_username" class="text-indigo-600 font-medium"></p>
            </div>
            
            <div class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-6">
                    <div>
                        <p class="text-sm text-slate-500 mb-1">Age</p>
                        <p class="font-medium text-slate-800" id="view_age"></p>
                    </div>
                    <div>
                        <p class="text-sm text-slate-500 mb-1">Phone</p>
                        <p class="font-medium text-slate-800" id="view_phone"></p>
                    </div>
                    <div class="md:col-span-2">
                        <p class="text-sm text-slate-500 mb-1">Address</p>
                        <p class="font-medium text-slate-800" id="view_address"></p>
                    </div>
                    <hr class="md:col-span-2 border-slate-100">
                    <div>
                        <p class="text-sm text-slate-500 mb-1">Start Date</p>
                        <p class="font-medium text-slate-800" id="view_start_date"></p>
                    </div>
                    <div>
                        <p class="text-sm text-slate-500 mb-1">Working Life</p>
                        <p class="font-bold text-indigo-600 bg-indigo-50 px-2 py-1 rounded inline-block" id="view_working_life"></p>
                    </div>
                    <div>
                        <p class="text-sm text-slate-500 mb-1">Working Days</p>
                        <p class="font-medium text-slate-800" id="view_working_days"></p>
                    </div>
                    <div>
                        <p class="text-sm text-slate-500 mb-1">Working Hours</p>
                        <p class="font-medium text-slate-800" id="view_working_hours"></p>
                    </div>
                    <div class="md:col-span-2">
                        <p class="text-sm text-slate-500 mb-1">Salary</p>
                        <p class="font-bold text-emerald-600 text-lg" id="view_salary"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="p-6 border-t border-slate-100 flex justify-end">
            <button onclick="closeViewModal()" class="px-6 py-2 bg-slate-800 text-white rounded-lg hover:bg-slate-900 font-medium transition-colors">Close</button>
        </div>
    </div>
</div>

<!-- Attachments Modal -->
<div id="attachmentsModal" class="fixed inset-0 bg-slate-900/50 backdrop-blur-sm z-50 hidden flex items-center justify-center">
    <div class="bg-white rounded-xl shadow-xl w-full max-w-2xl max-h-[90vh] flex flex-col m-4 animate-[modalSlideIn_0.2s_ease-out]">
        <div class="flex justify-between items-center p-6 border-b border-slate-100">
            <h3 class="text-xl font-bold text-slate-800" id="attachmentsModalTitle">Employee Attachments</h3>
            <button onclick="closeAttachmentsModal()" class="text-slate-400 hover:text-slate-600 transition-colors">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
            </button>
        </div>
        
        <div class="p-6 overflow-y-auto custom-scrollbar flex-1 bg-slate-50">
            <!-- Upload area -->
            <div class="bg-white p-5 rounded-lg border border-slate-200 shadow-sm mb-6">
                <h4 class="text-sm font-bold text-slate-800 mb-3">Upload New Attachment</h4>
                <div class="flex items-end gap-3">
                    <div class="flex-1">
                        <input type="file" id="attachmentFile" class="w-full border border-slate-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 file:mr-4 file:py-1 file:px-3 file:rounded file:border-0 file:text-sm file:font-semibold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100">
                    </div>
                    <button onclick="uploadAttachment()" id="uploadBtn" class="bg-indigo-600 hover:bg-indigo-700 text-white px-5 py-2 rounded-lg text-sm font-medium transition-colors shadow-sm flex items-center">
                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path></svg>
                        Upload
                    </button>
                </div>
            </div>

            <!-- List -->
            <h4 class="text-sm font-bold text-slate-800 mb-3">Saved Attachments</h4>
            <div id="attachmentsList" class="space-y-3">
                <!-- Injected via JS -->
                <div class="text-center text-slate-500 py-4 text-sm">Loading...</div>
            </div>
        </div>
    </div>
</div>

<script>
    const employeesData = {{ employees_json | tojson | safe }};
    let currentEmployeeId = null;

    function openEmployeeModal() {
        document.getElementById('employeeModalTitle').textContent = 'Add Employee';
        document.getElementById('employeeForm').action = '/api/hr/employees';
        document.getElementById('employeeForm').reset();
        document.getElementById('saveBtn').textContent = 'Save Employee';
        document.getElementById('employeeModal').classList.remove('hidden');
    }

    function editEmployee(emp) {
        document.getElementById('employeeModalTitle').textContent = 'Edit Employee';
        document.getElementById('employeeForm').action = `/api/hr/employees/${emp.id}/update`;
        document.getElementById('saveBtn').textContent = 'Update Employee';
        
        document.getElementById('full_name').value = emp.full_name || '';
        document.getElementById('username').value = emp.username || '';
        document.getElementById('age').value = emp.age || '';
        document.getElementById('phone').value = emp.phone || '';
        document.getElementById('address').value = emp.address || '';
        
        if (emp.start_date) {
            document.getElementById('start_date').value = emp.start_date.split('T')[0];
        }
        
        document.getElementById('salary').value = emp.salary || '';
        document.getElementById('working_days').value = emp.working_days || '';
        document.getElementById('working_hours_start').value = emp.working_hours_start || '';
        document.getElementById('working_hours_end').value = emp.working_hours_end || '';
        
        document.getElementById('employeeModal').classList.remove('hidden');
    }

    function closeEmployeeModal() {
        document.getElementById('employeeModal').classList.add('hidden');
    }

    function viewEmployee(emp) {
        document.getElementById('view_name').textContent = emp.full_name;
        document.getElementById('view_username').textContent = emp.username ? `@${emp.username}` : '';
        
        const photoContainer = document.getElementById('view_photo_container');
        if (emp.photo_path) {
            photoContainer.innerHTML = `<img src="${emp.photo_path}" class="w-full h-full object-cover">`;
        } else {
            photoContainer.innerHTML = `<span class="text-4xl text-slate-400 font-bold">${emp.full_name[0]}</span>`;
        }

        document.getElementById('view_age').textContent = emp.age ? `${emp.age} years` : '-';
        document.getElementById('view_phone').textContent = emp.phone || '-';
        document.getElementById('view_address').textContent = emp.address || '-';
        
        if (emp.start_date) {
            const sd = new Date(emp.start_date);
            document.getElementById('view_start_date').textContent = sd.toLocaleDateString('en-GB', {day: 'numeric', month: 'short', year: 'numeric'});
        } else {
            document.getElementById('view_start_date').textContent = '-';
        }

        document.getElementById('view_working_life').textContent = `${emp.working_life_months || 0} Months`;
        document.getElementById('view_working_days').textContent = emp.working_days || '-';
        
        let hrs = '-';
        if (emp.working_hours_start && emp.working_hours_end) {
            hrs = `${emp.working_hours_start} to ${emp.working_hours_end}`;
        } else if (emp.working_hours_start) {
            hrs = `From ${emp.working_hours_start}`;
        }
        document.getElementById('view_working_hours').textContent = hrs;
        
        document.getElementById('view_salary').textContent = emp.salary ? `$${emp.salary.toLocaleString()}` : '-';

        document.getElementById('viewModal').classList.remove('hidden');
    }

    function closeViewModal() {
        document.getElementById('viewModal').classList.add('hidden');
    }

    // --- ATTACHMENTS ---
    function openAttachmentsModal(employeeId, employeeName) {
        currentEmployeeId = employeeId;
        document.getElementById('attachmentsModalTitle').textContent = `Attachments: ${employeeName}`;
        document.getElementById('attachmentsModal').classList.remove('hidden');
        document.getElementById('attachmentFile').value = '';
        fetchAttachments();
    }

    function closeAttachmentsModal() {
        document.getElementById('attachmentsModal').classList.add('hidden');
        currentEmployeeId = null;
    }

    async function fetchAttachments() {
        if (!currentEmployeeId) return;
        const listEl = document.getElementById('attachmentsList');
        listEl.innerHTML = '<div class="text-center text-slate-500 py-4 text-sm">Loading...</div>';
        
        try {
            const res = await fetch(`/api/hr/employees/${currentEmployeeId}/attachments`);
            const data = await res.json();
            
            if (!data.attachments || data.attachments.length === 0) {
                listEl.innerHTML = `
                    <div class="bg-white p-6 rounded-lg border border-slate-200 border-dashed text-center text-slate-500 text-sm">
                        No attachments found for this employee.
                    </div>`;
                return;
            }

            let html = '';
            data.attachments.forEach(att => {
                const date = new Date(att.uploaded_at).toLocaleDateString('en-GB', {day: 'numeric', month: 'short', year: 'numeric'});
                html += `
                <div class="bg-white p-4 rounded-lg border border-slate-200 flex justify-between items-center hover:shadow-sm transition-shadow">
                    <div class="flex items-center">
                        <div class="bg-indigo-100 text-indigo-600 p-2 rounded-lg mr-4">
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"></path></svg>
                        </div>
                        <div>
                            <p class="font-medium text-slate-800 text-sm truncate max-w-xs" title="${att.file_name}">${att.file_name}</p>
                            <p class="text-xs text-slate-500 mt-0.5">Uploaded on ${date}</p>
                        </div>
                    </div>
                    <div class="flex gap-2">
                        <a href="${att.file_path}" target="_blank" class="p-2 text-indigo-600 bg-indigo-50 hover:bg-indigo-100 rounded-md transition-colors" title="View/Download">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
                        </a>
                        <button onclick="deleteAttachment(${att.id})" class="p-2 text-red-600 bg-red-50 hover:bg-red-100 rounded-md transition-colors" title="Delete">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                        </button>
                    </div>
                </div>`;
            });
            listEl.innerHTML = html;

        } catch (e) {
            listEl.innerHTML = `<div class="text-red-500 text-sm text-center py-4">Failed to load attachments.</div>`;
        }
    }

    async function uploadAttachment() {
        const fileInput = document.getElementById('attachmentFile');
        if (!fileInput.files || fileInput.files.length === 0) {
            alert('Please select a file first.');
            return;
        }

        const formData = new FormData();
        formData.append('file', fileInput.files[0]);

        const btn = document.getElementById('uploadBtn');
        const ogText = btn.innerHTML;
        btn.innerHTML = 'Uploading...';
        btn.disabled = true;

        try {
            const res = await fetch(`/api/hr/employees/${currentEmployeeId}/attachments`, {
                method: 'POST',
                body: formData
            });
            const data = await res.json();
            
            if (data.success) {
                fileInput.value = '';
                fetchAttachments();
            } else {
                alert('Upload failed: ' + data.error);
            }
        } catch (e) {
            alert('Upload failed.');
        } finally {
            btn.innerHTML = ogText;
            btn.disabled = false;
        }
    }

    async function deleteAttachment(id) {
        if (!confirm('Delete this attachment?')) return;
        
        try {
            const res = await fetch(`/api/hr/employees/attachments/${id}`, { method: 'DELETE' });
            const data = await res.json();
            if (data.success) {
                fetchAttachments();
            } else {
                alert('Delete failed: ' + data.error);
            }
        } catch (e) {
            alert('Delete failed.');
        }
    }
</script>
{% endblock %}