worker-service 0.2.0

Worker Service - A worker administration microservice that interoperates with the worker-matcher crate
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
{% extends "layout.html.tera" %}

{% block title %}Edit {{ record.label }} — {{ entity_plural | capitalize }} — {{ app_display }}{% endblock title %}

{% block content %}
<nav class="breadcrumb-nav" aria-label="Breadcrumb">
  <ol class="breadcrumb-list" aria-label="Breadcrumb trail">
    <li class="breadcrumb-list-item"><a href="/">Home</a></li>
    <li class="breadcrumb-list-item"><a href="/{{ entity_plural }}">{{ entity_plural | capitalize }}</a></li>
    <li class="breadcrumb-list-item"><a href="/{{ entity_plural }}/{{ record.id }}">{{ record.label }}</a></li>
    <li class="breadcrumb-list-item" aria-current="page">Edit</li>
  </ol>
</nav>

<section aria-label="Edit {{ entity_singular }}">
  <h2>Edit {{ record.label }}</h2>

  {% if errors and errors | length > 0 %}
  <div class="error-summary"
       role="alert"
       aria-labelledby="error-summary-title"
       tabindex="-1">
    <h2 id="error-summary-title">There is a problem</h2>
    <ul>
      {% for e in errors %}
        <li><a href="#{{ e.field }}">{{ e.message }}</a></li>
      {% endfor %}
    </ul>
  </div>
  {% endif %}

  <form class="form"
        aria-label="Edit {{ entity_singular }} form"
        hx-put="/api/{{ entity_plural }}/{{ record.id }}"
        hx-target="body"
        hx-push-url="/{{ entity_plural }}/{{ record.id }}"
        novalidate>
    <div class="field" aria-label="Label field" data-required>
      <label class="label" for="label">Label</label>
      <button class="hover-card-trigger"
              type="button"
              aria-label="Help: Label"
              aria-describedby="label-help-card">?</button>
      <div class="hover-card" id="label-help-card" role="tooltip" aria-label="Label help">
        Required. The primary display name used in lists, search results, breadcrumbs, and audit-log summaries. Keep it short (under 80 characters) and human-readable.
      </div>
      <span class="hint" id="label-hint">A short, human-readable label.</span>
      {% if field_errors and field_errors.label %}
        <span class="error-message" id="label-error" role="alert">{{ field_errors.label }}</span>
      {% endif %}
      <input class="text-input"
             type="text"
             id="label"
             name="label"
             value="{{ record.label | default(value='') }}"
             aria-label="Label"
             aria-describedby="label-hint{% if field_errors and field_errors.label %} label-error{% endif %}"
             {% if field_errors and field_errors.label %}aria-invalid="true" aria-errormessage="label-error"{% endif %}
             required>
    </div>

    <div class="field" aria-label="Subtitle field">
      <label class="label" for="subtitle">Subtitle</label>
      <button class="hover-card-trigger"
              type="button"
              aria-label="Help: Subtitle"
              aria-describedby="subtitle-help-card">?</button>
      <div class="hover-card" id="subtitle-help-card" role="tooltip" aria-label="Subtitle help">
        Optional secondary text shown beneath the label in the index table. Use it for disambiguation (e.g. department, location, or year of birth).
      </div>
      <span class="hint" id="subtitle-hint">Optional description shown alongside the label.</span>
      {% if field_errors and field_errors.subtitle %}
        <span class="error-message" id="subtitle-error" role="alert">{{ field_errors.subtitle }}</span>
      {% endif %}
      <input class="text-input"
             type="text"
             id="subtitle"
             name="subtitle"
             value="{{ record.subtitle | default(value='') }}"
             aria-label="Subtitle"
             aria-describedby="subtitle-hint{% if field_errors and field_errors.subtitle %} subtitle-error{% endif %}"
             {% if field_errors and field_errors.subtitle %}aria-invalid="true" aria-errormessage="subtitle-error"{% endif %}>
    </div>

    {% if healthcare %}
    <fieldset class="fieldset" aria-label="Healthcare identifiers">
      <legend>Healthcare identifiers</legend>

      <div class="field" aria-label="UK NHS number field">
        <label class="label" for="nhs-number">UK NHS number</label>
        <span class="hint" id="nhs-number-hint">10 digits, optionally formatted as 123 456 7890.</span>
        {% if field_errors and field_errors.nhs_number %}
          <span class="error-message" id="nhs-number-error" role="alert">{{ field_errors.nhs_number }}</span>
        {% endif %}
        <input class="united-kingdom-national-health-service-number-input"
               type="text"
               id="nhs-number"
               name="nhs_number"
               value="{{ healthcare.nhs_number | default(value='') }}"
               inputmode="numeric"
               pattern="[0-9 ]*"
               maxlength="12"
               aria-label="UK NHS number"
               aria-describedby="nhs-number-hint{% if field_errors and field_errors.nhs_number %} nhs-number-error{% endif %}"
               {% if field_errors and field_errors.nhs_number %}aria-invalid="true" aria-errormessage="nhs-number-error"{% endif %}>
      </div>

      <div class="field" aria-label="US Social Security number field">
        <label class="label" for="ssn">US Social Security number</label>
        <span class="hint" id="ssn-hint">9 digits, formatted as 123-45-6789.</span>
        {% if field_errors and field_errors.ssn %}
          <span class="error-message" id="ssn-error" role="alert">{{ field_errors.ssn }}</span>
        {% endif %}
        <input class="united-states-social-security-number-input"
               type="text"
               id="ssn"
               name="ssn"
               value="{{ healthcare.ssn | default(value='') }}"
               inputmode="numeric"
               pattern="[0-9-]*"
               maxlength="11"
               autocomplete="off"
               aria-label="US Social Security number"
               aria-describedby="ssn-hint{% if field_errors and field_errors.ssn %} ssn-error{% endif %}"
               {% if field_errors and field_errors.ssn %}aria-invalid="true" aria-errormessage="ssn-error"{% endif %}>
      </div>
    </fieldset>
    {% endif %}

    <fieldset class="fieldset" aria-label="Identifiers">
      <legend>Identifiers</legend>
      <span class="hint" id="identifiers-hint">Add or remove external identifiers such as MRN, SSN, NPI, PPN.</span>

      <div class="tag-input"
           aria-label="Identifier tag input"
           x-data='{
             items: {{ identifiers | json_encode | safe }},
             draftType: "MRN",
             draftValue: "",
             add() {
               var v = this.draftValue.trim();
               if (!v) return;
               this.items.push({ type: this.draftType, value: v });
               this.draftValue = "";
               if (window.lily &amp;&amp; window.lily.toast) {
                 window.lily.toast(this.draftType + " added", "info");
               }
             },
             remove(i) {
               var removed = this.items.splice(i, 1)[0];
               if (window.lily &amp;&amp; window.lily.toast &amp;&amp; removed) {
                 window.lily.toast(removed.type + " removed", "info");
               }
             }
           }'>
        <div class="tag-group" aria-label="Current identifiers">
          <template x-for="(item, i) in items" :key="i">
            <span class="tag" x-bind:aria-label="'Identifier: ' + item.type + ' ' + item.value">
              <strong x-text="item.type"></strong>
              <span x-text="item.value"></span>
              <button type="button"
                      x-bind:aria-label="'Remove ' + item.type + ' ' + item.value"
                      @click="remove(i)">&times;</button>
              <input type="hidden"
                     x-bind:name="'identifiers[' + i + '][type]'"
                     x-bind:value="item.type">
              <input type="hidden"
                     x-bind:name="'identifiers[' + i + '][value]'"
                     x-bind:value="item.value">
            </span>
          </template>
          <span class="hint"
                aria-label="Empty identifier list"
                x-show="items.length === 0">No identifiers yet.</span>
        </div>

        <div class="action-group" aria-label="Add identifier">
          <label class="screen-reader-span" for="identifier-type">Identifier type</label>
          <select class="select"
                  id="identifier-type"
                  aria-label="Identifier type"
                  x-model="draftType">
            <option class="option" value="MRN">MRN</option>
            <option class="option" value="SSN">SSN</option>
            <option class="option" value="NPI">NPI</option>
            <option class="option" value="PPN">PPN</option>
            <option class="option" value="DL">Driver's licence</option>
            <option class="option" value="TAX">Tax ID</option>
            <option class="option" value="OTHER">Other</option>
          </select>
          <label class="screen-reader-span" for="identifier-value">Identifier value</label>
          <input class="text-input"
                 type="text"
                 id="identifier-value"
                 aria-label="Identifier value"
                 placeholder="Enter value"
                 x-model="draftValue"
                 @keydown.enter.prevent="add()">
          <button class="button"
                  type="button"
                  aria-label="Add identifier"
                  @click="add()">Add</button>
        </div>
      </div>
    </fieldset>

    <fieldset class="fieldset" aria-label="Addresses">
      <legend>Addresses</legend>
      <span class="hint" id="addresses-hint">Add or remove postal addresses. At least one of city, postal code, or country is required per address.</span>

      <div class="address-input"
           aria-label="Address list editor"
           x-data='{
             items: {{ addresses | json_encode | safe }},
             draft: { line1: "", line2: "", city: "", state: "", postal_code: "", country: "" },
             add() {
               var hasContent = this.draft.city || this.draft.postal_code || this.draft.country || this.draft.line1;
               if (!hasContent) return;
               this.items.push(Object.assign({}, this.draft));
               this.draft = { line1: "", line2: "", city: "", state: "", postal_code: "", country: "" };
               if (window.lily &amp;&amp; window.lily.toast) {
                 window.lily.toast("Address added", "info");
               }
             },
             remove(i) {
               this.items.splice(i, 1);
               if (window.lily &amp;&amp; window.lily.toast) {
                 window.lily.toast("Address removed", "info");
               }
             },
             summary(a) {
               return [a.line1, a.city, a.postal_code, a.country].filter(Boolean).join(", ");
             }
           }'>
        <div class="tag-group" aria-label="Current addresses">
          <template x-for="(item, i) in items" :key="i">
            <span class="tag" x-bind:aria-label="'Address: ' + summary(item)">
              <span x-text="summary(item)"></span>
              <button type="button"
                      x-bind:aria-label="'Remove address: ' + summary(item)"
                      @click="remove(i)">&times;</button>
              <template x-for="(k) in Object.keys(item)" :key="k">
                <input type="hidden"
                       x-bind:name="'addresses[' + i + '][' + k + ']'"
                       x-bind:value="item[k]">
              </template>
            </span>
          </template>
          <span class="hint"
                aria-label="Empty address list"
                x-show="items.length === 0">No addresses yet.</span>
        </div>

        <div class="field" aria-label="Address line 1 field">
          <label class="label" for="address-line1">Line 1</label>
          <input class="text-input"
                 type="text"
                 id="address-line1"
                 aria-label="Address line 1"
                 placeholder="10 Downing St"
                 x-model="draft.line1">
        </div>
        <div class="field" aria-label="Address line 2 field">
          <label class="label" for="address-line2">Line 2</label>
          <input class="text-input"
                 type="text"
                 id="address-line2"
                 aria-label="Address line 2"
                 placeholder="Apartment, suite, building"
                 x-model="draft.line2">
        </div>
        <div class="field" aria-label="City field">
          <label class="label" for="address-city">City</label>
          <input class="text-input"
                 type="text"
                 id="address-city"
                 aria-label="City"
                 placeholder="London"
                 x-model="draft.city">
        </div>
        <div class="field" aria-label="State field">
          <label class="label" for="address-state">State / region</label>
          <input class="text-input"
                 type="text"
                 id="address-state"
                 aria-label="State or region"
                 placeholder="Greater London"
                 x-model="draft.state">
        </div>
        <div class="field" aria-label="Postal code field">
          <label class="label" for="address-postal">Postal code</label>
          <input class="text-input"
                 type="text"
                 id="address-postal"
                 aria-label="Postal code"
                 placeholder="SW1A 2AA"
                 x-model="draft.postal_code">
        </div>
        <div class="field" aria-label="Country field">
          <label class="label" for="address-country">Country (ISO 3166)</label>
          <input class="text-input"
                 type="text"
                 id="address-country"
                 aria-label="Country code"
                 placeholder="GB"
                 maxlength="2"
                 x-model="draft.country">
        </div>
        <div class="action-bar" role="toolbar" aria-label="Address actions">
          <button class="button"
                  type="button"
                  aria-label="Add address"
                  @click="add()">Add address</button>
        </div>
      </div>
    </fieldset>

    <fieldset class="fieldset" aria-label="Contact points">
      <legend>Contacts</legend>
      <span class="hint" id="contacts-hint">Phone, email, SMS, fax, pager, URL. The input switches based on the system.</span>

      <div class="tag-input"
           aria-label="Contact-point editor"
           x-data='{
             items: {{ telecoms | json_encode | safe }},
             draftSystem: "phone",
             draftValue: "",
             add() {
               var v = this.draftValue.trim();
               if (!v) return;
               this.items.push({ system: this.draftSystem, value: v });
               this.draftValue = "";
               if (window.lily &amp;&amp; window.lily.toast) {
                 window.lily.toast(this.draftSystem + " added", "info");
               }
             },
             remove(i) {
               var removed = this.items.splice(i, 1)[0];
               if (window.lily &amp;&amp; window.lily.toast &amp;&amp; removed) {
                 window.lily.toast(removed.system + " removed", "info");
               }
             }
           }'>
        <div class="tag-group" aria-label="Current contact points">
          <template x-for="(item, i) in items" :key="i">
            <span class="tag" x-bind:aria-label="'Contact: ' + item.system + ' ' + item.value">
              <strong x-text="item.system"></strong>
              <span x-text="item.value"></span>
              <button type="button"
                      x-bind:aria-label="'Remove ' + item.system + ' ' + item.value"
                      @click="remove(i)">&times;</button>
              <input type="hidden"
                     x-bind:name="'telecoms[' + i + '][system]'"
                     x-bind:value="item.system">
              <input type="hidden"
                     x-bind:name="'telecoms[' + i + '][value]'"
                     x-bind:value="item.value">
            </span>
          </template>
          <span class="hint"
                aria-label="Empty contact list"
                x-show="items.length === 0">No contact points yet.</span>
        </div>

        <div class="action-group" aria-label="Add contact point">
          <label class="screen-reader-span" for="contact-system">Contact system</label>
          <select class="select"
                  id="contact-system"
                  aria-label="Contact system"
                  x-model="draftSystem">
            <option class="option" value="phone">Phone</option>
            <option class="option" value="email">Email</option>
            <option class="option" value="sms">SMS</option>
            <option class="option" value="fax">Fax</option>
            <option class="option" value="pager">Pager</option>
            <option class="option" value="url">URL</option>
          </select>
          <label class="screen-reader-span" for="contact-value">Contact value</label>
          <template x-if="draftSystem === 'phone' || draftSystem === 'sms' || draftSystem === 'fax' || draftSystem === 'pager'">
            <input class="tel-input"
                   type="tel"
                   id="contact-value"
                   aria-label="Contact value (telephone)"
                   placeholder="+44 20 7946 0958"
                   x-model="draftValue"
                   @keydown.enter.prevent="add()">
          </template>
          <template x-if="draftSystem === 'email'">
            <input class="email-input"
                   type="email"
                   id="contact-value"
                   aria-label="Contact value (email)"
                   placeholder="alice@example.test"
                   x-model="draftValue"
                   @keydown.enter.prevent="add()">
          </template>
          <template x-if="draftSystem === 'url'">
            <input class="text-input"
                   type="url"
                   id="contact-value"
                   aria-label="Contact value (URL)"
                   placeholder="https://example.test/alice"
                   x-model="draftValue"
                   @keydown.enter.prevent="add()">
          </template>
          <button class="button"
                  type="button"
                  aria-label="Add contact point"
                  @click="add()">Add</button>
        </div>
      </div>
    </fieldset>

    {% if healthcare %}
    <fieldset class="fieldset" aria-label="Emergency contacts">
      <legend>Emergency contacts</legend>
      <span class="hint" id="emergency-contacts-hint">Family, next-of-kin, and other contacts to reach in an emergency. Mark one as primary.</span>

      <div class="tag-input"
           aria-label="Emergency contact editor"
           x-data='{
             items: {{ emergency_contacts | json_encode | safe }},
             draft: { name: "", relationship: "", phone: "", email: "", is_primary: false },
             error: "",
             add() {
               this.error = "";
               if (!this.draft.name.trim()) { this.error = "Name is required."; return; }
               if (!this.draft.relationship.trim()) { this.error = "Relationship is required."; return; }
               if (!this.draft.phone.trim() &amp;&amp; !this.draft.email.trim()) {
                 this.error = "At least one phone or email is required.";
                 return;
               }
               if (this.draft.is_primary) {
                 this.items.forEach(function (it) { it.is_primary = false; });
               }
               this.items.push(Object.assign({}, this.draft));
               this.draft = { name: "", relationship: "", phone: "", email: "", is_primary: false };
               if (window.lily &amp;&amp; window.lily.toast) {
                 window.lily.toast("Emergency contact added", "info");
               }
             },
             remove(i) {
               var removed = this.items.splice(i, 1)[0];
               if (window.lily &amp;&amp; window.lily.toast &amp;&amp; removed) {
                 window.lily.toast(removed.name + " removed", "info");
               }
             },
             setPrimary(i) {
               this.items.forEach(function (it, j) { it.is_primary = (i === j); });
               if (window.lily &amp;&amp; window.lily.toast) {
                 window.lily.toast(this.items[i].name + " set as primary", "info");
               }
             }
           }'>
        <ol class="summary-list" aria-label="Current emergency contacts">
          <template x-for="(item, i) in items" :key="i">
            <li class="summary-list-item"
                x-bind:aria-label="'Emergency contact: ' + item.name + ' (' + item.relationship + ')'">
              <dl>
                <dt>
                  <strong x-text="item.name"></strong>
                  <span x-show="item.relationship">&middot; <span x-text="item.relationship"></span></span>
                  <template x-if="item.is_primary">
                    <span class="badge" data-type="success" aria-label="Primary contact">Primary</span>
                  </template>
                </dt>
                <dd>
                  <div class="tag-group" aria-label="Contact telecoms">
                    <template x-if="item.phone">
                      <span class="tag" x-bind:aria-label="'Phone: ' + item.phone">
                        <strong>phone</strong>
                        <span x-text="item.phone"></span>
                      </span>
                    </template>
                    <template x-if="item.email">
                      <span class="tag" x-bind:aria-label="'Email: ' + item.email">
                        <strong>email</strong>
                        <span x-text="item.email"></span>
                      </span>
                    </template>
                  </div>
                  <div class="action-bar" role="toolbar" aria-label="Contact actions">
                    <button type="button"
                            class="action-bar-button"
                            x-show="!item.is_primary"
                            x-bind:aria-label="'Mark ' + item.name + ' as primary'"
                            @click="setPrimary(i)">Make primary</button>
                    <button type="button"
                            class="action-bar-button"
                            x-bind:aria-label="'Remove ' + item.name"
                            @click="remove(i)">Remove</button>
                  </div>
                  <template x-for="(k) in Object.keys(item)" :key="k">
                    <input type="hidden"
                           x-bind:name="'emergency_contacts[' + i + '][' + k + ']'"
                           x-bind:value="item[k]">
                  </template>
                </dd>
              </dl>
            </li>
          </template>
        </ol>
        <p x-show="items.length === 0">
          <span class="hint" aria-label="Empty emergency contact list">No emergency contacts yet.</span>
        </p>

        <div x-show="error" x-transition>
          <span class="error-message"
                role="alert"
                x-text="error"
                x-bind:aria-label="error"></span>
        </div>

        <div class="field" aria-label="Contact name field" data-required>
          <label class="label" for="ec-name">Name</label>
          <input class="text-input"
                 type="text"
                 id="ec-name"
                 aria-label="Emergency contact name"
                 placeholder="Jane Smith"
                 x-model="draft.name">
        </div>
        <div class="field" aria-label="Contact relationship field" data-required>
          <label class="label" for="ec-relationship">Relationship</label>
          <select class="select"
                  id="ec-relationship"
                  aria-label="Relationship to {{ entity_singular }}"
                  x-model="draft.relationship">
            <option class="option" value="">Choose&hellip;</option>
            <option class="option" value="spouse">Spouse</option>
            <option class="option" value="partner">Partner</option>
            <option class="option" value="parent">Parent</option>
            <option class="option" value="child">Child</option>
            <option class="option" value="sibling">Sibling</option>
            <option class="option" value="guardian">Guardian</option>
            <option class="option" value="friend">Friend</option>
            <option class="option" value="other">Other</option>
          </select>
        </div>
        <div class="field" aria-label="Contact phone field">
          <label class="label" for="ec-phone">Phone</label>
          <input class="tel-input"
                 type="tel"
                 id="ec-phone"
                 aria-label="Emergency contact phone"
                 placeholder="+44 20 7946 0958"
                 x-model="draft.phone">
        </div>
        <div class="field" aria-label="Contact email field">
          <label class="label" for="ec-email">Email</label>
          <input class="email-input"
                 type="email"
                 id="ec-email"
                 aria-label="Emergency contact email"
                 placeholder="jane@example.test"
                 x-model="draft.email">
        </div>
        <div class="field" aria-label="Primary flag field">
          <label class="label" for="ec-primary">
            <input id="ec-primary"
                   type="checkbox"
                   aria-label="Mark as primary contact"
                   x-model="draft.is_primary">
            Primary contact
          </label>
          <span class="hint">Setting this clears the primary flag on any existing contact.</span>
        </div>
        <div class="action-bar" role="toolbar" aria-label="Emergency contact actions">
          <button class="button"
                  type="button"
                  aria-label="Add emergency contact"
                  @click="add()">Add contact</button>
        </div>
      </div>
    </fieldset>
    {% endif %}

    <fieldset class="fieldset" aria-label="Identity documents">
      <legend>Identity documents</legend>
      <span class="hint" id="documents-hint">Passport, driver's licence, national ID, etc. Numbers are validated; expiry must be after issue date.</span>

      <div class="tag-input"
           aria-label="Document editor"
           x-data='{
             items: {{ documents | json_encode | safe }},
             draft: { type: "PASSPORT", number: "", issuing_country: "", issuing_authority: "", issue_date: "", expiry_date: "" },
             error: "",
             add() {
               this.error = "";
               if (!this.draft.number.trim()) {
                 this.error = "Document number is required.";
                 return;
               }
               if (this.draft.issue_date &amp;&amp; this.draft.expiry_date &amp;&amp; this.draft.issue_date >= this.draft.expiry_date) {
                 this.error = "Expiry date must be after issue date.";
                 return;
               }
               this.items.push(Object.assign({}, this.draft));
               this.draft = { type: this.draft.type, number: "", issuing_country: "", issuing_authority: "", issue_date: "", expiry_date: "" };
               if (window.lily &amp;&amp; window.lily.toast) {
                 window.lily.toast("Document added", "info");
               }
             },
             remove(i) {
               var removed = this.items.splice(i, 1)[0];
               if (window.lily &amp;&amp; window.lily.toast &amp;&amp; removed) {
                 window.lily.toast(removed.type + " removed", "info");
               }
             },
             expiryBadge(item) {
               if (!item.expiry_date) return { type: "info", label: "no expiry" };
               var today = new Date().toISOString().slice(0, 10);
               if (item.expiry_date < today) return { type: "error", label: "expired" };
               var soon = new Date(); soon.setMonth(soon.getMonth() + 6);
               if (item.expiry_date < soon.toISOString().slice(0, 10)) return { type: "warning", label: "expiring soon" };
               return { type: "success", label: "valid" };
             }
           }'>
        <div class="tag-group" aria-label="Current documents">
          <template x-for="(item, i) in items" :key="i">
            <span class="tag" x-bind:aria-label="'Document: ' + item.type + ' ' + item.number">
              <strong x-text="item.type"></strong>
              <span x-text="item.number"></span>
              <span x-show="item.issuing_country">(<span x-text="item.issuing_country"></span>)</span>
              <span class="badge"
                    x-bind:data-type="expiryBadge(item).type"
                    x-bind:aria-label="'Expiry: ' + expiryBadge(item).label"
                    x-text="expiryBadge(item).label"></span>
              <button type="button"
                      x-bind:aria-label="'Remove ' + item.type + ' ' + item.number"
                      @click="remove(i)">&times;</button>
              <template x-for="(k) in Object.keys(item)" :key="k">
                <input type="hidden"
                       x-bind:name="'documents[' + i + '][' + k + ']'"
                       x-bind:value="item[k]">
              </template>
            </span>
          </template>
          <span class="hint"
                aria-label="Empty document list"
                x-show="items.length === 0">No documents yet.</span>
        </div>

        <div x-show="error" x-transition>
          <span class="error-message"
                role="alert"
                x-text="error"
                x-bind:aria-label="error"></span>
        </div>

        <div class="field" aria-label="Document type field" data-required>
          <label class="label" for="document-type">Type</label>
          <select class="select"
                  id="document-type"
                  aria-label="Document type"
                  x-model="draft.type">
            <option class="option" value="PASSPORT">Passport</option>
            <option class="option" value="BIRTH_CERTIFICATE">Birth certificate</option>
            <option class="option" value="NATIONAL_ID">National ID</option>
            <option class="option" value="DRIVERS_LICENSE">Driver's licence</option>
            <option class="option" value="VOTER_ID">Voter ID</option>
            <option class="option" value="MILITARY_ID">Military ID</option>
            <option class="option" value="RESIDENCE_PERMIT">Residence permit</option>
            <option class="option" value="WORK_PERMIT">Work permit</option>
            <option class="option" value="OTHER">Other</option>
          </select>
        </div>
        <div class="field" aria-label="Document number field" data-required>
          <label class="label" for="document-number">Number</label>
          <input class="text-input"
                 type="text"
                 id="document-number"
                 aria-label="Document number"
                 placeholder="X12345678"
                 x-model="draft.number">
        </div>
        <div class="field" aria-label="Issuing country field">
          <label class="label" for="document-country">Issuing country (ISO 3166)</label>
          <input class="text-input"
                 type="text"
                 id="document-country"
                 aria-label="Issuing country"
                 placeholder="US"
                 maxlength="2"
                 x-model="draft.issuing_country">
        </div>
        <div class="field" aria-label="Issuing authority field">
          <label class="label" for="document-authority">Issuing authority</label>
          <input class="text-input"
                 type="text"
                 id="document-authority"
                 aria-label="Issuing authority"
                 placeholder="US Dept of State"
                 x-model="draft.issuing_authority">
        </div>
        <div class="field" aria-label="Issue date field">
          <label class="label" for="document-issue">Issue date</label>
          <input class="text-input"
                 type="date"
                 id="document-issue"
                 aria-label="Issue date"
                 x-model="draft.issue_date">
        </div>
        <div class="field" aria-label="Expiry date field">
          <label class="label" for="document-expiry">Expiry date</label>
          <input class="text-input"
                 type="date"
                 id="document-expiry"
                 aria-label="Expiry date"
                 x-model="draft.expiry_date">
        </div>
        <div class="action-bar" role="toolbar" aria-label="Document actions">
          <button class="button"
                  type="button"
                  aria-label="Add document"
                  @click="add()">Add document</button>
        </div>
      </div>
    </fieldset>

    <div class="field" aria-label="Status field">
      <label class="label" for="status">Status</label>
      <button class="hover-card-trigger"
              type="button"
              aria-label="Help: Status"
              aria-describedby="status-help-card">?</button>
      <div class="hover-card" id="status-help-card" role="tooltip" aria-label="Status help">
        Setting status to <strong>Inactive</strong> performs a soft delete: the record is removed from search and the index table, but the full history remains in the audit log and can be restored within 30 days.
      </div>
      <span class="hint" id="status-hint">Active records appear in search; inactive records are soft-deleted.</span>
      <select class="select"
              id="status"
              name="status"
              aria-label="Status"
              aria-describedby="status-hint">
        <option class="option" value="active" {% if record.active %}selected{% endif %}>Active</option>
        <option class="option" value="inactive" {% if not record.active %}selected{% endif %}>Inactive</option>
      </select>
    </div>

    <div class="action-bar" role="toolbar" aria-label="Edit form actions">
      <button class="button" type="submit" aria-label="Save {{ entity_singular }}">Save</button>
      <a class="button"
         href="/{{ entity_plural }}/{{ record.id }}"
         aria-label="Cancel edit and return to {{ entity_singular }}">Cancel</a>
    </div>
  </form>
</section>
{% endblock content %}