<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Suche nach Kontrollstreifen</title>
    <link rel="stylesheet" type="text/css" href="../wbs2.css">
</head>
        
<body>

<?php
// Debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

<?php
// eigener Filename für GET und POST
$filename = 'suche_kontstr.php';
$today = date('Y-m-d');
$yesterday = (new DateTime($today))->modify('-1 day')->format('Y-m-d');
$startwoche = (new DateTime($today))->modify('-7 day')->format('Y-m-d');
$firstdaythisyear = (new DateTime($today))->modify('first day of January this year')->format('Y-m-d');
$firstdaylastyear = (new DateTime($today))->modify('first day of January last year')->format('Y-m-d');
$lastdaylastyear = (new DateTime($today))->modify('last day of December last year')->format('Y-m-d');
?>
<script>
    function set_heute() {
        document.getElementById('Dv').value = '<?php echo $today; ?>';
        document.getElementById('Db').value = '<?php echo $today; ?>';
    }
    function set_gestern() {
        document.getElementById('Dv').value = '<?php echo $yesterday; ?>';
        document.getElementById('Db').value = '<?php echo $yesterday; ?>';
    }
    function set_woche() {
        document.getElementById('Dv').value = '<?php echo $startwoche; ?>';
        document.getElementById('Db').value = '<?php echo $today; ?>';
        document.getElementById('Tv').value = '00:00';
        document.getElementById('Tb').value = '23:59';
    }
    function set_jahr() {
        document.getElementById('Dv').value = '<?php echo $firstdaythisyear; ?>';
        document.getElementById('Db').value = '<?php echo $today; ?>';
        document.getElementById('Tv').value = '00:00';
        document.getElementById('Tb').value = '23:59';
    }
    function set_vorjahr() {
        document.getElementById('Dv').value = '<?php echo $firstdaylastyear; ?>';
        document.getElementById('Db').value = '<?php echo $lastdaylastyear; ?>';
        document.getElementById('Tv').value = '00:00';
        document.getElementById('Tb').value = '23:59';
    }
</script>
<?php

// Get the parameters form submission
$filiale = isset($_POST['Filiale']) ? $_POST['Filiale'] : '';
$terminal = isset($_POST['Terminal']) ? $_POST['Terminal'] : '';
$text = isset($_POST['Text']) ? $_POST['Text'] : '';
$datum_von = isset($_POST['Dv']) ? $_POST['Dv'] : '';
$datum_bis = isset($_POST['Db']) ? $_POST['Db'] : '';
$zeit_von = isset($_POST['Tv']) ? $_POST['Tv'] : '';
$zeit_bis = isset($_POST['Tb']) ? $_POST['Tb'] : '';

// Buttons via GET Reset
If (isset($_GET['do'])) {
    $todo = $_GET['do'];
    if ($todo == 'Reset') {
        $filiale = "";
        $terminal = "";
        $text = "";
        $datum_von = $yesterday;
        $datum_bis = $today;
        $zeit_von ="00:00";
        $zeit_bis ="23:59";
        }
}

// Get the parameters from the URL
if (isset($_GET['fil'])) {
    $filiale = $_GET['fil'];
    }

if (empty($datum_von)) {
    $datum_von = $yesterday;
    $datum_bis = $today;
}
if (empty($datum_bis)) {
   $datum_bis = $datum_von;
}
if (empty($zeit_von)) {
   $zeit_von = "00:00";
}
if (empty($zeit_bis)) {
    $zeit_bis = "23:59";
}
if (empty($filiale)) {
    $filiale = "";
}
if (empty($terminal)) {
    $terminal = "";
}

// Include the functions file
include '../functions.php';

// Include the database configuration file
include '../admin/wbs2_config.php';

// Create connection
global $conn;
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    echo "Connection failed";
}
else {
    // echo "Connected successfully";
    // echo "<br>";
}

// Filialen Array laden
$filialen_array = FilialenArray();
?>

