<!DOCTYPE html>
<html>
<head>
    <meta lang="de"/>
    <meta charset="utf-8"/>
    <meta name="viewport" content="device=device-width, initial-scaling=1"/>
    <link rel="Stylesheet" type="text/css" href="style.css"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <title>Candle Bibliothek | Löschen Erfolgreich Meine Bücher</title>
    <link rel="icon" href="pictures/candle.png">
    <script src="burgerIcon.js"></script>
    <script src="notwendiges_laden.js" defer></script>
</head>
<body>
<header id="header">
    <h1 class="kopf" >
        <br><br>
        Bibliothek
        <a href="index.php" id="bildID">
            <img id="CandleID" src="pictures/candle.png"
                 alt="Bild von einer Kerze" width="200" height="192"/></a>
        <br><br>
        Candle
    </h1>
    <div>
        <a id="uberuns" href="uberuns.php">Über Uns</a>
        <a id="buecher" href="buecher.php">Bücher</a>
        <a id="meinebuecher" href="anmelden_meinebuecher.php">
            Meine Bücher</a>
        <?php
               session_start();
               if(isset($_SESSION["eingeloggt"])  && $_SESSION["eingeloggt"] == 1 ){
                echo "<a id='abmelden' href='abmelden.php'>Abmelden</a>";}
        ?>
        <form action="suchergebnisse.php">
            <input type="hidden" name="searchType" value="simple">
            <input name="searchFor" class="suchleiste" type="search"
                   placeholder="Suche...">
            <button type="submit" id="buttonID" class="searchButton">
                <i class="fa fa-search"></i>
            </button>
        </form>
        <a id="erweitertesuche" href="erweitertesuche.php">
            Erweiterte Suche</a>
        <a href="warenkorb.php" id="warenKorbID">
            <img id="shoppingCard" src="pictures/einkaufswagen.png"
                 alt="Bild von einem Wagen" width="50" height="50"/>
        </a>
    </div>
</header>

<?php

$BookID = $_GET['buchID'];

$servername = "localhost";
$username = "web_b-3";
$password = "een7Ao6s";
$dbname = "bibliothek_candle";

$connection =
    mysqli_connect($servername, $username, $password, $dbname);

if (!$connection) {
    die("Verbindung fehlgeschlagen: " . mysqli_connect_error());
}

$sqlDelete = $connection->prepare("
DELETE FROM `ausgeliehenes_buch` WHERE `ausgeliehenes_buch`.`AusleihID` = ?;");
$sqlDelete->bind_param("i", $ID);
$ID = $_GET['ausleihID'];
// SQL-Befehl ausführen
$sqlDelete->execute();

$sqlGetQuantity = $connection->prepare(
    "SELECT Anzahl, MaxAnzahl FROM `buch` WHERE BuchID = ?;");
$sqlGetQuantity->bind_param("i", $BookID);
$sqlGetQuantity->execute();
// Ergebnis speichern
$resultGetQuantity = $sqlGetQuantity->get_result();
if (mysqli_num_rows($resultGetQuantity) > 0) {
    while($rowGetQuantity = mysqli_fetch_assoc($resultGetQuantity)) {
        $bookQuantity = $rowGetQuantity['Anzahl'];
        $bookMaxQuantity = $rowGetQuantity['MaxAnzahl'];
        $bookTempQuantity = $bookQuantity + 1;
        $bookTempQuantityNew = $bookMaxQuantity - $bookTempQuantity;

        $sqlGetBorrowedQuantity = $connection->prepare(
            "SELECT COUNT(*) AS Ausgeliehen 
            FROM `ausgeliehenes_buch` WHERE BuchID = ?;"
        );
        $sqlGetBorrowedQuantity->bind_param("i", $BookID);
        $sqlGetBorrowedQuantity->execute();
        // Ergebnis speichern
        $resultGetBorrowedQuantity = $sqlGetBorrowedQuantity->get_result();
        if (mysqli_num_rows($resultGetBorrowedQuantity) > 0) {
            while($rowGetBorrowedQuantity = mysqli_fetch_assoc($resultGetBorrowedQuantity)) {
                $bookBorrowedQuantity = $rowGetBorrowedQuantity['Ausgeliehen'];
                
                if ($bookBorrowedQuantity == $bookTempQuantityNew) {
                    $sqlUpdateBook = $connection->prepare(
                        "UPDATE buch SET Anzahl = Anzahl+1 
                        WHERE buch.BuchID = ? ;"
                    );
                    $sqlUpdateBook->bind_param("i", $BookID);
                    $sqlUpdateBook->execute();
                }
                else {
                    $sqlGetBorrowedID = $connection->prepare(
                        "SELECT AusleihID 
                        FROM `ausgeliehenes_buch` 
                        WHERE BuchID = ? 
                        AND Reserviert=1 
                        ORDER BY Enddatum 
                        DESC LIMIT 1;"
                    );
                    $sqlGetBorrowedID->bind_param("i", $BookID);
                    $sqlGetBorrowedID->execute();
                    // Ergebnis speichern
                    $resultGetBorrowedID = $sqlGetBorrowedID->get_result();
                    if (mysqli_num_rows($resultGetBorrowedID) > 0) {
                        while($rowGetBorrowedID = mysqli_fetch_assoc($resultGetBorrowedID)) {
                            $bookBorrowedID = $rowGetBorrowedID['AusleihID'];
                            $sqlUpdateBookID = $connection->prepare(
                                "UPDATE `ausgeliehenes_buch` 
                                SET `Verlängerbar` = '1', 
                                `Reserviert` = '0' 
                                WHERE `ausgeliehenes_buch`.`AusleihID` = ?;"
                            );
                            $sqlUpdateBookID->bind_param("i", $bookBorrowedID);
                            $sqlUpdateBookID->execute();
                            
                        }
                    }
                }
            }
        }
    }
}

?>

<div class="hauptcontainer" id="verlängerung">
    <h1 id="verlängerungH1">Buch erfolgreich entfernt</h1>
    <div id="verlängerungContent">
        <p id="möchten-sie">Die Reservierung wurde erfolgreich aufgehoben.</p>

        <div id="verlängerungButtonContainer">
            <a id = "verlängerungErfolgreichButton1" href="meinebuecher.php">Zurück zum Warennkorb</a>
        </div>

    </div>

</div>





<!--Link JS file-->
<!--If it was small and got big, it calls the function only one time and doesn't do it all the time-->
<script src="resize.js"></script>

<nav id="activeid" class="active"></nav>

<footer id="footer" class="fuss" ></footer>
</body>

</html>