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
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
# Specification: Add Crew Member Functionality
## Vessel & Crew Info Page - SailingMedAdvisor

**Document Version:** 1.0  
**Last Updated:** January 22, 2026  
**Component:** Vessel & Crew Information Management

---

## Overview

The Add Crew Member functionality allows authorized users to register new crew members, passengers, or captain information into the SailingMedAdvisor system. This feature is located within the "Vessel & Crew Info" tab and provides a comprehensive data entry form for capturing essential crew information, documentation, and emergency contacts.

---

## Location & Access

**Navigation Path:**  
Main Navigation Bar → **VESSEL & CREW** tab → Crew Information section → **Add New Crew Member** (expandable section)

**Access Control:**  
- Requires user authentication (session-based)
- Available to all authenticated users
- No role-based restrictions currently implemented

---

## User Interface Components

###  1. Collapsible Section Header

**Element:** `div.col-header.crew-med-header`  
**Default State:** Collapsed (▸ icon)  
**Interactive Element:** Clickable header with toggle icon  
**Visual Indicator:** Arrow icon (▸ when collapsed, ▾ when expanded)

**Header Text:** "Add New Crew Member"

**Behavior:**
- Single-click toggles expansion/collapse
- Icon rotates to indicate current state
- State persists using localStorage with key based on `data-sidebar-id="crew-add"`

---

### 2. Input Form Fields

The add crew form is organized into logical grouped sections with a responsive grid layout.

#### **2.1 Name Fields**
**Layout:** 3-column grid (`grid-template-columns: 1fr 1fr 1fr`)

| Field Label | Element ID | Type | Required | Placeholder | Notes |
|------------|-----------|------|----------|-------------|--------|
| First Name | `cn-first` | text | Yes (*) | - | Primary identifier |
| Middle Name(s) | `cn-middle` | text | No | - | Optional middle names |
| Last Name | `cn-last` | text | Yes (*) | - | Family name |

#### **2.2 Personal Information**
**Layout:** 3-column grid

| Field Label | Element ID | Type | Required | Options/Format | Notes |
|------------|-----------|------|----------|----------------|--------|
| Sex | `cn-sex` | select | Yes (*) | Male, Female, Non-binary, Other, Prefer not to say | Gender identity |
| Birthdate | `cn-birthdate` | date | Yes (*) | YYYY-MM-DD | Age calculation base |
| Position | `cn-position` | select | Yes (*) | Captain, Crew, Passenger | Role aboard vessel |

#### **2.3 Documentation Fields**
**Layout:** 4-column grid

| Field Label | Element ID | Type | Required | Format/List | Notes |
|------------|-----------|------|----------|-------------|--------|
| Citizenship | `cn-citizenship` | text + datalist | Yes (*) | Country names | Autocomplete enabled |
| Passport Number | `cn-passport` | text | Yes (*) | Alphanumeric | Official passport ID |
| Issue Date | `cn-pass-issue` | date | No | YYYY-MM-DD | Passport issue |
| Expiry Date | `cn-pass-expiry` | date | No | YYYY-MM-DD | Passport expiration |

**Datalist Options** (id="countries"):  
USA, Canada, UK, Australia, New Zealand, France, Germany, Spain, Italy, Netherlands, Singapore, Malaysia, Thailand, Philippines, Japan, China, India

#### **2.4 Contact & File Upload**
**Layout:** 3-column grid (contact) + single row (files)

