import { Controller, useWatch } from "react-hook-form";
import MuiDatePicker from "@/core/components/MuiDatePicker";
import React from "react";
import moment from "jalali-moment";

const ToDateController = ({ control }) => {
    const minDate = useWatch({ control, name: "from_date" });
    return (
        <Controller
            name="date_to"
            control={control}
            render={({ field: { onChange, value }, fieldState: { error } }) => (
                <MuiDatePicker
                    name="date_to"
                    minDate={minDate}
                    error={!!error}
                    value={value}
                    label={'تاریخ تصادف (تا تاریخ)'}
                    placeholder={"تا تاریخ"}
                    setFieldValue={(name, newValue) => {
                        const formattedDate = moment(new Date(newValue)).locale("en").format("YYYY-MM-DD");
                        onChange(formattedDate); // Update the field value in react-hook-form
                    }}
                    helperText={error ? error.message : null}
                />
            )}
        />
    );
};
export default ToDateController;
