# Patients Tab Flow - Doctor Side

## Overview
This document describes the complete flow for the Patients Tab on the doctor's side of the RXNetworx application. This is a click-through demonstration that shows how doctors review and approve prescription requests from patients.

## Flow Diagram

```
Patient Submits Intake Form (Patient Side)
           ↓
[PATIENTS TAB] - List of New and Pending Requests
           ↓
[VIEW PATIENT] - Review Patient Details & Medical Info
           ↓
[APPROVAL & SIG] - Doctor Decision
           ↓
     ┌──────┴──────┬─────────────────┐
     ↓             ↓                 ↓
[APPROVE]    [REQUEST INFO]      [DECLINE]
     ↓
Generate eRX
     ↓
[PRESCRIPTIONS TAB] - Approved eRX List
```

## Pages & Features

### 1. Patients Tab (`/patients`)
**File:** `resources/views/patients.blade.php`

**Features:**
- Displays all newly submitted intake forms from patients
- Shows New and Pending status requests
- Table includes:
  - Patient Name (with avatar initials)
  - Date of Request
  - Chief Complaint
  - Medication
  - Status (New/Pending badge)
  - Action button (View)
- Badge showing total pending reviews
- Responsive design for mobile and desktop

**Demo Data:** 5 sample patients with various conditions

### 2. View Patient Page (`/patients/{id}`)
**File:** `resources/views/patients/view.blade.php`

**Features:**
- **Personal Information Section:**
  - First Name, Last Name
  - Date of Birth, Gender
  - Contact Number, Email
  - Full Address

- **Medical Information Section:**
  - Chief Complaint
  - Requested Medication
  - Pregnancy/Breastfeeding status
  - Medical Conditions
  - Current Medications
  - Drug Allergies (highlighted in red)

- **Pharmacy Information Section:**
  - Selected Pharmacy
  - Request Type (Refill/New Prescription)

- **Approval & Sig Section:**
  - **Sig (Directions for Use)** - Required field
    - Textarea for entering prescription directions
    - Examples provided:
      - "Take 1 tablet by mouth twice daily after meals for 7 days"
      - "Apply thin layer to affected area once daily"
      - "Inject 0.5 mL weekly"
  
  - **Additional Notes** - Optional field
    - For pharmacist instructions
  
  - **Action Buttons:**
    - **Decline** (Red) - Reject the prescription request
    - **Request More Info** (Yellow) - Ask patient for additional information
    - **Approve & Sign** (Green) - Approve and generate eRX

### 3. Prescriptions Tab (`/prescriptions`)
**File:** `resources/views/prescriptions.blade.php`

**Features:**
- Displays all approved eRX prescriptions
- Table includes:
  - eRX ID (unique identifier)
  - Patient Name & DOB
  - Medication
  - Sig (Directions)
  - Date Approved
  - Pharmacy
  - Status (Sent to Pharmacy/Picked Up)
  - Actions (View Details, Download eRX)
- Badge showing total approved prescriptions
- Success message when new prescription is approved
- Demo data includes 3 sample approved prescriptions

## Routes

```php
// Doctor - Patients Tab
Route::get('/patients', [PrototypeController::class, 'patients'])
    ->name('patients');

Route::get('/patients/{id}', [PrototypeController::class, 'viewPatient'])
    ->name('patients.view');

Route::post('/patients/{id}/process', [PrototypeController::class, 'processPatientRequest'])
    ->name('patients.process');

// Doctor - Prescriptions Tab
Route::get('/prescriptions', [PrototypeController::class, 'prescriptions'])
    ->name('prescriptions');
```

## Controller Methods

### `patients()`
- **Purpose:** Display list of pending patient requests
- **Access:** Doctor only
- **Returns:** View with patient list
- **Demo Data:** 5 sample patients

### `viewPatient($id)`
- **Purpose:** Display detailed patient information and assessment form
- **Access:** Doctor only
- **Parameters:** `$id` - Patient ID
- **Returns:** View with patient details
- **Error Handling:** Redirects to patients list if patient not found

### `processPatientRequest(Request $request, $id)`
- **Purpose:** Process doctor's decision on patient request
- **Access:** Doctor only
- **Parameters:** 
  - `$id` - Patient ID
  - `action` - approve|request_more_info|decline
  - `sig` - Prescription directions (required for approval)
  - `notes` - Additional notes (optional)

