import { useEffect, useMemo, useState, type ReactNode } from 'react'; import { SectionTitle, sectionFrame } from '../../components/SectionTitle'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { Alert, Autocomplete, Box, Button, Checkbox, CircularProgress, Dialog, DialogActions, DialogContent, Avatar, DialogTitle, FormControlLabel, Paper, Stack, Tab, Tabs, TextField, createFilterOptions, } from '@mui/material'; import { useAuth } from '../../auth/AuthContext'; import { useLabel, useT, useUi } from '../../i18n'; import { useNotify } from '../../notify/NotifyProvider'; import { ApiError, rpc, type Envelope } from '../../api/client'; import { getExtraFields, getView, runAction, type ExtraSpec, type Option } from '../../api/entity'; import { fetchRecaptchaSiteKey } from '../../api/session'; import { useRecaptcha } from '../../auth/recaptcha'; import { fromInputDate, resolveFields, toDisplayDate, toInputDate, toPayload, type Mode, type ResolvedField } from './fields'; import { CONFIRM_ENTITIES, CREATE_DEFAULTS, CREATE_HIDDEN, CREATE_REQUIRED, EDIT_HIDDEN_FIELDS, aliasSource, EDIT_HIDDEN_SECTIONS, FLEET_ENTITIES, HIDDEN_CREATE_TITLES, NON_OMANI_ONLY, getLayout, type DynamicLookup } from './layouts'; import { AttachmentField } from './AttachmentField'; import { DateField } from '../../components/DateField'; import { ExtraFieldsDialog } from './ExtraFieldsDialog'; import { deleteAttachment, type PendingFile, type StoredAttachment } from '../../api/attachments'; type Values = Record; /** The Riyada card document field per entity, and the section that carries the tick box (the manager's info for a company). */ const RIYADA_DOC: Record = { OWNERS: 'OWNER_RIYADAH_CARD_HOLDER_DOC', COMPANIES: 'RIYADAH_CARD_HOLDER_DOC' }; const RIYADA_SECTION: Record = { OWNERS: 'OWNERSINFO', COMPANIES: 'MANAGER_INFORMATION' }; const PROFILE_SECTIONS = [ { title: 'MAININFO', fields: ['NAME', 'EMAIL', 'LOGIN', 'MOBILE', 'AREA_CODE', 'JOB_TITLE', 'LAST_LOGIN_DATE'] }, { title: 'SETTING', fields: ['LANGUAGE'] }, ]; /** Scroll a field into view and focus its input (fields are wrapped with data-field=NAME). */ function focusField(name: string) { requestAnimationFrame(() => { const el = document.querySelector(`[data-field="${name}"]`); el?.scrollIntoView({ behavior: 'smooth', block: 'center' }); el?.querySelector('input, textarea')?.focus({ preventScroll: true }); }); } // Plate codes follow the document nationality: Omani ('OMN') picks from a dropdown (`select`), any other // nationality types the code as free text (`text`). Legacy flips the same pair on every one of these pages. const PLATE_NATIONALITY_FIELDS = ['LICENCE_NATIONALITY', 'FLEET_NATIONALITY', 'FLEET_NATIONALITY_CODE', 'TRUCK_NATIONALITY', 'NATIONALITY_CODE']; const PLATE_RULES: { select: string; text: string; nat?: string[] }[] = [ { select: 'FLEET_PLATE_CODE', text: 'PLATE_CODE_AR' }, // fleet pages, blacklists, RFID tags, violations, fleet request (truck) { select: 'FLEET_PLATE_CODE', text: 'PLATE_CODE' }, // trouble shooting { select: 'PLATE_CODE', text: 'PLATE_CODE_AR', nat: ['TRUCK_NATIONALITY'] }, // rented truck { select: 'FLEET_PLATE_CODE', text: 'FLEET_PLATE_CODE_AR' }, // visitors permits { select: 'TRAILER_PLATE_CODE', text: 'TRAILER_PLATE_CODE_AR' }, // fleet request (trailer) ]; // fleet pages: "the vehicle belongs to the stakeholder" tick box. Ticked, the owner is the stakeholder itself (the // backend fills the details in), so those fields are locked and the personal document row is hidden. const OWNER_LOCKED = ['OWNER_NAME', 'NATIONALITY_CODE', 'GOVERNORATE_CODE', 'STATE_CODE', 'CITY_CODE', 'ADDRESS']; const OWNER_DOC = ['PERSONAL_DOCUMENT_TYPE', 'PERSONAL_DOC_NUMBER', 'NATIONALITY_LICENCE']; const PLATE_CODE_NAMES = /(^|_)(FLEET_)?PLATE_CODE(_AR)?$/; const NUMBER_WITH_AREA = new Set(['PHONE_NUMBER', 'FAX_NUMBER', 'MOBILE_NUMBER']); const NAME_PARTS = ['FIRST_NAME', 'FATHER_NAME', 'GRANDFATHER_NAME', 'FAMILY_NAME']; /** * Like the legacy pages: a phone/fax/mobile number and its area code share one cell, and a person's * name parts sit together in one row, instead of each field taking a grid cell of its own. */ function groupFields(list: ResolvedField[]): { kind: 'one' | 'pair' | 'names'; fields: ResolvedField[] }[] { const out: { kind: 'one' | 'pair' | 'names'; fields: ResolvedField[] }[] = []; for (let i = 0; i < list.length; i++) { const f = list[i]; if (NAME_PARTS.includes(f.name)) { let j = i; while (j < list.length && NAME_PARTS.includes(list[j].name)) j++; if (j - i > 1) { out.push({ kind: 'names', fields: list.slice(i, j) }); i = j - 1; continue; } } const next = list[i + 1]; if (NUMBER_WITH_AREA.has(f.name) && next && /AREA_CODE$/.test(next.name)) { out.push({ kind: 'pair', fields: [f, next] }); i++; continue; } // a plate number and its plate code share one cell, number first, as on the legacy fleet pages if (/(^|_)PLATE_NUMBER$/.test(f.name) && next && PLATE_CODE_NAMES.test(next.name)) { out.push({ kind: 'pair', fields: [f, next] }); i++; continue; } out.push({ kind: 'one', fields: [f] }); } return out; } // Lists such as the 8k stakeholders would freeze a plain select; show at most 100 matches. const filter = createFilterOptions