


import { Button, Upload, UploadFile } from 'antd'
import { UploadOutlined } from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
import { useFormikContext } from 'formik';
import { ImageBaseURL } from '../../../../../api/config';


const FileProduct = ({ tabKey}:any) => {
    const { t } = useTranslation();
  const formik = useFormikContext<any>();
  const name =  `variable[${tabKey}].${"main_photo"}`;
  let FormikName = formik?.values?.variable[tabKey]?.main_photo as any;
  const FormikimageUrl = FormikName ? ImageBaseURL + FormikName :  '';

  const imageUrl = typeof FormikName === "string" ? (FormikimageUrl)?.replace("public", "/storage") : (FormikName instanceof File) ? URL.createObjectURL(FormikName) : "";

  const fileList: UploadFile[] = [

    {
      uid: '-1',
      name: '',
      status: 'done',
      url: imageUrl,
      thumbUrl: imageUrl
    }
  ];
  const FilehandleChange = (value:any) => {
    
    formik.setFieldValue(name, value.file.originFileObj)

  };
  const customRequest = async ({ onSuccess}: any) => {
    onSuccess();
  };
  return (
    <div className="ValidationField">
      <label  className="text">
        {t(`main_photo`)}
      </label>

      <Upload
        listType="picture"
        maxCount={1}
        defaultFileList={[...fileList]}
        onChange={ FilehandleChange}
        customRequest={customRequest}
        className={`w-100`}
        

      >
        <Button className={" w-100"} icon={<UploadOutlined  />}>
          { t("upload_image") }
         
          </Button>
      </Upload>



    </div>
  )
}

export default FileProduct