> **SUPERSEDED — HISTORICAL RECORD ONLY (archived 2026-07-18).** This document is part of the original "superpowers" wayleave build, whose design diverged from the projects-module patterns it should have mirrored. Do NOT use it as a pattern source for wayleave or any new module. The wayleave module is being realigned to the projects module (meta-driven fields, OpenLayers, shared JS) — see CLAUDE.md.

# Wayleave V2 Refactor — Change Manifest

Companion to `2026-04-22-wayleave-module-v2-refactor.md`.
Completed 2026-04-24. Every change below was verified in-browser by the user at its task checkpoint.

---

## Created (9)

| File | Purpose |
|---|---|
| `sql/wayleave_06_v2_refactor.sql` | `wayleave.teams` lookup table + seeded starter teams. |
| `www/html/html_body_wayleaveedit.php` | V2 editor body template (header bar + horizontal-nav + content-panel pattern). |
| `www/html/html_body_wayleave_list.php` | V2 list body template (matches projectlist / stocklist_list layout). |
| `www/css/wayleave_edit_v2.css` | V2 editor styles — content-panel toggle, pending-list, source tags, map sidebar + buttons, file-icon, toast popup. |
| `www/js/wayleave_edit_v2.js` | V2 editor core: nav, static/dynamic fields, section tabs, save, toast, autocomplete rebind, OpenLayers coverage map (draw/edit/delete), read-only OL map, coverage tab (polygons + pending + direct UPRNs + stocklists + titles), audit log, attachments loader, layer-manager helper. |
| `www/js/wayleave_edit_v2_fileuploads.js` | V2 attachments module — modal open, upload with progress, download, delete (kept but button hidden), file-type icon map. |
| `www/fn/wayleave_audit_log_load.php` | Prev/new value audit log endpoint — UNION across `agreements_history` + 5 per-type value history tables with `LAG()` + `agreement_journal` rows. |
| `www/fn/wayleave_file_delete.php` | Soft-delete attachment endpoint on `public.file_uploads` (kept alive for future re-wiring; button currently hidden per feedback). |
| `docs/superpowers/plans/2026-04-22-wayleave-module-v2-refactor.md` | The implementation plan this session executed against. |

## Modified (7)

| File | Change |
|---|---|
| `www/fn/global_functions.php` | `wayleaveedit` route flipped to V2 (v2 header/nav/html/css/js, version `2`). `wayleave` list route flipped to V2 (v2 header/nav, new `html_body_wayleave_list.php`, version `2`). |
| `www/fn/wayleave_load.php` | Joins `users.users`, `accounts.accounts`, self-join for parent agreement to return `bd_manager_name` / `account_name` / `parent_agreement_name`. Adds `teams` lookup. Dynamic values keyed by `field_form_id` (plus `values_by_fid` legacy mirror). Returns `files` array from `wayleave.vw_file_uploads`. Returns `attached_stocklists` with names via `stocklists.stocklists` join. |
| `www/fn/wayleave_projects_load.php` | Rewritten from UPRN-based join on `projects.project_premises` (unpopulated in this deployment) to ST_Intersects spatial join on `wayleave.agreement_polygons ⟷ projects.projects.geom`, returning percent-of-project-area overlap. |
| `www/fn/autocomplete.php` | Added `wayleavename` (agreement autocomplete for Parent Agreement input) and `landregtitle` (title-number autocomplete from `landregistry.ccod` for the Coverage→Titles sub-panel). |
| `www/fn/project_save.php` | Fixed pre-existing schema-offset bug in the `INSERT INTO projects.projects_history SELECT * FROM projects.projects` call — now names every column explicitly and supplies `'UPDATE'` / `now()` / `:history_user` for the audit columns. (Unrelated to wayleave; uncovered during Task 8 regression testing.) |
| `www/js/main.js` | Wrapped the 7 `data-autocomplete` bindings (usernames / companyname / accountname / projectname / stocklistname / wayleavename / strategyanalysistabels / landregtitle) in `window.bindAutocompleteInputs` with `:not(.ac-bound)` idempotency guards, called once at DOMReady. The V2 editor re-calls the function after dynamically rendering its autocomplete inputs. |
| `unusedfiles.md` | Added "Retired 2026-04-24 — Wayleave V2 Refactor" section documenting the six deleted files and their replacements. |

## Deleted (6)

