import React, { useEffect, useMemo, useRef, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { updateLifestyleDetails } from "../redux/registrationFormSlice"; import { Grid, FormControl, InputLabel, Select, MenuItem, Button, TextField, Checkbox, ListItemText, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, } from "@mui/material"; import { LocalizationProvider } from "@mui/x-date-pickers"; import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import { TimePicker } from "@mui/x-date-pickers/TimePicker"; import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns"; import { useLifestyleMasters } from "../hooks/useMasters"; import horoscopeImg from "../assets/images/horoscopeimg.png"; const LifestyleDetailsForm = ({ onSubmitStep, onSkipStep, errors, onFieldChange, }) => { const dispatch = useDispatch(); const data = useSelector((state) => state.registerform.lifestyleDetails); const inputRef = useRef(null); const requiredMark = *; const { data: lifestyleMasters, isLoading: isLifestyleMastersLoading } = useLifestyleMasters(); const dietOptions = useMemo(() => { const raw = lifestyleMasters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.diet || raw.diets || []; }, [lifestyleMasters]); const hobbyOptions = useMemo(() => { const raw = lifestyleMasters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.hobbies || raw.hobby || []; }, [lifestyleMasters]); const grahaOptions = useMemo(() => { const raw = lifestyleMasters; if (!raw) return []; if (Array.isArray(raw)) return raw; return raw.grahas || raw.graha || []; }, [lifestyleMasters]); const [confirmDialog, setConfirmDialog] = useState({ open: false, type: "", house: null, item: "", sourceHouse: null, pendingValue: [], }); useEffect(() => { inputRef.current?.focus(); }, []); const handleChange = (field, value) => { dispatch(updateLifestyleDetails({ [field]: value })); if (onFieldChange) onFieldChange(field); }; const handleMultiChange = (field, value) => { const nextValue = Array.isArray(value) ? value : String(value).split(","); dispatch(updateLifestyleDetails({ [field]: nextValue })); if (onFieldChange) onFieldChange(field); }; const handleChartChange = (type, house, newValue) => { const currentChart = type === "graha" ? data.graha : data.amsam; const currentHouseValue = currentChart?.[house] || []; // Find added items const addedItems = newValue.filter((item) => !currentHouseValue.includes(item)); if (addedItems.length > 0) { for (const addedItem of addedItems) { for (const [otherHouse, items] of Object.entries(currentChart || {})) { if (String(otherHouse) !== String(house) && items && items.includes(addedItem)) { setConfirmDialog({ open: true, type, house, item: addedItem, sourceHouse: otherHouse, pendingValue: newValue, }); return; } } } } // No duplicates, proceed with update const updates = {}; const nextChart = { ...(currentChart || {}) }; nextChart[house] = newValue; if (type === "graha") { updates.graha = nextChart; } else { updates.amsam = nextChart; } dispatch(updateLifestyleDetails(updates)); if (onFieldChange) onFieldChange(type); }; const handleConfirmMove = () => { const { type, house, item, sourceHouse, pendingValue } = confirmDialog; const currentChart = type === "graha" ? data.graha : data.amsam; const nextChart = { ...(currentChart || {}) }; // Remove from source house if (nextChart[sourceHouse]) { nextChart[sourceHouse] = nextChart[sourceHouse].filter((i) => i !== item); } // Add to target house nextChart[house] = pendingValue; const updates = type === "graha" ? { graha: nextChart } : { amsam: nextChart }; dispatch(updateLifestyleDetails(updates)); if (onFieldChange) onFieldChange(type); setConfirmDialog({ ...confirmDialog, open: false }); }; const handleCancelMove = () => { setConfirmDialog({ ...confirmDialog, open: false }); }; const handleSubmit = () => { console.log("Submitting lifestyle details:", data); onSubmitStep(); }; const parseDateValue = (value) => { if (!value) return null; const date = new Date(value); return Number.isNaN(date.getTime()) ? null : date; }; const parseTimeValue = (value) => { if (!value || typeof value !== "string") return null; const [hours, minutes] = value.split(":").map(Number); if (Number.isNaN(hours) || Number.isNaN(minutes)) return null; const date = new Date(); date.setHours(hours, minutes, 0, 0); return date; }; const formatDate = (value) => { if (!(value instanceof Date) || Number.isNaN(value)) return ""; const y = value.getFullYear(); const m = String(value.getMonth() + 1).padStart(2, "0"); const d = String(value.getDate()).padStart(2, "0"); return `${y}-${m}-${d}`; }; const formatTime = (value) => { if (!(value instanceof Date) || Number.isNaN(value)) return ""; const h = String(value.getHours()).padStart(2, "0"); const m = String(value.getMinutes()).padStart(2, "0"); return `${h}:${m}`; }; const renderChartCell = (label, value, onChange) => (