<!DOCTYPE html>
<html lang="pl">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="fav.png" type="image/x-icon">
    <link rel="stylesheet" href="style.css">
    <title>Mieszalnia farb</title>
</head>

<body>
    <header>
        <img src="baner.png" alt="Mieszalnia farb">
    </header>
    <form method="post">
        <label for="odbior-od">Data odbioru od: </label>
        <input type="date" id="odbior-od" name="odbior-od">
        <label for="odbior-do">do: </label>
        <input type="date" id="odbior-do" name="odbior-do">
        <input type="submit" value="Wyszukaj">
    </form>
    <main>
        <table>
            <tr>
                <th>Nr zamówienia</th>
                <th>Nazwisko</th>
                <th>Imię</th>
                <th>Kolor</th>
                <th>Pojemność [ml]</th>
                <th>Data odbioru</th>
            </tr>

            <?php
            $conn = mysqli_connect("localhost", "root", "", "mieszalnia");

            if (!empty($_POST["odbior-od"]) || !empty($_POST["odbior-do"])) {
                $od = mysqli_real_escape_string($conn, $_POST["odbior-od"]);
                $do = mysqli_real_escape_string($conn, $_POST["odbior-do"]);

                if ($od !== "" && $do !== "") {
                    $sql = "SELECT kli.Nazwisko, kli.Imie, kli.Id, zam.kod_koloru, zam.data_odbioru, zam.pojemnosc FROM klienci AS kli 
                    JOIN zamowienia AS zam ON kli.Id = zam.id_klienta
                    WHERE zam.data_odbioru BETWEEN '$od' AND '$do'
                    ORDER BY zam.data_odbioru ASC";
                }
            } else {
                $sql = "SELECT kli.Nazwisko, kli.Imie, kli.Id, zam.kod_koloru, zam.data_odbioru, zam.pojemnosc FROM klienci AS kli 
                    JOIN zamowienia AS zam ON kli.Id = zam.id_klienta
                    ORDER BY zam.data_odbioru ASC";
            }

            $result = mysqli_query($conn, $sql);

            while ($row = mysqli_fetch_assoc($result)) {
                echo '
                        <tr>
                            <td>' . $row["Id"] . '</td>
                            <td>' . $row["Nazwisko"] . '</td>
                            <td>' . $row["Imie"] . '</td>
                            <td style="background-color: #' . $row["kod_koloru"] . ';">' . $row["kod_koloru"] . '</td>
                            <td>' . $row["pojemnosc"] . '</td>
                            <td>' . $row["data_odbioru"] . '</td>
                        </tr>
                    ';
            }

            mysqli_close($conn);
            ?>
        </table>
    </main>
    <footer>
        <h3>Egzamin INF.03</h3>
        <p>Autor: 24</p>
    </footer>
</body>

</html>