# Task 07 — Vihar Detail (Captain View)

Captain's view of a single vihar — read all fields, see allocations, take actions.

## File to create

```
apps/web/src/app/captain/vihars/[id]/page.tsx
```

## Layout

### Header card
- Status pill (top right)
- Date + time (display)
- Head saint + samuday
- Counts: Sadhuji / Sadhviji / Other
- Toggles status: Temple Darshan, Updhi (read-only icons)
- Remarks (if any)

### Route card
- "From: {departure name}" with formatted address
- "To: {arrival name}" with formatted address
- Walking distance (planned + actual if available)
- Walking duration (planned)

### Allocations card
- List of allocated volunteers. Each row:
  - Name + locality
  - Status: "Not checked in" / "Checked in at 5:32" / "Checked out · 5.4 km"
  - "Distance approval pending" badge if applicable
  - Action menu (kebab): Reassign, Mark check-out on behalf, Approve distance
- "+ Add more volunteers" button if vihar is still planned/in_progress

### Actions footer
- "Re-share on WhatsApp" — links to share sheet (Task 05)
- "Cancel Vihar" — opens dialog asking for reason, POSTs to `/api/vihars/{id}/cancel`
- "Close Manually" — opens dialog with start/end time + distance, POSTs to `/api/vihars/{id}/close-manual`
  Only shown if status is `planned` or `in_progress`
- "Reopen" — only shown for `completed`/`auto_closed` within 24h. POSTs to `/api/vihars/{id}/reopen`

## Data sources

```
GET /api/vihars/{id}        # full detail with allocations
```

For approving distance:
```
POST /api/allocations/{allocationId}/approve-distance
{ approvedKm: 5.2, decision: 'approved' }
```

For check-out on behalf:
```
POST /api/allocations/{allocationId}/check-out-on-behalf
{ latitude?: ..., longitude?: ..., startTime: '05:30', distanceKm: 5.2, notes?: '...' }
```

For reassigning:
```
DELETE /api/allocations/{allocationId}    # soft-delete the active one
POST   /api/vihars/{id}/allocations       # add the replacement
{ userIds: [newUserId] }
```

## Acceptance

1. Open a vihar from the list. All fields display correctly.
2. See allocated volunteers with their check-in/out states.
3. Reassign a volunteer — old row deactivates, new row appears.
4. Mark check-out on behalf for a volunteer who didn't show up.
5. Approve a pending distance — badge clears.
6. Cancel a planned vihar with reason — status flips, redirect back to list.
7. Close manually a planned vihar — fields filled, status = completed.
8. Reopen a completed vihar within 24h — back to in_progress.

## Gotchas

- Distance approval dialog should pre-fill with the volunteer's submitted km;
  captain can override.
- Reassignment is two API calls (DELETE + POST). If POST fails, the DELETE
  has already happened. Wrap in a confirmation dialog so user only triggers
  it intentionally.
- "Close Manually" should validate end > start time before submit.
- Reopen window is enforced server-side; the button just hides itself client-side
  if updatedAt > 24h ago.
- The kebab menu actions are role-checked client-side too — only show what
  applies based on current state.

## Out of scope

- Editing the vihar's core fields (date, route, counts) post-creation —
  Phase 2 (need partial update endpoint, currently stub)
- GPS trail visualization on a map — Phase 2
- Per-volunteer audit trail — Phase 2
