<?php
session_start();

if (empty($_SESSION['status'])) {
    if (!file_exists('config/config.inc.php')) {
        $_SESSION['status'] = 'error';
    } else {
        $_SESSION['status'] = 'login';
    }
}

if ($_SESSION['status'] == "login") {
    echo "<html>
        <head>
            <script>
                setTimeout(\"window.location.href='../login.php'\", 0);
            </script>
        </head>
        <body>
        </body>
        </html>";
    return;
}
include '../config/config.inc.php';
$data = new Config();


// Create connection
$conn = new mysqli($data->getSQLHost(), $data->getSQLUser(), $data->getSQLPassword(), $data->getSQLDatabase());
mysqli_set_charset($conn, "utf8");

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query("SELECT * FROM `feuerwehr_pi`");

include("include/header.php");
$header = new Header();
$header->showHeader("Feuerwehr"); ?>

<main class="mdl-layout__content mdl-color--grey-100">
    <div class="mdl-grid demo-content">
        <div class="demo-charts mdl-color--white mdl-shadow--2dp mdl-cell mdl-cell--12-col mdl-grid">
            <h3 id="feedback"></h3>
            <div class="mdl-textfield mdl-js-textfield getmdl-select getmdl-select__fix-height">
                <input class="mdl-textfield__input" value="" id="ort" name="ort" readonly/>
                <input value="" type="hidden" name="ort"/>
                <i class="mdl-icon-toggle__label material-icons">keyboard_arrow_down</i>
                <label class="mdl-textfield__label" for="ort">Wache</label>
                <ul for="ort" class="mdl-menu mdl-menu--bottom-left mdl-js-menu">
                    <?php
                    if ($result->num_rows > 0) {
                        // output data of each row
                        while ($row = $result->fetch_assoc()) {
                            ?>
                            <li class="mdl-menu__item"
                                data-val="<?php echo($row['id']); ?>"> <?php echo($row['Ort']) ?></li> <?php
                        }
                    }
                    ?>
                </ul>
            </div>

            <br>

            <div class="mdl-textfield mdl-js-textfield">
                <input class="mdl-textfield__input" type="text" id="name" name="name">
                <label class="mdl-textfield__label" for="name">Funkrufname</label>
            </div>

            <br>

            <div class="mdl-textfield mdl-js-textfield">
                <input class="mdl-textfield__input" type="text" id="number" name="number">
                <label class="mdl-textfield__label" for="number">Funknummer</label>
            </div>

            <br>

            <div class="mdl-textfield mdl-js-textfield">
                <input class="mdl-textfield__input" type="text" id="type" name="number">
                <label class="mdl-textfield__label" for="type">Fahrzeug</label>
            </div>

            <br>

            <button onclick="addVehicle()"
                    class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored">
                OK
            </button>
        </div>

</main>
</div>


<script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script src="../style/js/jQuery.js"></script>
<script>

    function addVehicle() {
        var ort = $("#ort").val();
        var name = $("#name").val();
        var number = $("#number").val();
        var type = $("#type").val();
        $.ajax({
            url: "../scripts/vehicle.php",
            type: "POST",
            data: "org=ff&ort=" + ort + "&name=" + name + "&number=" + number + "&type=" + type,
            success: function (resp) {
                $("#feedback").html(resp);
            }
        });
    }

</script>

</body>
</html>
