> **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 V3 Updates — Change Manifest

Companion to `2026-04-24-wayleave_module-v3-updates.md`.
Completed 2026-04-27. Every change below was verified in-browser by the user at its task checkpoint.

---

## Created

None. All changes are modifications to existing files; no new PHP endpoints, JS modules, or SQL migration scripts were required.

---

## Modified (7)

| File | Change |
|---|---|
| `www/fn/wayleave_load.php` | Extended `$sqlCounts` with `af` CTE (`ST_Union` of polygons) and `all_uprns` CTE (plain `UNION` across all four sources) to return `premise_count` and `project_overlap_count` on every load. Title source added to `all_uprns` via `basedata.abp.title_no` join. Bind loop extended to 12 params. |
| `www/fn/wayleave_premises_load.php` | Added 4th source CTE `title_uprns` (JOIN `wayleave.agreement_landregistry → basedata.abp.title_no`). Fixed pending-removal bug: `poly_uprns` filter changed from `is_assigned=true AND is_approved=true` to `is_approved=true` so pending-removal UPRNs remain in the confirmed list until approved. |
| `www/fn/wayleave_map_load.php` | Added `title` source branch to the `resolved` CTE (`:a5`). Same pending-removal fix: polygon branch filter changed to `is_approved=true`. |
| `www/fn/wayleave_audit_log_load.php` | Static-column history branch rewrites FK values to labels using correlated subqueries with `NULLIF` guard (status, BD manager, account, parent agreement). Dynamic text-history branch restructured as a subquery to materialise `LAG()` before the outer query joins `agreement_field_dropdown_options` twice (once for current value, once for previous) — resolves dropdown option values to labels. |
| `www/html/html_body_wayleaveedit.php` | Coverage tab button and panel removed. Premises tab restructured as Bootstrap 2-col layout (`col-lg-7` table / `col-lg-5` side panels): Pending Approval + Direct UPRNs + Stocklists + Linked Titles. Dual badges added to Premises nav button (`#wl-premise-count-badge` + `#wl-premises-pending-badge`). Badge spans added to Projects and Attachments nav buttons. Header Save button added to `.project-actions`. Map tab updated: `#wl-map-buttons` div replaced with `#wl-map-buttons.wl-map-button-box` glass panel containing Draw, Delete, and inline Yes/No confirm buttons. `#wl-polygon-summary` strip added below map. |
| `www/js/wayleave_edit_v2.js` | **Task 3:** `wireSave()` drops inline-save handler; `wireNav()` toggles `#wl-header-save` visibility by form-tab test; `renderSectionTabsAndPanels()` drops per-section save button. **Task 4:** Coverage IIFE replaced by Premises Side Panels IIFE (exposes `WL.loadPremisesSidePanels` / `WL.refreshPending`); Map IIFE extended with `ol.interaction.Select/Modify/Draw`, `savePolygon`, `startDraw`, `deleteSelected`, `wireMapButtons`, cookie restore/save via `getCookie/setCookie`. `updateBadges()` retargeted to `#wl-premises-pending-badge`. **Task 5:** `renderHeader()` uses `state.counts.premise_count` and populates all four nav badges on load; `WL.loadProjects` and `WL.loadFiles` write their badges on tab-load. **Task 6:** `createMapLayerControls` iterates categories in `entity → reference → base` fixed order with proper labels; single `pointLayer`/`viewPointSource` replaced with four per-source entity layers (`srcPolygon/Direct/Stocklist/Title`), each with distinct colour (blue/teal/amber/purple) and released-green override; entity layers registered in the sidebar with checkboxes; singleclick popup tests all four entity layers. **Tasks 7 + 8:** `wireMapButtons()` implements three-button delete-confirm swap; `selectInteraction` resets confirm state on deselect. |
| `www/css/wayleave_edit_v2.css` | `.wl-map-buttons` selector renamed to `.wl-map-button-box`; glass styling added (`rgba(255,255,255,0.4)` + `backdrop-filter` matching `.wl-map-sidebar`). `.wl-map-button-box .btn { width: 100% }` added so buttons fill the panel. |

---

## Database changes

None. The title→UPRN derivation uses a read-time JOIN against `basedata.abp.title_no`. An index on `basedata.abp(title_no)` was added manually by the user (reducing the title-source query from 28 s to 0.4 s); no DDL file was produced for this as it is a plain index on an existing column in a legacy schema.

---

## Bug fixes

| Bug | Root cause | Fix |
|---|---|---|
| Premises removed from confirmed list immediately on polygon shrink | `poly_uprns` CTE filtered `is_assigned=true AND is_approved=true`, excluding pending-removal rows (`is_assigned=false, is_approved=true`) | Changed to `is_approved=true` across `wayleave_premises_load.php`, `wayleave_map_load.php`, and `wayleave_load.php` |
| `#wl-hdr-premise-count` showed `~N` (direct count only) on load | `renderHeader()` read `direct_uprn_count` instead of the new `premise_count` | `renderHeader()` now reads `state.counts.premise_count` |

---

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

| Item | Status | Task |
|---|---|---|
| Bug — `#wl-hdr-premise-count` populate on page load | ✅ | T1 + T5 |
| Bug — `#wl-premise-count-badge` populate on page load | ✅ | T1 + T5 |
| Bug — Journal log shows raw FK ids | ✅ | T2 |
| Bug — Linked Titles must also contribute UPRNs | ✅ | T1 |
| Bug — Save button moves to header, gated by form tab | ✅ | T3 |
| Unresolved — Remove Coverage tab; redistribute sub-panels | ✅ | T4 |
| Unresolved — Layer sidebar ordering (entity / reference / base) | ✅ | T6 |
| Unresolved — Map buttons in a visible container | ✅ | T7 |
| Unresolved — Polygon delete confirmation | ✅ | T8 |
| Unresolved — Entity wayleave layers | ✅ | T6 |
| New feature — Projects + Attachments tab badges | ✅ | T5 |

---

## Decisions and follow-ups

Noted during this session, not implemented:

- **Title-UPRN snapshot table.** Title-derived UPRNs are computed live via `basedata.abp.title_no` JOIN. Removing a linked title instantly drops every UPRN it contributed. A future ticket could write these to a `wayleave.agreement_title_uprns` snapshot table for persistence. User confirmed live derivation is acceptable for now.
- **Entity layer colours.** Polygon-source UPRNs: `#0d6efd` (blue); direct: `#20c997` (teal); stocklist: `#ffc107` (amber); title: `#6f42c1` (purple); released override: `#198754` (green). Agreed implicitly; no legend added yet.
- **`basedata.abp.title_no` index.** Added manually by user; not captured in a SQL migration file. Should be documented in a future DDL housekeeping script.

## Verification trail

- T1 + T2: All working — user confirmed after task checkpoint.
- T3: All working — user confirmed after task checkpoint.
- T4: All working — user confirmed after task checkpoint (bug in pending-removal logic surfaced and fixed before sign-off).
- T5–T9: User confirmed "proceed" at each checkpoint.