<div id="suchfenster" class="main">

    <div class="boxinput">
    <h2>Suche in Bons (Kontrollstreifen)</h2>
    <form method="post" <?php echo 'action="' . $filename . '"' ?>>
        <table>
        <tr>
            <td><label for="Filiale">Filiale</label></td>
            <td>
                    <?php
                    if (empty($filiale)) {
                        echo '<select id="Filiale" name="Filiale">';
                    
                        echo '<option value="">-- Select Filiale --</option>';
                        foreach ($filialen_array as $filiale_nr => $filiale_name) {
                            echo "<option value=\"" . htmlspecialchars($filiale_nr) . "\">" . htmlspecialchars($filiale_name) . " " . htmlspecialchars($row['Filiale_Name']) . "</option>";
                        }
                        echo "</select>";
                    }
                    else {
                        echo '<input type="text" id="Filiale" name="Filiale" value="'. $filiale . '">';
                    }   
                    ?>
            </td>
        </tr>
        <tr>
            <td><label for="Terminal">Terminal</label></td>
            <td>
                <?php
                if (empty($terminal)) {
                    echo '<select id="Terminal" name="Terminal">';
                    echo '<option value="">-- Select Terminal --</option>';
                    for ($i = 1; $i <= 20; $i++) {
                        echo "<option value=\"$i\">$i</option>";
                    }
                    echo "</select>";
                }
                else {
                    echo '<input type="text" id="Terminal" name="Terminal" value="'. $terminal . '">';
                }
                ?>
            </td>
        </tr>
        <tr>
                <td><label for="Dv">Datum/Zeit von</label></td>
                <?php echo'<td><input type="date" id="Dv" name="Dv" value="' . $datum_von . '">'; ?>

                <label for="Tv"> </label>
                <?php echo'<td><input type="time" id="Tv" name="Tv" value="' . $zeit_von . '">'; ?>

                <input type="button" value="heute" onclick="set_heute()">
                <input type="button" value="gestern" onclick="set_gestern()">
                <input type="button" value="Woche" onclick="set_woche()">
                <input type="button" value="Jahr" onclick="set_jahr()">
                <input type="button" value="Vorjahr" onclick="set_vorjahr()">
               
                </td>
            </tr>
            <tr>
                <td><label for="Db">Datum/Zeit bis</label></td>
                <?php echo'<td><input type="date" id="Db" name="Db" value="' . $datum_bis . '">'; ?>

                <label for="Tb"> </label>
                <?php echo'<td><input type="time" id="Tb" name="Tb" value="' . $zeit_bis . '">'; ?>
                </td>
        </tr>
            <?php /*
            <tr>
                <td><label for="Nr">Nr</label></td>
                <?php echo '<td> <input type="text" id="Nr" name="Nr" value="'. $nr . '"></td>'; ?>
            </tr>
            */ ?>
            <tr>
                <td><label for="Text">Artikel/Text/Nr</label></td>
                <?php echo '<td><input type="text" id="Text" name="Text" value="' . $text . '" ></td>' ?>
            </tr>
        </table>
        <br>
        <input type="submit" value="Suchen">
        <input type="button" value="Zurück" onclick="history.back()">
        <?php SetButton ('Reset'); ?>
        <!-- Add a Print button -->
        <button onclick="window.print()">Drucken</button>
        
    </form>
    </div>

    <div class="boxcomment">
    <?php
    // if (!empty($filiale) && !empty($terminal)) {
    $bonids = SelectFilTerm($filiale, $terminal, $datum_von, $datum_bis, $zeit_von, $zeit_bis, $text);
    // }
    ?>
    </div>    
</div>

<div class="main_kontstr">
    <?php

// Gefundene Bons anzeigen
if (!empty($bonids)) {
    $bon_count = count($bonids);
    If ($bon_count > 10000) {
        echo "<br>";
        echo "Gefundene Bons: " . $bon_count . "<br>";
        echo "Bitte Suche einschränken!";
    }
    else {
        global $bon_total;
        $count = 0;
        foreach ($bonids as $bonid) {
            if ($count >= 1000) {
                echo "<p>Maximal 1000 Bons angezeigt!</p>";
                break;
            }
            ShowKontStr($bonid->Filiale, $bonid->Terminal, $bonid->Nr);
            $count++;
        }
        
    }
}

    ?>
</div>

<?php
// Close the connection
$conn->close();
?>

</body>
</html>