import { MyBetaModalFormProps, renderTextHelper } from '@/common';
import { CompanySealsTypeEnum } from '@/gen/Enums';

// import SealInfo from '@/pages/contract/contracts_other/modals/Show';
import { ProCard, ProDescriptions } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Form, Space } from 'antd';

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const navigate = useNavigate();

  return (
    <>
      <Space direction="vertical" style={{ width: '100%' }}>
        <ProCard title="基本信息" size="small">
          <ProDescriptions size="small" column={2}>
            <ProDescriptions.Item label="申请事项" span={2}>
              <Space>
                {props?.item?.title || '-'}
                {/* <SealInfo
                  item={{
                    id: props?.item?.model?.id,
                    type: 'link',
                  }}
                  title="查看详情"
                /> */}
              </Space>
            </ProDescriptions.Item>

            {/* 基本信息 */}
            <ProDescriptions.Item label="关联项目">
              {props?.item?.asset_project?.name || '-'}
            </ProDescriptions.Item>
            <ProDescriptions.Item label="申请人">
              {props?.item?.model?.company_employee_name || '-'}
            </ProDescriptions.Item>

            {/* 用印详情 */}
            <ProDescriptions.Item label="申请份数">
              {props?.item?.model?.copies ||
                props?.item?.model?.number_contract_copies ||
                '-'}
            </ProDescriptions.Item>
            <ProDescriptions.Item label="申请时间">
              {props?.item?.created_at || '-'}
            </ProDescriptions.Item>
            <ProDescriptions.Item label="简要说明" span={2}>
              {props?.item?.model?.reason || '-'}
            </ProDescriptions.Item>
            {/* 印章信息 */}
            <ProDescriptions.Item label="申请印章" span={2}>
              {(props?.item?.model?.seals || props?.item?.model?.seals)?.map(
                (sealId: string, index: number) => (
                  <Space key={index} style={{ marginRight: 16 }}>
                    {props?.item?.model?.seals?.[index]?.company_supplier
                      ?.name || '-'}
                    <renderTextHelper.Tag
                      Enums={CompanySealsTypeEnum}
                      value={props?.item?.model?.seals?.[index]?.type || '-'}
                      key={`seal_id_${index}`}
                    />
                  </Space>
                ),
              ) || '-'}
            </ProDescriptions.Item>

            {/* 申请信息 */}

            <ProDescriptions.Item label="附件" span={3}>
              {props?.item?.model?.files &&
                props?.item?.model?.files.length > 0 && (
                  <div style={{ marginBottom: 8 }}>
                    <button
                      onClick={() => {
                        props?.item?.model?.files.forEach(
                          (file: any, index: number) => {
                            setTimeout(async () => {
                              const fileName = file?.name || `文件${index + 1}`;
                              try {
                                const response = await fetch(file?.url);
                                const blob = await response.blob();
                                const url = window.URL.createObjectURL(blob);
                                const a = document.createElement('a');
                                a.href = url;
                                a.download = fileName;
                                document.body.appendChild(a);
                                a.click();
                                window.URL.revokeObjectURL(url);
                                document.body.removeChild(a);
                              } catch (error) {
                                console.error('下载失败:', error);
                                window.open(file?.url, '_blank');
                              }
                            }, index * 100); // 延迟100ms避免浏览器阻塞
                          },
                        );
                      }}
                      style={{
                        padding: '4px 12px',
                        marginBottom: '8px',
                        backgroundColor: '#1890ff',
                        color: 'white',
                        border: 'none',
                        borderRadius: '4px',
                        cursor: 'pointer',
                      }}
                    >
                      全部下载
                    </button>
                  </div>
                )}
              {props?.item?.model?.files?.map((file: any, index: number) => {
                const fileName = file?.name || `文件${index + 1}`;

                const handleDownload = async (e: React.MouseEvent) => {
                  e.preventDefault();
                  try {
                    const response = await fetch(file?.url);
                    const blob = await response.blob();
                    const url = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = fileName;
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);
                    document.body.removeChild(a);
                  } catch (error) {
                    console.error('下载失败:', error);
                    // 如果下载失败，则在新窗口打开
                    window.open(file?.url, '_blank');
                  }
                };
                return (
                  <div key={index}>
                    <a href={file?.url} onClick={handleDownload}>
                      {fileName}
                    </a>
                  </div>
                );
              }) || '-'}
            </ProDescriptions.Item>
          </ProDescriptions>
        </ProCard>
      </Space>
    </>
  );
}
