import { MyBetaModalFormProps } from '@/common';
import { ProCard } from '@ant-design/pro-components';
export default function Attachments(props: MyBetaModalFormProps) {
  return (
    <ProCard title="附件文件">
      {props?.item?.map((item: any) => {
        const handleDownload = async (e: React.MouseEvent) => {
          e.preventDefault();
          try {
            const response = await fetch(item.url);
            const blob = await response.blob();
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = item.name;
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(url);
            document.body.removeChild(a);
          } catch (error) {
            console.error('下载失败:', error);
            // 如果下载失败，则在新窗口打开
            window.open(item.url, '_blank');
          }
        };

        return (
          <div key={item.url}>
            <a href={item.url} onClick={handleDownload}>
              {item.name}
            </a>
          </div>
        );
      })}

      {/* {props?.item?.map((res: any, index: number) => {
        return res?.type?.indexOf('image') > -1 ? (
          <Image
            src={res?.url}
            alt={res?.name || '文件'}
            key={`item_${index}`}
            width={100}
            height={100}
          />
        ) : (
          <a href={res?.url} key={`item_${index}`}>
            {res?.name || '文件'}
          </a>
        );
      })} */}
    </ProCard>
  );
}
