import { useState } from 'react' import { Box, Button, MenuItem, TextField, ToggleButton, ToggleButtonGroup, Typography } from '@mui/material' import type { Property, PropertyUnit } from '../../domain/property' import { useUpdateUnit } from '../../hooks/useProperties' import { FIT_OUT_LABELS } from '../../lib/constants' import { suggestFutureRent } from '../../lib/rentEstimate' import { DS_BORDER, DS_TEXT } from '../../lib/ds' const NEEDS_BUILD = new Set(['SHELL', 'BASIC']) interface UnitDraft { rentPricePerSqm?: number availableFrom?: string fitOut?: string fitOutByLandlord?: boolean mieterausbaubeitragPerSqm?: number parkingSpots?: number expectedRentPerSqm?: number } interface Props { property: Property unit: PropertyUnit onClose: () => void } /** Shared per-unit editor: price, availability, fit-out, cost-bearer, MAB, parking allocation, expected price. */ export function UnitFieldsEditor({ property, unit, onClose }: Props) { const updateUnit = useUpdateUnit(property.id) const [d, setD] = useState(() => ({ rentPricePerSqm: unit.rentPricePerSqm ?? property.rentPricePerSqm, availableFrom: unit.availableFrom, fitOut: unit.fitOut ?? '', fitOutByLandlord: unit.fitOutByLandlord, mieterausbaubeitragPerSqm: unit.mieterausbaubeitragPerSqm, parkingSpots: unit.parkingSpots, expectedRentPerSqm: unit.expectedRentPerSqm, })) const effFit = d.fitOut || property.hardFacts?.fitOut const needsBuild = NEEDS_BUILD.has(effFit ?? '') const suggestion = suggestFutureRent(property.location?.city ?? '', d.rentPricePerSqm ?? unit.rentPricePerSqm ?? property.rentPricePerSqm) function save() { const fit = (d.fitOut || undefined) as 'SHELL' | 'BASIC' | 'FULL' | 'PREMIUM' | undefined const nb = fit === 'SHELL' || fit === 'BASIC' updateUnit.mutate({ unitId: unit.id, data: { rentPricePerSqm: d.rentPricePerSqm, availableFrom: d.availableFrom || undefined, fitOut: fit, fitOutByLandlord: nb ? d.fitOutByLandlord : undefined, mieterausbaubeitragPerSqm: nb && d.fitOutByLandlord === false ? d.mieterausbaubeitragPerSqm : undefined, parkingSpots: d.parkingSpots, expectedRentPerSqm: d.expectedRentPerSqm, }, }) onClose() } return ( setD(p => ({ ...p, rentPricePerSqm: parseInt(e.target.value) || undefined }))} slotProps={{ htmlInput: { min: 0, step: 10 } }} helperText="Leer = Objekt-Preis" /> {unit.available && ( setD(p => ({ ...p, availableFrom: e.target.value }))} helperText="Leer = sofort" /> )} setD(p => ({ ...p, fitOut: e.target.value }))} > Wie Objekt{property.hardFacts?.fitOut ? ` (${FIT_OUT_LABELS[property.hardFacts.fitOut] ?? property.hardFacts.fitOut})` : ''} {FIT_OUT_LABELS.SHELL} {FIT_OUT_LABELS.BASIC} {FIT_OUT_LABELS.FULL} {FIT_OUT_LABELS.PREMIUM} setD(p => ({ ...p, parkingSpots: parseInt(e.target.value) || undefined }))} slotProps={{ htmlInput: { min: 0 } }} helperText={property.hardFacts?.parking != null ? `aus Objekt-Pool: ${property.hardFacts.parking}` : 'Leer = Objekt-Wert'} /> setD(p => ({ ...p, expectedRentPerSqm: parseInt(e.target.value) || undefined }))} slotProps={{ htmlInput: { min: 0, step: 10 } }} helperText={suggestion ? `Vorschlag (indexiert): CHF ${suggestion}/m² — leer = heutiger Preis` : 'Leer = heutiger Preis'} /> {needsBuild && ( Wer baut aus? { if (v) setD(p => ({ ...p, fitOutByLandlord: v === 'landlord', ...(v === 'landlord' ? { mieterausbaubeitragPerSqm: undefined } : {}) })) }} > Vermieter Mieter {d.fitOutByLandlord === false && ( setD(p => ({ ...p, mieterausbaubeitragPerSqm: parseInt(e.target.value) || undefined }))} slotProps={{ htmlInput: { min: 0, max: 1000, step: 25 } }} /> )} leer = wie Objekt )} ) }