import { MyBetaModalFormProps, renderTextHelper } from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import {
  ContractArchivesConfidentialityLevelEnum,
  ContractArchivesFileStatusEnum,
  ContractArchivesFileTypeEnum,
  ContractArchivesPeriodTypeEnum,
} from '@/gen/Enums';
import { ProCard, ProDescriptions } from '@ant-design/pro-components';
import { Space, Spin } from 'antd';
import { useState } from 'react';

export default function Show(props: MyBetaModalFormProps) {
  const [loading, setLoading] = useState(false);
  const [data, setData] = useState<any>({});

  const handleOpenModal = () => {
    if (props?.item?.id) {
      setLoading(true);
      Apis.Contract.ContractArchives.Show({ id: props.item.id })
        .then((res) => {
          setData(res?.data || {});
        })
        .finally(() => {
          setLoading(false);
        });
    }
  };
  return (
    <MyModal
      title={props.title || '查看'}
      type={props.item?.type || 'primary'}
      width="800px"
      onOpen={handleOpenModal}
      node={
        <Space direction="vertical" style={{ width: '100%' }}>
          <ProCard extra={props.extra}>
            <Spin spinning={loading}>
              <ProDescriptions column={3}>
                {/* 基本信息 */}
                <ProDescriptions.Item label="归档编号" span={1}>
                  {data?.code}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="归档名称">
                  {data?.name}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="合同ID">
                  {data?.contracts_id}
                </ProDescriptions.Item>

                {/* 保管信息 */}
                <ProDescriptions.Item label="保管期限">
                  <renderTextHelper.Tag
                    Enums={ContractArchivesPeriodTypeEnum}
                    value={data?.period_type}
                    key="period_type"
                  />
                </ProDescriptions.Item>
                {data?.period_type === 'FixedTerm' && (
                  <ProDescriptions.Item label="保管年限">
                    {data?.storage_years} 年
                  </ProDescriptions.Item>
                )}
                <ProDescriptions.Item label="保密等级">
                  <renderTextHelper.Tag
                    Enums={ContractArchivesConfidentialityLevelEnum}
                    value={data?.confidentiality_level}
                    key="confidentiality_level"
                  />
                </ProDescriptions.Item>

                {/* 存放信息 */}
                {/* <ProDescriptions.Item label="案卷编号">
                  {data?.file_number}
                </ProDescriptions.Item> */}
                <ProDescriptions.Item label="文件盒编号">
                  {data?.box_number}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="文件柜编号">
                  {data?.cabinet_number}
                </ProDescriptions.Item>

                <ProDescriptions.Item label="保管人">
                  {data?.keeper_name}-{data?.keeper_phone}
                </ProDescriptions.Item>
                {/* 时间信息 */}
                <ProDescriptions.Item label="归档日期">
                  {data?.archive_date}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="归档年份">
                  {data?.archive_year}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="是否封存">
                  {data?.is_sealed ? `是 | ${data?.seal_date}` : '否'}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="创建日期">
                  {data?.created_at}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="最近修改">
                  {data?.updated_at}
                </ProDescriptions.Item>
              </ProDescriptions>
            </Spin>
          </ProCard>

          <Spin spinning={loading}>
            {/* 归档文件列表 */}
            {(data?.contract_archive_files ?? []).map(
              (file: any, index: number) => (
                <ProCard
                  key={index}
                  title={`归档文件 ${index + 1}`}
                  style={{ marginBottom: 16 }}
                >
                  <ProDescriptions column={4}>
                    <ProDescriptions.Item label="文件名称">
                      {file?.name}
                    </ProDescriptions.Item>
                    <ProDescriptions.Item label="文件类型">
                      <renderTextHelper.Tag
                        Enums={ContractArchivesFileTypeEnum}
                        value={file?.type}
                        key="file_type"
                      />
                    </ProDescriptions.Item>
                    <ProDescriptions.Item label="文件份数">
                      {file?.number}
                    </ProDescriptions.Item>
                    <ProDescriptions.Item label="文件状态">
                      <renderTextHelper.Tag
                        Enums={ContractArchivesFileStatusEnum}
                        value={file?.status}
                        key="file_status"
                      />
                    </ProDescriptions.Item>
                    <ProDescriptions.Item label="备注" span={2}>
                      {file?.remark}
                    </ProDescriptions.Item>
                    <ProDescriptions.Item label="扫描件" span={2}>
                      {file?.info && file?.info.length > 0 ? (
                        <Space>
                          {file?.info.map((item: any, fileIndex: number) => (
                            <a
                              key={fileIndex}
                              href={item?.url}
                              target="_blank"
                              rel="noopener noreferrer"
                            >
                              {item?.name || `文件 ${fileIndex + 1}`}
                            </a>
                          ))}
                        </Space>
                      ) : (
                        '-'
                      )}
                    </ProDescriptions.Item>
                  </ProDescriptions>
                </ProCard>
              ),
            )}
          </Spin>
        </Space>
      }
    />
  );
}
