import ValidationField from "../../../../Components/ValidationField/ValidationField";
import { useFormikContext } from "formik";
import { useObjectToEdit } from "../../../../zustand/ObjectToEditState";
import { useTranslation } from "react-i18next";
import { QueryStatusEnum } from "../../../../enums/QueryStatus";
import { Button, Divider, Spin } from "antd";
import { MdCancel } from "react-icons/md";
import { ModalBodyProps } from "../../../../types/Sales";
import { onEnterDown } from "../../../../utils/onKeyDown";

const Form = ({ handleCloseModel = () => {} }: ModalBodyProps) => {
  const { objectToEdit } = useObjectToEdit();
  const { t } = useTranslation();

  const formik = useFormikContext();
  const { values, setFieldValue } = useFormikContext<any>();

  const handleNext = () => {
    setFieldValue("currentModalIndex", values?.currentModalIndex + 1);
  };

  const student_info = objectToEdit?.data?.data;
  const status = objectToEdit?.data?.status;
  const PackagesInfo = student_info?.packages?.map((info: any) => ({
    id: info?.id,
    name:(
    <div>
      {info.name } ( <span className={`${info?.coupon_id !== null && 'removed_sales'}`}>{info?.original_price.toLocaleString("en-US")}</span> ) 
      {info?.coupon_id !== null && <span className="new_sales"> {info?.final_price.toLocaleString("en-US")}</span>}
      </div>
      )
  }));
  const isDisabled = status === QueryStatusEnum.LOADING || !formik.dirty || !values?.phone_number
  const handleKeyDown = (e:any) =>{
    onEnterDown(e,handleNext,isDisabled)
  }
  return (
    values?.currentModalIndex == 1 && (
      <div className="w-100">
        <header className="modal_title">
          <span>{t(`models.add_sales`)} </span>
          <MdCancel onClick={handleCloseModel} />
        </header>

        <Divider />

        <div className="sales_info_modal">

          <div className="info between">
            {/* <img src="/Image/faker_user.png" alt="" /> */}
            <span className="mr-20">
              <h5>
                {student_info?.first_name + " " + student_info?.last_name}
              </h5>
              <h5>
                {t("models.course")}: <p> {student_info?.grade_name}</p>
              </h5>
            </span>
            <span className="ml-20" 
            style={{marginLeft:"20px"}}
            >
              <h5>
                {t("models.mobile_number")}: <p> {student_info?.phone_number}</p>
              </h5>
            </span>
          </div>

          <ValidationField
            placeholder="choose"
            label="package"
            name="package_id"
            type="Select"
            className="package"
            option={PackagesInfo}
            onKeyDown={handleKeyDown}
          />
        </div>

        <div className="buttons">
          <Button className="back_button pointer" onClick={handleCloseModel}>
            {t("practical.cancel")}
          </Button>
          <Button
            className="add_button"
            disabled={isDisabled}
            onClick={handleNext}
          >
            {t(`practical.sale`)}
            {status === QueryStatusEnum.LOADING && (
              <span className="Spinier_Div">
                <Spin />
              </span>
            )}
          </Button>
        </div>
      </div>
    )
  );
};

export default Form;