**Actions:**
1. **Approve:**
   - Generates unique eRX ID (format: eRX-####)
   - Stores prescription in session
   - Redirects to Prescriptions tab with success message

2. **Request More Info:**
   - Redirects to Patients tab with info message
   - (In production: would send notification to patient)

3. **Decline:**
   - Redirects to Patients tab with warning message
   - (In production: would notify patient and log reason)

### `prescriptions()`
- **Purpose:** Display list of approved prescriptions
- **Access:** Doctor only
- **Returns:** View with prescription list
- **Data Source:** 
  - Session-stored approved prescriptions
  - Falls back to demo data if none exist

## Navigation Integration

### Desktop Sidebar
- "Patients" link with warning badge (5 pending)
- "Prescriptions" link
- Active state highlighting

### Mobile Bottom Navigation
- "Patients" icon and label
- "eRX" (Prescriptions) icon and label
- Active state styling

## User Experience Flow

### Typical Doctor Workflow:

1. **Doctor logs in** with email containing "doctor"
   - Example: `doctor@rxnetworx.com`

2. **Views Dashboard** showing metrics:
   - 42 Pending RX
   - Activity feed

3. **Clicks "Patients" tab**
   - Sees 5 pending requests
   - Can sort/filter by date, complaint, etc.

4. **Clicks "View" on a patient** (e.g., Sarah Johnson)
   - Reviews personal information
   - Checks medical history and allergies
   - Reads chief complaint: "Persistent cough and chest congestion for 5 days"
   - Sees requested medication: Amoxicillin 500mg
   - Notes patient takes Lisinopril for hypertension

5. **Makes decision:**
   
   **Option A - Approve:**
   - Enters Sig: "Take 1 capsule by mouth three times daily for 10 days"
   - Optionally adds notes: "Patient has been screened for allergies"
   - Clicks "Approve & Sign"
   - System generates eRX-0004
   - Redirected to Prescriptions tab with success message
   - eRX now visible in approved prescriptions list

   **Option B - Request More Info:**
   - Clicks "Request More Info"
   - Returns to Patients tab
   - Message displayed: "Request for more information sent to patient"
   - (In production: patient receives notification)

   **Option C - Decline:**
   - Clicks "Decline"
   - Returns to Patients tab
   - Message displayed: "Prescription request declined"
   - (In production: patient receives notification with reason)

6. **Views Prescriptions Tab**
   - Sees all approved eRX prescriptions
   - Can download or view details
   - Tracks status (Sent to Pharmacy, Picked Up)

## Technical Notes

### Session Management
- Approved prescriptions stored in session: `approved_prescriptions`
- Allows persistence during demo session
- In production: would use database storage

### Demo Data Structure
Each patient record includes:
```php
[
    'id' => 'PT-001',
    'first_name' => 'Sarah',
    'last_name' => 'Johnson',
    'date_of_birth' => '1985-03-15',
    'gender' => 'Female',
    'contact_number' => '(555) 123-4567',
    'email' => 'sarah.johnson@email.com',
    'street_address' => '123 Main Street',
    'city' => 'Los Angeles',
    'state' => 'CA',
    'zip_code' => '90001',
    'chief_complaint' => 'Persistent cough and chest congestion for 5 days',
    'medication' => 'Amoxicillin 500mg',
    'request_type' => 'new', // or 'refill'
    'is_pregnant' => 'no',
    'is_breastfeeding' => 'no',
    'has_medical_conditions' => 'yes',
    'medical_conditions_list' => 'Hypertension',
    'has_other_medications' => 'yes',
    'other_medications_list' => 'Lisinopril 10mg daily',
    'has_drug_allergies' => 'no',
    'drug_allergies_list' => '',
    'pharmacy' => 'CVS Pharmacy - 456 Main St, Los Angeles, CA 90001',
    'date_submitted' => '2026-01-20 09:30:00',
    'status' => 'New',
    'status_class' => 'warning'
]
```

### Security Considerations
- All patient routes check for doctor user type
- Redirects to login if not authenticated as doctor
- Form uses CSRF protection (@csrf)
- Input validation on sig field (required)

### Accessibility
- Semantic HTML structure
- ARIA labels on interactive elements
- Keyboard navigation support
- Screen reader friendly
- Proper heading hierarchy
- Focus states on form inputs

### Responsive Design
- Mobile-first approach
- Stacked layout on mobile
- Table scrolls horizontally on small screens
- Touch-friendly button sizes (minimum 44px)
- Readable font sizes on all devices

## Flash Messages

Success/Info/Warning messages are displayed using Bootstrap alerts:
- **Success** (Green): "Prescription approved and sent to pharmacy! eRX ID: eRX-####"
- **Info** (Blue): "Request for more information sent to patient."
- **Warning** (Yellow): "Prescription request declined."

## Styling

Custom CSS includes:
- Badge colors for status indicators
- Hover effects on table rows
- Active navigation states
- Card shadows and borders
- Responsive utilities
- Form input focus states

## Testing the Flow

### Quick Test Path:

1. Login as doctor:
   ```
   Email: doctor@rxnetworx.com
   Password: (any)
   ```

2. Click "Patients" in sidebar

3. Click "View" on first patient (Sarah Johnson)

4. Enter Sig: "Take 1 capsule by mouth three times daily for 10 days"

5. Click "Approve & Sign"

6. Verify redirect to Prescriptions tab

7. Confirm new eRX appears in list with:
   - Generated eRX ID
   - Patient name: Sarah Johnson
   - Medication: Amoxicillin 500mg
   - Sig as entered
   - Status: "Sent to Pharmacy"

## Future Enhancements (Production)

- Database integration for persistent storage
- Real-time notifications to patients
- Digital signature capture
- PDF generation for eRX documents
- Audit trail logging
- Search and filter functionality
- Pagination for large patient lists
- Email notifications to pharmacy
- Drug interaction checking
- Prescription history tracking
- Patient communication portal

## Files Modified/Created

### Created:
- `resources/views/patients.blade.php`
- `resources/views/patients/view.blade.php`
- `resources/views/prescriptions.blade.php`
- `PATIENTS_TAB_FLOW.md` (this file)

### Modified:
- `app/Http/Controllers/PrototypeController.php`
  - Added `patients()` method
  - Added `viewPatient($id)` method
  - Added `processPatientRequest(Request $request, $id)` method
  - Added `prescriptions()` method
  - Added `getPatientsData()` helper method

- `routes/web.php`
  - Added patients routes
  - Added prescriptions route

- `resources/views/layouts/app.blade.php`
  - Updated desktop navigation links
  - Updated mobile navigation links
  - Added active state handling

## Support

For questions or issues with the Patients Tab flow, please refer to:
- Main application documentation
- Controller comments
- Blade template comments
- This flow documentation
