# tips.php Documentation

Purpose
- Render a responsive viewer/editor for the tips table with add, edit, and delete actions.
- Enforce local-time behavior and UI constraints for tipCreate and tipMod.

Dependencies
- PHP: connDb.php (PDO connection, $dbTable, $dbName, helpers like getAllTips).
- AJAX endpoints: addRow.php, updateRow.php, deleteRow.php.
- JS/CSS: Bootstrap 5, jQuery 3.7, DataTables + Responsive, Font Awesome.
- Safe HTML output via formatCell($value).

Server-side flow
- Reads table schema:
  - DESCRIBE tips to get column names in ordinal (table) order.
  - INFORMATION_SCHEMA for type/nullability/length into $columnSchema.
- Form column lists:
  - $columnNames: all columns in DB order.
  - $formEditableColumns: excludes primary key, created_at, updated_at (for Add form).
- Field rules:
  - tipMod: editable on add and edit; defaults to now on add.
  - tipCreate: editable on add, readonly on edit; defaults to now on add.
- Exports to JS:
  - TABLE_NAME, PRIMARY_KEY_COLUMN, EDITABLE_COLUMNS, ALL_COLUMN_NAMES, COLUMN_SCHEMA, DEFAULT_ADD_VALUES.

Client-side flow
- DataTables initialization with responsive, search, and deferred rendering.
- Loading overlay hidden on DataTables initComplete.
- Edit modal (View/Edit):
  - Fields rendered strictly in ALL_COLUMN_NAMES (DB ordinal) order.
  - Hidden inputs for PK and tableName.
  - tipCreate: readonly text; displayed using local time "YYYY-MM-DD HH:MM:SS".
  - tipMod: datetime-local input (calendar/time picker), defaulted to current local time on open.
  - Submit sends JSON to updateRow.php; toasts on success/failure.
- Add modal:
  - Inputs generated from EDITABLE_COLUMNS preserving order.
  - tipCreate and tipMod are forced to datetime-local inputs; both default to "now" in local time; both editable on add.
  - Client-side validation (Bootstrap) before JSON POST to addRow.php.
- Delete:
  - Confirm modal posts JSON to deleteRow.php, then reloads on success.

Date/time helpers (local time)
- formatLocalDateTime(Date): "YYYY-MM-DD HH:MM:SS" for display.
- formatForDatetimeLocal(Date): "YYYY-MM-DDTHH:MM" for input[type=datetime-local].
- toDatetimeLocalValue(value): converts DB-like strings to datetime-local value.
- toLocalDisplay(value): converts DB value to local "YYYY-MM-DD HH:MM:SS".

Security and formatting
- All cell content is output via formatCell to escape HTML and handle NULLs/URLs/datetimes.
- Row JSON passed via data-row-json is HTML-escaped to prevent injection.
- Favicon provided inline (data URI) to avoid 404.

Ordering rules
- Edit/Add field order is determined by DB ordinal order from DESCRIBE/INFORMATION_SCHEMA.
- No custom front-of-form block; all columns appear in schema order.

Customization
- Change Add form fields by adjusting $formEditableColumns in PHP.
- To reorder fields without DB changes, reorder ALL_COLUMN_NAMES (client) or $columnNames (server) before rendering.
- Per-column input behavior can be overridden in the JS loop using COLUMN_SCHEMA.

Troubleshooting
- primaryKeyValue undefined: resolved by deriving from parsed rowData before building hidden PK field.
- Timezone: all UI times are browser local. Ensure server normalizes to desired timezone in addRow.php/updateRow.php if needed.

Notes
- Errors are shown via Bootstrap toasts; detailed errors are logged server-side by connDb.php.
- Double-click on first table cell opens the edit modal for that row.
