<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/style.css">
	<link rel="icon" href="img/favicon.png" type="image/x-icon">
    <title>Artikelübersicht</title>
</head>
<body style="background-color: aliceblue;">
    <header class="header">
        <div class="header-container">
            <h1>Materialverwaltung</h1>
            <nav>
                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="add_material_form.php">Artikel hinzufügen</a></li>
                    <li><a href="article_overview.php">Artikel Übersicht</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <div class="container">
        <h2>Artikelübersicht</h2>
        
        <!-- Artikel suchen -->
        <form method="GET" action="article_overview.php" style="margin-bottom: 20px;">
			<input type="text" name="search" placeholder="Artikel suchen" size="50"> <!-- Hier das size-Attribut geändert -->
			<input type="submit" value="Suchen">
		</form>
        
        <!-- Artikel anzeigen -->
        <table>
            <tr>
                <th>Name</th>
                <th>Hersteller</th>
                <th>Lagerort</th>
                <th>Anzahl soll</th>
                <th>Anzahl ist</th>
                <th>Ablaufdatum</th>
                <th>Barcode</th>
                <th>Bild</th>
                <th>Aktion</th>
                <th></th> <!-- Neue Zelle für das Löschen-Bild -->
            </tr>
            <?php
            // Datenbankverbindung herstellen
            include_once 'config/config.php';

            // Verbindung zur Datenbank herstellen
            $conn = new mysqli($host, $username, $password, $database);
            if ($conn->connect_error) {
                die("Verbindung zur Datenbank fehlgeschlagen: " . $conn->connect_error);
            }

            // SQL-Abfrage vorbereiten
            $sql = "SELECT * FROM materials";
            
            // Wenn nach einem Artikel gesucht wird
            if(isset($_GET['search']) && !empty($_GET['search'])){
                $search = $_GET['search'];
                $sql .= " WHERE item_name LIKE '%" . $search . "%' OR manufacturer LIKE '%" . $search . "%' OR location LIKE '%" . $search . "%' OR barcode LIKE '%" . $search . "%'";
            }

            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                // Daten ausgeben
                while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . $row["item_name"] . "</td>";
                    echo "<td>" . $row["manufacturer"] . "</td>";
                    echo "<td>" . $row["location"] . "</td>";
                    echo "<td>" . $row["amount_should"] . "</td>";
                    echo "<td>" . $row["amount_is"] . "</td>";
                    echo "<td>" . $row["expiration_date"] . "</td>";
                    echo "<td>" . $row["barcode"] . "</td>";
                    echo "<td>";
                    if (!empty($row["image"])) {
                        echo "<img src='" . $row["image"] . "' alt='Bild' style='width: 50px; height: 50px;'>";
                    } else {
                        echo "<img src='img/free.png' alt='Standardbild' style='width: 50px; height: 50px;'>";
                    }
                    echo "</td>";
                    echo "<td><a href='remove_article.php?id=" . $row["id"] . "'><img src='img/delete.png' alt='Löschen' style='width: 20px; height: 20px;'></a></td>";
                    echo "<td></td>"; // Platzhalter-Zelle für das Löschen-Bild
                    echo "</tr>";
                }
            } else {
                echo "<tr><td colspan='10'>Keine Artikel gefunden</td></tr>";
            }
            $conn->close();
            ?>
        </table>
    </div>
</body>
</html>