| Field Label | Element ID | Type | Required | Format | Notes |
|------------|-----------|------|----------|--------|--------|
| Cell/WhatsApp | `cn-phone` | text | No | +[country][number] | International format |
| Passport Photo/PDF | `cn-passport-photo` | file | No | image/*,.pdf | Photo upload |
| Passport Page Photo/PDF | `cn-passport-page` | file | No | image/*,.pdf | Document scan |

#### **2.5 Emergency Contact Section**
**Sub-header:** "Emergency Contact"  
**Layout:** 4-column grid + notes field

| Field Label | Element ID | Type | Required | Format | Notes |
|------------|-----------|------|----------|--------|--------|
| Name | `cn-emerg-name` | text | No | Full name | Contact person |
| Relationship | `cn-emerg-rel` | text | No | e.g., Spouse, Parent | Relation to crew |
| Phone | `cn-emerg-phone` | text | No | Phone number | Contact number |
| Email | `cn-emerg-email` | email | No | email@domain.com | Email address |
| Emergency Contact Notes | `cn-emerg-notes` | text | No | Additional info | Full-width field |

---

### 3. Action Button

**Element:** `<button onclick="addCrew()">`  
**Class:** `btn btn-sm`  
**Style:** `background:var(--dark); width:100%;`  
**Text:** "+ Add Crew Member"

**Visual Properties:**
- Dark background color (CSS variable: `--dark` = `#2c3e50`)
- Full width of container
- Small button sizing (`btn-sm` = `padding: 6px 12px; font-size: 13px`)
- White text color
- Pointer cursor on hover
- Transition effects on interaction

---

## Functional Behavior

### 1. Form Submission Process

**Trigger:** Click on "+ Add Crew Member" button  
**Handler Function:** `addCrew()` (defined in `/static/js/crew.js`)

**Execution Flow:**

1. **Data Collection**
   - Reads all input field values by element ID
   - Reads file uploads (if any)
   - Constructs crew member object

2. **Validation**
   - Checks required fields (marked with *)
   - First Name, Last Name, Sex, Birthdate, Position, Citizenship, and Passport Number are mandatory
   - Displays browser alert if required fields are missing
   - Validation message format: "Please fill in: First Name, Last Name, Sex, Birthdate, Position, Citizenship, Passport Number."

3. **File Handling**
   - Processes uploaded passport photo/PDF files
   - Stores file references or base64 encoded data
   - Associates files with crew member record

4. **API Request**
   ```javascript
   POST /api/data/patients
   Headers: { credentials: 'same-origin' }
   Content-Type: application/json
   Body: [updated patients array including new crew member]
   ```

5. **Response Handling**
   - **Success (200 OK):**
     - Displays success alert
     - Clears all form fields
     - Reloads crew list display (`loadData()`)
     - Collapses the Add New Crew Member section
   
   - **Failure (non-200):**
     - Displays error alert with message
     - Retains form data for correction
     - Does not clear form fields

6. **UI Updates**
   - Updates crew member dropdown in chat interface (`#p-select`)
   - Updates crew list display in Crew Information section
   - Updates crew list in Crew Health & Log tab
   - Refreshes crew credentials (if username/password set)

---

### 2. Data Structure

**Stored Object Format:**
```javascript
{
  "firstName": string,
  "middleName": string | "",
  "lastName": string,
  "name": string,  // Computed: "firstName middleName lastName"
  "sex": string,   // "Male" | "Female" | "Non-binary" | "Other" | "Prefer not to say"
  "birthdate": string,  // "YYYY-MM-DD"
  "position": string,   // "Captain" | "Crew" | "Passenger"
  "citizenship": string,
  "passportNumber": string,
  "passportIssueDate": string | "",    // "YYYY-MM-DD"
  "passportExpiryDate": string | "",   // "YYYY-MM-DD"
  "phone": string | "",
  "passportPhoto": string | null,      // File reference or base64
  "passportPage": string | null,       // File reference or base64
  "emergencyContact": {
    "name": string | "",
    "relationship": string | "",
    "phone": string | "",
    "email": string | "",
    "notes": string | ""
  },
  "history": string | "",              // Medical history notes
  "username": string | "",             // Optional login credential
  "password": string | ""              // Optional login credential
}
```

**Storage Location:**  
`/data/patients.json` (server-side file storage)

**Data Persistence:**  
- Written to disk immediately upon successful API call
- Loaded on page initialization and tab navigation
- Cached in browser session until page reload

---

### 3. Form Field Clearing

After successful submission, all form fields are reset:

```javascript
document.getElementById('cn-first').value = '';
document.getElementById('cn-middle').value = '';
document.getElementById('cn-last').value = '';
// ... (all fields cleared to empty string)
document.getElementById('cn-passport-photo').value = '';
document.getElementById('cn-passport-page').value = '';
```

---

### 4. Integration Points

#### **4.1 Patient Selection Dropdown**
- Location: MEDGEMMA CHAT tab → "Crew Member Receiving Care"
- Element ID: `#p-select`
- Auto-populates with format: "LastName, FirstName"
- Includes default option: "Unnamed Crew"
- Updates immediately after adding new crew member

#### **4.2 Crew Information List**
- Location: VESSEL & CREW tab → Crew Information section
- Container ID: `#crew-info-list`
- Displays all registered crew members as expandable cards
- Each card shows:
  - Name and position
  - Basic details (birthdate, citizenship, passport)
  - Emergency contact information
  - Edit and Delete action buttons

#### **4.3 Crew Health & Log**
- Location: CREW HEALTH & LOG tab
- Container ID: `#crew-medical-list`
- Shows crew members with medical history entries
- Allows adding medical notes per crew member

#### **4.4 Login Credentials**
- Location: SETTINGS tab → Crew Login Credentials
- Displays crew members who have username/password set
- Enables authentication for restricted access

---

## CSS Styling

### Form Container Styles

```css
.col-body {
    padding: 15px;
    background: #f8f9fa;
    display: none;  /* Initially collapsed */
}
```

### Grid Layout

```css
display: grid;
grid-template-columns: 1fr 1fr 1fr;  /* 3-column for names */
grid-template-columns: 1fr 1fr 1fr 1fr;  /* 4-column for docs/contact */
gap: 8px;
margin-bottom: 8px;
font-size: 15px;
```

### Input Field Styles

```css
input, select, textarea {
    padding: 6px;
    width: 100%;
    font-size: 15px;
}

