


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


const File = ({ tabKey}:any) => {
    const { t } = useTranslation();
  const formik = useFormikContext<any>();
  const name = `Attribute[${tabKey}].${"main_photo"}`;
  const imageUrl =  formik?.values?.Attribute[tabKey]?.main_photo ? URL.createObjectURL(formik?.values?.Attribute[tabKey]?.main_photo) : "" ;

  const fileList: UploadFile[] = [

    {
      uid: '-1',
      name: formik?.values?.Attribute[tabKey]?.main_photo?.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 w-100">
        {t(`main_photo`)}
        <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>
      </label>

    



    </div>
  )
}

export default File