# RFID Integration Guide

## Overview

The application now supports RFID scanner integration for quick report creation. When you scan an RFID tag with a barcode scanner, the application automatically:

1. Opens a dedicated form
2. Displays the scanned RFID code (read-only)
3. Attempts to find and auto-populate the location where that RFID is registered
4. Allows you to complete the report details

## How to Use

### Step 1: Connect RFID Scanner
- Connect your RFID reader/barcode scanner to your device
- Most RFID scanners emulate keyboard input
- The scanner should send data followed by an Enter key

### Step 2: Scan an RFID Code
- Open the application in your browser
- Point your RFID scanner at the tag
- The application will automatically capture the code

### Step 3: RFID Form Opens Automatically
When an RFID code is scanned:
- A new modal form appears titled "Sesizare prin RFID"
- The RFID code is displayed in a read-only field
- The location is auto-filled if the RFID has been used before

### Step 4: Fill in the Details

#### If Location is Found (Auto-populated):
- ✓ Location field shows the matching location
- Click "Descriere" (Description) field
- Enter problem description (minimum 10 characters)
- Select category (Gropi, Iluminat, Trafic, etc.)
- Click "Trimite sesizarea" (Submit report)

#### If Location is NOT Found:
- Click "Ia locatia din centru hartii" (Take location from map center)
- The location will be taken from the center of the current map view
- Enter problem description
- Select category
- Click "Trimite sesizarea" (Submit report)

### Step 5: Report Submission
- The report is saved with:
  - RFID code (read-only)
  - Location (from database or map)
  - Description
  - Category
  - Current timestamp

## Keyboard Shortcuts

- **Escape**: Close the RFID form and return to map
- **RFID Scan + Enter**: Open RFID form with the scanned code
- **Ctrl+C / Alt+C / Cmd+C**: Not captured as RFID (safe to use for copying)

## Database Integration

### Storing RFID Data
RFID codes are stored in the report with field name: `rfid_code`

### Finding Locations by RFID
If an RFID code has been used before, the location is automatically retrieved from the most recent report with that code.

### Adding Existing Locations
To populate locations for new RFID codes:
1. Manually create a report with location
2. The RFID code will be stored with that report
3. Future scans of that RFID will auto-populate the location

## Troubleshooting

### RFID Scanner Not Working?
- Verify scanner is properly connected to your device
- Check that scanner emulates keyboard input (most modern RFID readers do)
- Ensure the application window has focus
- Test by typing manually into the form field

### Location Not Found?
- The RFID code must exist in a previous report in `sesizari.json`
- Use the "Ia locatia din centru hartii" button to manually select location
- The center pin on the map shows the current location point

### Code Won't Scan?
- RFID reader may need configuration to output keyboard data
- Check scanner manual for keyboard emulation mode
- Try scanning from the application window (not when map is in focus)

## Technical Details

### RFID Code Requirements
- Minimum 3 characters
- No maximum length (but practical scanners usually produce 8-15 chars)
- Can contain letters, numbers, and special characters
- Case-insensitive matching

### Location Sources (in priority order)
1. Auto-matched from existing report with same RFID code
2. Manually selected from map center
3. User-editable after form opens

### Form Validation
- RFID code: Required (provided by scanner)
- Description: Minimum 10 characters
- Location: Required (auto-filled or user-selected)
- Category: Required (defaults to "Gropi / carosabil")

## Examples

### Example 1: Known RFID Location
```
1. Scan RFID: "TAG-00012345"
2. Form opens
3. Location automatically shows: "Strada Armoniei 12 (46.537077, 24.582920)"
4. Enter description: "Bec ars in lampă"
5. Select category: "Iluminat public"
6. Click Submit
7. Report saved with RFID code
```

### Example 2: New RFID Location
```
1. Scan RFID: "TAG-NEW-001"
2. Form opens
3. Location shows: "Locatia nu a fost gasita..."
4. Click "Ia locatia din centru hartii"
5. Location auto-fills with map center coordinates
6. Enter description: "Groapă mare pe carosabil"
7. Select category: "Gropi / carosabil"
8. Click Submit
9. Report saved with new RFID location mapping
```

## Browser Compatibility

- Chrome/Edge: Fully supported
- Firefox: Fully supported
- Safari: Fully supported (including iOS with external keyboard)
- Mobile browsers: Works with external RFID keyboard/scanner

## Notes for IT/Admin

### Setting Up RFID Readers
Most modern RFID scanners work out of the box:
1. Connect via USB or Bluetooth
2. Ensure operating system recognizes as keyboard
3. Scan tag - data should appear as text input
4. Codes typically end with automatic Enter key press

### Batch Operations
If you need to process multiple RFID tags:
1. Each scan opens a new form
2. Fill details quickly (10+ second description minimum)
3. Reports queue up in `sesizari.json`
4. Map updates in real-time after each submission

### Data Integrity
- RFID codes are stored exactly as scanned
- No automatic cleanup or transformation
- Case-sensitive when matching (but searches are case-insensitive)
- Duplicate codes are allowed (creates multiple reports)

## Support

For issues or feature requests, check:
1. Application console (F12) for JavaScript errors
2. Browser network tab for API issues
3. `sesizari.json` file integrity
4. RFID scanner keyboard emulation settings
