import React, { useEffect } from "react";
import { Choice } from "../../../../../../types/Item";
import { useFormikContext } from "formik";
import { useTranslation } from "react-i18next";
import { getCharFromNumber } from "../../../../../../utils/getCharFromNumber";
import TextField from "./TextField";
import { useObjectToEdit } from "../../../../../../zustand/ObjectToEditState";
import ImageBoxField from "../../../../../../Components/CustomFields/ImageBoxField/ImageBoxField";
import { GoTrash } from "react-icons/go";
import { Popconfirm } from "antd";

const QuestionFIeld = ({ index, data }: { index: number; data: Choice }) => {
  const formik = useFormikContext<any>();
  const { setDeletedQuestions, DeletedQuestions } = useObjectToEdit();

  const [t] = useTranslation();
  useEffect(() => {
    setDeletedQuestions([]);
  }, [window?.location.pathname]);

  const handleDeleteQuestion = () => {
    const DeleteQuestionId = formik.values.Questions?.[index];
    if (DeleteQuestionId?.id) {
      setDeletedQuestions([...DeletedQuestions, DeleteQuestionId]);
    }
    const updatedAnswers = formik.values.Questions.filter(
      (_: any, i: any) => i !== index,
    );
    formik.setFieldValue(`Questions`, updatedAnswers);
  };


  
  const values = formik.values.Questions?.[index] ;
  
  const handelCanDeleteAnswers = ()=>{
    const content = values?.content ;
    const content_image = values?.content_image ;
    if(!content  && !content_image ){
      return true
    }
    return false
  }



  return (
    <>
      <div className="exercise_forms">
        <div className="ChoiceFields">
          <TextField
            className="textarea_exercise"
            placeholder={"question"}
            label2={
              t(`input.question`) +
              ` ` +
              `( ${t(`alphabet.${getCharFromNumber(index)}`)} )`
            }
            name={index}
            id={`question_${index + 1}`}
            type="TextArea"
          />

          <ImageBoxField name={`Questions.${index}.content_image`} />

              {handelCanDeleteAnswers() ? 
              <div className="answer_status"  >
                <p className="delete_question_options"   onClick={()=>{handleDeleteQuestion()}}>
                  {t("header.delete_question")}
                  <GoTrash
                    className="trash_icon"
                   
                    size={17}
                  />
                </p>
              </div>
                
            : 
            
            <div className="answer_status"  >

          <Popconfirm
    title={t("header.this_will_un_do_all_your_changes")}
    okText={t("practical.yes")}
    cancelText={t("practical.no")}
    onConfirm={()=>{handleDeleteQuestion()}}
    defaultOpen={false}

  >
            <p className="delete_question_options">
              {t("header.delete_question")}
              <GoTrash
                className="trash_icon"
               
                size={17}
              />
            </p>
</Popconfirm>
          </div>
            
            }
       
            
         
        </div>
      </div>
    </>
  );
};

export default QuestionFIeld;