label {
    font-size: 13px;
    margin-bottom: 2px;
    display: block;
}
```

### Button Styles

```css
.btn-sm {
    padding: 6px 12px;
    font-size: 13px;
    background: var(--dark);  /* #2c3e50 */
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
}
```

---

## Error Handling

### Client-Side Validation Errors

**Missing Required Fields:**
```
Alert Message: "Please fill in: [list of missing fields]"
Behavior: Form remains open, data retained, focus on first missing field
```

**File Upload Errors:**
```
Alert Message: "Error uploading files: [error description]"
Behavior: Form remains open, previously entered data retained
```

### Server-Side Errors

**API Request Failure:**
```
Alert Message: "Failed to add crew member: [HTTP status or error message]"
Behavior: Form remains open, all data retained for retry
```

**Network Error:**
```
Alert Message: "Network error. Please check your connection and try again."
Behavior: Form remains open, all data retained
```

---

## Accessibility Features

- **Keyboard Navigation:** Full tab-through support for all form fields
- **Screen Reader Support:** Proper label associations via `<label>` elements
- **Required Field Indicators:** Asterisk (*) in field labels
- **Focus Management:** First field receives focus when section expands
- **Error Messaging:** Clear, descriptive error messages for validation failures

---

## Security Considerations

1. **Authentication:** Requires active session (cookie-based)
2. **Data Validation:** Server-side validation of all inputs
3. **File Upload Security:**  
   - Restricted to image/* and .pdf MIME types
   - File size limits enforced server-side
   - Sanitized file names

4. **XSS Prevention:** All user inputs are escaped before rendering
5. **CSRF Protection:** SessionMiddleware with same-site=lax policy

---

## Performance Considerations

- **Form Load Time:** < 100ms (minimal DOM manipulation)
- **API Response Time:** Typically < 500ms for add operation
- **File Upload:** Dependent on file size and connection speed
- **Data Reload:** Full crew list refresh after successful addition

---

## Browser Compatibility

- **Tested Browsers:** Chrome, Firefox, Edge, Safari
- **Required Features:**
  - ES6+ JavaScript support
  - Fetch API
  - LocalStorage
  - HTML5 form inputs (date, email, file)
  - CSS Grid Layout

---

## Related Documentation

- Backend API: `/api/data/patients` endpoint specification
- Crew Management Module: `/static/js/crew.js`
- Data Storage: `/data/patients.json` format specification
- Authentication: Session management and login flow

---

## Known Issues & Limitations

1. **File Storage:** Currently stores base64 encoded files in JSON (may cause performance issues with large files)
2. **Validation:** Phone number format validation is not enforced
3. **Duplicate Prevention:** No check for duplicate passport numbers or names
4. **Photo Preview:** Uploaded photos are not previewed before submission
5. **Internationalization:** Form labels and messages are English-only

---

## Future Enhancements

- Photo preview before submission
- Duplicate detection (passport number, name)
- International phone number validation
- Multi-language support
- Batch import from CSV/Excel
- Photo compression before upload
- Separate file storage system (not embedded in JSON)
- Auto-save draft functionality
- Field-level inline validation

---

**End of Specification**