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) => (
{label}
); const renderChartGrid = (type) => { const getValue = (house) => (type === "graha" ? data.graha : data.amsam)?.[house] || []; const onChange = (house, value) => handleChartChange(type, house, value); const label = type === "graha" ? "Rasi" : "Navamsam"; return (
{renderChartCell(label, getValue(1), (value) => onChange(1, value))} {renderChartCell(label, getValue(2), (value) => onChange(2, value))} {renderChartCell(label, getValue(3), (value) => onChange(3, value))} {renderChartCell(label, getValue(4), (value) => onChange(4, value))} {renderChartCell(label, getValue(5), (value) => onChange(5, value))}
Horoscope
{renderChartCell(label, getValue(6), (value) => onChange(6, value))} {renderChartCell(label, getValue(7), (value) => onChange(7, value))} {renderChartCell(label, getValue(8), (value) => onChange(8, value))} {renderChartCell(label, getValue(9), (value) => onChange(9, value))} {renderChartCell(label, getValue(10), (value) => onChange(10, value))} {renderChartCell(label, getValue(11), (value) => onChange(11, value))} {renderChartCell(label, getValue(12), (value) => onChange(12, value))}
); }; return (
Select Diet {errors.diets && (

{errors.diets}

)}
Select Hobbies & Interests {errors.hobbies && (

{errors.hobbies}

)}

Astrology / Horoscope

handleChange("dob", formatDate(value))} slotProps={{ textField: { name: "dob", fullWidth: true, error: Boolean(errors.dob), helperText: errors.dob, }, }} />
handleChange("tob", formatTime(value))} slotProps={{ textField: { name: "tob", fullWidth: true, error: Boolean(errors.tob), helperText: errors.tob, }, }} />
handleChange("placeOfBirth", e.target.value)} error={Boolean(errors.placeOfBirth)} helperText={errors.placeOfBirth} placeholder="Enter Place of Birth" variant="outlined" />

Add Rasi

{renderChartGrid("graha")} {errors.graha_duplicate && (

{errors.graha_duplicate}

)}

Add Navamsam

{renderChartGrid("amsam")} {errors.amsam_duplicate && (

{errors.amsam_duplicate}

)}
{onSkipStep && ( )} {"Duplicate Selection"} {confirmDialog.item} is already selected in House {confirmDialog.sourceHouse}. Do you want to move it to House {confirmDialog.house}?
); }; export default LifestyleDetailsForm;