import { Table } from "@mantine/core";
import type {BookInfo} from "~/utils/models.ts";


export const BookTable = ({ books }: {books: BookInfo[]}) => {
    // 格式化发布日期
    const formatDate = (timestamp: string) => {
        const date = new Date(parseInt(timestamp) * 1000);
        return date.toLocaleDateString();
    };

    // 生成表格行
    const rows = books.map((book, index) => (
        <Table.Tr key={book.isbn || index}>
            <Table.Td>{book.title}</Table.Td>
            <Table.Td>{book.subtitle}</Table.Td>
            <Table.Td>{book.author}</Table.Td>
            <Table.Td>{book.editor}</Table.Td>
            <Table.Td>{book.translator}</Table.Td>
            <Table.Td>{book.publisher}</Table.Td>
            <Table.Td>{formatDate(book.publication_date)}</Table.Td>
            <Table.Td>{book.isbn}</Table.Td>
            <Table.Td>{book.barcode}</Table.Td>
            <Table.Td>{book.language}</Table.Td>
            <Table.Td>{book.pages}</Table.Td>
            <Table.Td>{book.format}</Table.Td>
            <Table.Td>{book.binding}</Table.Td>
            <Table.Td>{book.edition}</Table.Td>
            <Table.Td>{book.weight.toFixed(2)} g</Table.Td>
            <Table.Td>{book.category_id}</Table.Td>
            <Table.Td>{book.subject}</Table.Td>
            <Table.Td style={{ maxWidth: '200px', wordBreak: 'break-word' }}>
                {book.keywords}
            </Table.Td>
            <Table.Td style={{ maxWidth: '300px', wordBreak: 'break-word' }}>
                {book.abstract}
            </Table.Td>
            <Table.Td style={{ maxWidth: '300px', wordBreak: 'break-word' }}>
                {book.description}
            </Table.Td>
            <Table.Td>
                <a href={book.cover_image} target="_blank" rel="noopener noreferrer">
                    View Image
                </a>
            </Table.Td>
        </Table.Tr>
    ));

    return (
        <Table striped highlightOnHover withColumnBorders withTableBorder w="200%">
            <Table.Thead>
                <Table.Tr>
                    <Table.Th>Title</Table.Th>
                    <Table.Th>Subtitle</Table.Th>
                    <Table.Th>Author</Table.Th>
                    <Table.Th>Editor</Table.Th>
                    <Table.Th>Translator</Table.Th>
                    <Table.Th>Publisher</Table.Th>
                    <Table.Th>Publication Date</Table.Th>
                    <Table.Th>ISBN</Table.Th>
                    <Table.Th>Barcode</Table.Th>
                    <Table.Th>Language</Table.Th>
                    <Table.Th>Pages</Table.Th>
                    <Table.Th>Format</Table.Th>
                    <Table.Th>Binding</Table.Th>
                    <Table.Th>Edition</Table.Th>
                    <Table.Th>Weight</Table.Th>
                    <Table.Th>Category ID</Table.Th>
                    <Table.Th>Subject</Table.Th>
                    <Table.Th>Keywords</Table.Th>
                    <Table.Th>Abstract</Table.Th>
                    <Table.Th>Description</Table.Th>
                    <Table.Th>Cover Image</Table.Th>
                </Table.Tr>
            </Table.Thead>
            <Table.Tbody>
                {rows}
            </Table.Tbody>
        </Table>
    );
};
