import { Form, Input, InputNumber } from "antd";
import React from "react";
import useFormField from "../../../Hooks/useFormField";
import { MdOutlineEdit } from "react-icons/md";
import { Field } from "formik";
import { ValidationFieldPropsInput } from "../utils/types";

const NumberField = ({
  name,
  label,
  placeholder,
  isDisabled,
  onChange,
  type,
  no_label,
  label_icon,
  ...props
}: ValidationFieldPropsInput) => {
  const { errorMsg, isError, t, formik } = useFormField(name, props);

  const handleChange = (
    e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
  ) => {
    console.log("Change:", e);
    formik.setFieldValue(name, e);
  };

  return (
    <div className="ValidationField w-100">
      {no_label ? (
        <label htmlFor={name} className="text">
          <span>empty</span>
        </label>
      ) : label_icon ? (
        <div className="LabelWithIcon">
          <label htmlFor={name} className="text">
            {t(`input.${label ? label : name}`)}
          </label>
          <MdOutlineEdit size={22} style={{ color: "#A098AE" }} />
        </div>
      ) : (
        <label htmlFor={name} className="text">
          {t(`input.${label ? label : placeholder ? placeholder : name}`)}
        </label>
      )}

      <Form.Item
        hasFeedback
        validateStatus={isError ? "error" : ""}
        help={isError ? errorMsg : ""}
      >
        <Field
          as={InputNumber}
          type={type ?? "text"}
          placeholder={t(
            `input.${placeholder ? placeholder : label ? label : name}`,
          )}
          name={name}
          disabled={isDisabled}
          size="large"
          onChange={handleChange}
          {...(type === "number" && { min: 0 })}
          {...props}
        />
      </Form.Item>
    </div>
  );
};

export default React.memo(NumberField);
