import { MyBetaModalFormProps, renderTextHelper } from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import { CompanySealsTypeEnum } 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(true);
  const [data, setData] = useState<any>({});

  const handleOpenModal = () => {
    const contractsId = props?.item?.id;
    if (contractsId) {
      setLoading(true);
      Apis.Contract.ContractSealLogs.List({ contracts_id: contractsId })
        .then((res: any) => {
          const list = res?.data || [];
          if (list.length > 0) {
            return Apis.Contract.ContractSealLogs.Show({ id: list[0].id });
          }
          return Promise.resolve({ data: {} });
        })
        .then((res: any) => {
          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="合同ID" span={1}>
                  {data?.contracts_id}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="用印人">
                  {data?.seal_person || data?.company_employee?.name || '-'}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="用印时间">
                  {data?.use_seal_time}
                </ProDescriptions.Item>

                <ProDescriptions.Item label="合同份数">
                  {data?.number_contract_copies}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="是否用印">
                  {data?.is_use_seal ? '是' : '否'}
                </ProDescriptions.Item>

                <ProDescriptions.Item label="印章类型" span={3}>
                  {data?.seal_ids?.map((sealId: string, index: number) => (
                    <span key={index} style={{ marginRight: 16 }}>
                      <renderTextHelper.Tag
                        Enums={CompanySealsTypeEnum}
                        value={sealId}
                        key={`seal_id_${index}`}
                      />
                    </span>
                  )) || '-'}
                </ProDescriptions.Item>

                <ProDescriptions.Item label="备注" span={3}>
                  {data?.remarks || '-'}
                </ProDescriptions.Item>

                <ProDescriptions.Item label="创建日期">
                  {data?.created_at}
                </ProDescriptions.Item>
                <ProDescriptions.Item label="最近修改">
                  {data?.updated_at}
                </ProDescriptions.Item>
              </ProDescriptions>
            </Spin>
          </ProCard>
        </Space>
      }
    />
  );
}
