import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanySuppliersGradeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import dayjs from 'dayjs';
import AddGrade from '../modals/AddGrade';

export default function Index({ ...rest }) {
  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              company_suppliers_id: rest.item?.company_suppliers_id,
            },
            sort,
            Apis.CompanySupplier.CompanySupplierGrades.List,
          )
        }
        toolBarRender={(action) => [
          <AddGrade
            key="AddGrade"
            item={{ ...rest.item, size: 'middle' }}
            reload={action?.reload}
            title="添加评级记录"
          />,
        ]}
        search={false}
        columns={[
          {
            title: '评级日期',
            dataIndex: 'created_at',
            render(_, record) {
              if (!record.created_at) return '';
              return dayjs(record.created_at).format('YYYY-MM-DD');
            },
          },
          MyColumns.EnumTag({
            title: '评定等级',
            dataIndex: 'grade',
            valueEnum: CompanySuppliersGradeEnum,
            search: false,
          }),
          {
            title: '评级说明',
            dataIndex: 'grade_remark',
          },
          {
            title: '评定人',
            dataIndex: 'rater',
            render(_, record) {
              return `${record.rater || ''}-${record.rater_phone || ''}`;
            },
          },

          {
            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?.grade_files?.length > 0
                    ? item.grade_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.UpdatedAt(),
        ]}
      />
    </>
  );
}
