"use client"; import { useState, useRef, useEffect } from "react"; import { TextField, Popover, IconButton, Box } from "@mui/material"; import moment from "moment-jalaali"; import ChevronLeftIcon from "@mui/icons-material/ChevronLeft"; import ChevronRightIcon from "@mui/icons-material/ChevronRight"; moment.loadPersian({ dialect: "persian-modern", usePersianDigits: false }); const WEEKDAYS = ["ش", "ی", "د", "س", "چ", "پ", "ج"]; const MONTHS = [ "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", ]; function JalaliDatePicker({ value, onChange, label, placeholder = "انتخاب تاریخ", error = false, helperText = "", disabled = false, className = "", textFieldProps = {}, disabledDates = null, nextIcon = null, prevIcon = null, dayLabelRender = null, isFirst = false, }) { const [anchorEl, setAnchorEl] = useState(null); const [selectedDate, setSelectedDate] = useState( value ? moment(value) : moment() ); const [displayDate, setDisplayDate] = useState( value ? moment(value) : moment() ); const nextIconRef = useRef(null); const prevIconRef = useRef(null); useEffect(() => { if (value) { const newDate = moment(value); setSelectedDate(newDate); setDisplayDate(newDate); } }, [value]); const handleClick = (event) => { if (!disabled) { setAnchorEl(event.currentTarget); } }; const handleClose = () => { setAnchorEl(null); }; const open = Boolean(anchorEl); const handleDateClick = (day) => { const newDate = moment(displayDate) .jDate(day) .jMonth(displayDate.jMonth()) .jYear(displayDate.jYear()); if (disabledDates && disabledDates(newDate)) { return; } setSelectedDate(newDate); if (onChange) { onChange({ _d: newDate.toDate() }); } handleClose(); }; const handlePrevMonth = () => { setDisplayDate(moment(displayDate).subtract(1, "jMonth")); }; const handleNextMonth = () => { setDisplayDate(moment(displayDate).add(1, "jMonth")); }; const getDaysInMonth = () => { const year = displayDate.jYear(); const month = displayDate.jMonth(); const daysInMonth = moment.jDaysInMonth(year, month); const firstDayOfMonth = moment([year, month, 1], "jYYYY/jMM/jDD").day(); const days = []; const startOffset = (firstDayOfMonth + 1) % 7; for (let i = 0; i < startOffset; i++) { days.push(null); } for (let day = 1; day <= daysInMonth; day++) { days.push(day); } return days; }; const formatDisplayValue = () => { if (!selectedDate) return ""; return selectedDate.format("jYYYY/jMM/jDD"); }; const days = getDaysInMonth(); return (