import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { CreditRatingEnum, RatingOutlookEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space, Tag } from 'antd';
import AddCredit from '../modals/AddCredit';
import CreditShow from './modals/CreditShow';
import CreditUpdate from './modals/CreditUpdate';

export default function Credit({ ...rest }) {
  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              supplier_id: rest.item?.company_suppliers_id,
            },
            sort,
            Apis.CompanySupplier.SupplierCreditReports.List,
          )
        }
        search={false}
        toolBarRender={(action) => [
          <AddCredit
            key="AddCredit"
            item={{ ...rest.item, size: 'middle' }}
            reload={action?.reload}
            title="添加第三方资信调查报告"
          />,
        ]}
        columns={[
          {
            title: '调查机构',
            dataIndex: 'investigation_agency',
            search: false,
          },
          MyColumns.EnumTag({
            title: '信用评级',
            dataIndex: 'credit_rating',
            valueEnum: CreditRatingEnum,
            search: false,
          }),

          MyColumns.EnumTag({
            title: '评级展望',
            dataIndex: 'rating_outlook',
            valueEnum: RatingOutlookEnum,
            search: false,
          }),

          {
            title: '失信记录',
            dataIndex: 'has_execution_info',
            search: false,
            render: (_, item: any) => (
              <Tag color={item?.has_execution_info ? 'red' : 'green'}>
                {item?.has_execution_info ? '有' : '无'}
              </Tag>
            ),
          },
          {
            title: '诉讼记录',
            dataIndex: 'has_lawsuit_record',
            search: false,
            render: (_, item: any) => (
              <Tag color={item?.has_lawsuit_record ? 'red' : 'green'}>
                {item?.has_lawsuit_record ? '有' : '无'}
              </Tag>
            ),
          },
          {
            title: '行政处罚',
            dataIndex: 'has_admin_penalty',
            search: false,
            render: (_, item: any) => (
              <Tag color={item?.has_admin_penalty ? 'red' : 'green'}>
                {item?.has_admin_penalty ? '有' : '无'}
              </Tag>
            ),
          },
          {
            title: '出具日期',
            dataIndex: 'issue_date',
            search: false,
            valueType: 'date',
          },
          {
            title: '报告有效期',
            dataIndex: 'expiry_date',
            search: false,
            valueType: 'date',
          },
          {
            title: '调查文件',
            search: false,
            render: (_, item) => {
              // 文件名缩写函数
              const truncateFileName = (
                fileName: string,
                maxLength: number = 16,
              ) => {
                if (!fileName || fileName.length <= maxLength) {
                  return fileName;
                }
                const extensionIndex = fileName.lastIndexOf('.');
                if (
                  extensionIndex > -1 &&
                  extensionIndex < fileName.length - 1
                ) {
                  // 有扩展名的情况
                  const extension = fileName.substring(extensionIndex);
                  const nameWithoutExt = fileName.substring(0, extensionIndex);
                  const availableLength = maxLength - extension.length;
                  if (availableLength <= 5) {
                    // 如果剩余长度太少，直接截断并保留扩展名
                    return (
                      fileName.substring(0, maxLength - 3) + '...' + extension
                    );
                  }
                  return (
                    nameWithoutExt.substring(0, availableLength - 3) +
                    '...' +
                    extension
                  );
                }
                // 没有扩展名的情况
                return fileName.substring(0, maxLength - 3) + '...';
              };

              return (
                <div style={{ lineHeight: '1.8' }}>
                  {item?.files?.length > 0
                    ? item.files.map((file: any, index: number) => {
                        const fileName = file?.name || `文件${index + 1}`;
                        const truncatedName = truncateFileName(fileName);
                        return (
                          <div key={index}>
                            <a
                              href={file?.url}
                              target="_blank"
                              rel="noopener noreferrer"
                              style={{ display: 'block' }}
                              title={fileName} // 完整文件名作为提示
                            >
                              {truncatedName}
                            </a>
                          </div>
                        );
                      })
                    : '-'}
                </div>
              );
            },
            width: 200,
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={`item_${index}`}>
                <CreditShow item={item} reload={action?.reload} title="查看" />
                <CreditUpdate
                  item={item}
                  reload={action?.reload}
                  title="编辑"
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
