# PTW Frontend

PTW admin frontend (`React + Vite`) for `ptw_backend`, mounted through unified API at `/api/ptw`.

## Run

- `npm run install-ptw-ui`
- `npm run dev-ptw-ui` (or `npm run dev` from repo root)

## API/Button Mapping (Primary)

- **Overview**
  - `Refresh` -> `GET /users`, `GET /ptw_form_details`, `GET /ptw_permits`, `GET /tickets`

- **Users & Access**
  - `Create User` -> `POST /users` (multipart, supports `photo`)
  - `Edit` -> `PUT /users/:user_id` (multipart)
  - `Delete` -> `DELETE /users/:user_id`
  - `View Photo` -> `GET /users/:user_id/photo`
  - `Assign Roles` -> `POST /user_role`
  - role `x` remove -> `DELETE /user_role/:user_role_id`
  - `Map Cities` -> `POST /admin_level_user_multi_city_map`
  - city `x` remove -> `DELETE /admin_level_user_multi_city_map/:user_id/:city_id`
  - logs table -> `GET /users_logs`

- **Form Builder**
  - `Create Form` -> `POST /ptw_form_details`
  - `Update Form` -> `PUT /ptw_form_details/:form_id`
  - `Delete Form` -> `DELETE /ptw_form_details/:form_id`
  - `Add Section` -> `POST /ptw_section_details`
  - `Add Question` -> `POST /ptw_questions`
  - `Add Options` -> `POST /ptw_question_options`
  - `Map Locations` -> `POST /ptw_location_forms_details`
  - `Map Section Order` -> `POST /ptw_form_section_map`
  - `Set Skip Logic` -> `POST /ptw_alternative_sections` (and section-question map flow)
  - `Publish Nested Form` -> `POST /form-builder`
  - logs -> `GET /ptw_form_logs`, `GET /ptw_section_logs`

- **Permits & Submissions**
  - `Create Permit` -> `POST /ptw_permits`
  - `Update Status` -> `PUT /ptw_permits/:permit_id`
  - `Delete Permit` -> `DELETE /ptw_permits/:permit_id`
  - `Create Opening Submission` -> `POST /submissions` (`submission_role=opening`)
  - `Create Closing Submission` -> `POST /submissions` (`submission_role=closing`)
  - `Upload Media` -> multipart fields `media_<question_id>`
  - `View Media` -> `GET /ptw_answers_media/:answers_id/:occurrence/file`
  - map view -> `GET /ptw_opening_closing_map`

- **Tickets & Logs**
  - `Raise Ticket` -> `POST /tickets`
  - `Update` -> `PUT /tickets/:id`
  - `Close` -> `PUT /tickets/:id` (`status_id`, `closed_at`)
  - `Delete` -> `DELETE /tickets/:id`
  - `View Ticket Logs` -> `GET /tickets_logs`

- **Master Data**
  - `Add` -> `POST /<table>`
  - `Edit` -> `PUT /<table>/:id`
  - `Delete` -> `DELETE /<table>/:id`
  - `Refresh` -> `GET /<table>`

## Permit authorization section setup

Use the **Permit Authorization** question type for the final sign-off section before form submission.

1. **Question type** (once per database)
   - Run migration `ptw_backend/sql/migrations/add_permit_authorization_question_type.sql`, or ensure `ptw_question_type` contains `Permit Authorization`.

2. **Section**
   - Create a section titled **Authorisation Acceptance** (or set `section_type_code` to `authorization`).
   - Map it to the form via **Form Management → Form–section mapping** as the **last** section with `skipable = 0`.

3. **Question**
   - In **Question Management**, create one question:
     - Text: e.g. `Permit authorization sign-off`
     - Type: **Permit Authorization**
     - `allows_media` is set automatically (8 signature blobs stored per answer)
   - Map that question to the authorization section.

4. **Fill flow**
   - The fill page renders the 8-role table (name, company, signature) plus manual permit start/expiry/task duration.
   - Answers are stored as JSON in `ptw_answers.answer_value`; signatures as `ptw_answers_media` occurrences 1–8 on the same answer row.
