<?php
session_start();
require 'connect.php';

$search = isset($_GET['search']) ? trim($_GET['search']) : '';

if ($search) {
    $sql = "SELECT DISTINCT book.bookid, book.title, book.isbn, book.genre, book.publishedyear, book.description,
                   book.coverimage, author.firstname, author.lastname 
            FROM book
            INNER JOIN book_author ON book.bookid = book_author.bookid
            INNER JOIN author ON book_author.authorid = author.authorid
            WHERE LOWER(book.title) LIKE LOWER(:search)
            OR LOWER(CONCAT(author.firstname, ' ', author.lastname)) LIKE LOWER(:search)
            OR LOWER(book.isbn) LIKE LOWER(:search)
            OR LOWER(book.genre) LIKE LOWER(:search)
            OR LOWER(book.description) LIKE LOWER(:search)
            ORDER BY book.title ASC";

    try {
        $stmt = $conn->prepare($sql);
        $searchPattern = "%{$search}%";
        $stmt->bindParam(':search', $searchPattern, PDO::PARAM_STR);
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    } catch(PDOException $e) {
        die("Search failed: " . $e->getMessage());
    }
}


?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search Results - Library Management System</title>
    <style>
        .search-results {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }

        .search-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }

        .book-card {
            border: 1px solid #ddd;
            padding: 10px;
            border-radius: 5px;
            text-align: center;
        }

        .book-card img {
            max-width: 150px;
            height: auto;
            margin-bottom: 10px;
        }

        .book-card h3 {
            margin: 5px 0;
            font-size: 1.1em;
        }

        .book-card p {
            margin: 5px 0;
            color: #666;
        }

        .search-header {
            margin-bottom: 20px;
        }

        .no-results {
            text-align: center;
            padding: 40px;
            color: #666;
        }
    </style>
</head>
<body>
    <?php include 'Components/Header.html'; ?>

    <div class="search-results">
        <div class="search-header">
            <h1>Search Results</h1>
            <p>Showing results for: "<?php echo htmlspecialchars($search); ?>"</p>
        </div>

        <?php if (isset($results) && !empty($results)): ?>
            <div class="search-grid">
                <?php foreach ($results as $book): ?>
                    <div class="book-card">
                        <a href="BookView.php?bookid=<?php echo $book['bookid']; ?>">
                            <img src="./BookImages/<?php echo htmlspecialchars($book['coverimage']); ?>" 
                                 alt="<?php echo htmlspecialchars($book['title']); ?>">
                        </a>
                        <h3>
                            <a href="BookView.php?bookid=<?php echo $book['bookid']; ?>" 
                               style="text-decoration: none; color: green;">
                                <?php echo htmlspecialchars($book['title']); ?>
                            </a>
                        </h3>
                        <p><?php echo htmlspecialchars($book['firstname'] . ' ' . $book['lastname']); ?></p>
                        <p><?php echo htmlspecialchars($book['genre']); ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="no-results">
                <h2>No results found</h2>
                <p>Try different keywords or check your spelling</p>
            </div>
        <?php endif; ?>
    </div>

    <?php include 'Components/Footer.html'; ?>
</body>
</html>