| File | Reason |
|---|---|
| `www/html/wayleave_edit.php` | Replaced by `html_body_wayleaveedit.php`. |
| `www/js/wayleave_edit.js` | Replaced by `wayleave_edit_v2.js` + `wayleave_edit_v2_fileuploads.js`. |
| `www/css/wayleave_edit.css` | Replaced by `wayleave_edit_v2.css`. |
| `www/html/wayleave.php` | Replaced by `html_body_wayleave_list.php` (V2 layout). |
| `www/fn/wayleave_releases_load.php` | Releases tab retired — release state inferred from agreement status. |
| `www/fn/wayleave_releases_save.php` | See above. |

## Database changes

One migration script (run by user): `sql/wayleave_06_v2_refactor.sql`. Creates:

- `wayleave.teams (id, team_name, display_order, is_active, created_datetime)` — seeded with any distinct values already in `wayleave.agreements.wayleave_team`, plus starter rows: Internal / External / Legal / Property.

No other schema changes. The existing `wayleave.agreements.wayleave_team varchar(100)` column stays as-is — the dropdown stores the `team_name` string so `wayleave_save.php`'s history logging continues to work unchanged.

## Feedback coverage — `2026-04-22-wayleave-module-feedback.md`

| Item | Status | Implemented in |
|---|---|---|
| V1 → V2 layout (header / nav / body) | ✅ | T2, T5, T6 + list-page follow-up |
| File upload matches V2 pattern | ✅ | T9 |
| Journal log matches V2 pattern | ✅ | T10 |
| Title linking on Coverage tab + autocomplete | ✅ | T5 (markup), T11 (`landregtitle` + behaviour) |
| Dropdowns populate from saved data | ✅ | T3 (values-by-form-id) + T7 (`populateAllValues`) |
| BD Manager / Account / Parent / Stocklist → text-search + hidden id | ✅ | T3 (label joins), T4 (`wayleavename`), T7 (`autocompleteField`), T11 (stocklist autocomplete) |
| Field category = new tab | ✅ | T7 (`renderSectionTabsAndPanels`) |
| Premise/project tabulators filterable | ✅ | T12 (`headerFilter: 'input'`) |
| Wayleave team = dropdown | ✅ | T1 (lookup) + T3 (return list) + T7 (render as `<select>`) |
| Journal log includes previous/new values | ✅ | T10 |
| Map = viewer + editor + reference info | ✅ | T11 (OL editor) + T13 (OL read-only viewer) + slim layer manager on both |

## Decisions and follow-ups

Noted during this session, not implemented:

- **OpenLayers migration.** Wayleave v1 used Leaflet-Geoman; v2 switched both maps to OpenLayers per user direction. Fresh decision, not in original plan.
- **Releases tab removed.** User confirmed release status is implicit in agreement status; no duplicate workflow needed.
- **Delete attachment disabled (UI).** Endpoint + client function kept in place; the button is commented out in the card template. Re-wire by uncommenting the trash button in `wayleave_edit_v2.js` → `renderAttachments()`.
- **Map layer manager — slim port.** Only `tile`/`xyz`/`wms` layer types are consumed; vector project-layer types intentionally skipped. A future ticket should extract `map_v5.js`'s layer logic into a shared module for true parity (Option 2 from the Task 13 conversation).
- **Projects overlap query** uses `ST_Intersects` against `projects.projects.geom`. Once projects get a first-class polygon editor (mentioned as future work), the overlap signal will become more reliable.

## Verification trail

Every task ended with a manual in-browser verification step that the user signed off on:

- T1 SQL migration run + verified.
- T2 placeholder render + asset 200s.
- T3 JSON payload keys confirmed (after OPcache flush).
- T4 autocomplete JSON returns ≤15 agreements.
- T5 V2 skeleton renders (8 tabs).
- T6 styles apply; hidden panels use `display: none`.
- T7 header populates, dropdowns show saved values, autocompletes resolve to labels, section tabs appear, save persists.
- T8 autocompletes still wire on project/account/wayleave editors; shared file regression-free (project-save PDO bug surfaced and fixed).
- T9 upload modal + card list + download round-trip; delete hidden per user request.
- T10 audit log shows prev/new values with headerFilter on each column; static-column rows map to friendly labels.
- T11 OL coverage map: draw/edit/delete polygons + approved stocklist names + title autocomplete + toast restyled to match projectedit.
- T12 tabulator headerFilters on Premises / Projects; Releases tab removed; spatial overlap query returns expected rows.
- T13 OL read-only map: polygons + premise points + popup + layer-manager sidebar + buttons inside map container + sidebar styled to match `custom.css#sidebarLayers`.
- T14 V1 editor + list files deleted; no 404s; sibling wayleave routes unaffected.
