<?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();

try {
    $pdo = new PDO('mysql:host=' . $data->getSQLHost() . ';dbname=' . $data->getSQLDatabase() . ';charset=utf8mb4', $data->getSQLUser(), $data->getSQLPassword());
} catch (PDOException $exception) {
    echo "Fehler: " . $exception->getMessage();
    exit;
}

if ($_POST) {

    if (isset($_POST['org'])) {

        if(strcmp($_POST['org'], "ff")) {
            if ($_POST['ort'] AND  $_POST['name'] AND $_POST['number'] AND $_POST['type']) {
                $insert = $pdo->prepare("INSERT INTO feuerwehr (id, OrtID, Organistationskennung, Fahrzeugkennung, Fahrzeugtyp) VALUES (?,?,?,?,?)");

                $select = $pdo->prepare("SELECT COUNT(OrtID) FROM feuerwehr WHERE OrtID = ?");
                if ($select->execute(array($_POST ['ort']))) {
                    while ($row = $select->fetch()) {

                        if ($row <= 9) {
                            $row = $_POST['ort'] . "0" . $row;
                        } else {
                            $row = $_POST['ort'] . $row;
                        }
                    }
                    $insert->bindParam(1, $row);
                    $insert->bindParam(2, $_POST['ort']);
                    $insert->bindParam(3, $_POST['name']);
                    $insert->bindParam(4, $_POST['number']);
                    $insert->bindParam(5, $_POST['type']);
                    $insert->execute();
                    echo "Eintrag vom Fahrzeug erfolgreich";
                    }


            }

        }

        if(strcmp($_POST['org'], "pol")) {

        }

        if(strcmp($_POST['org'], "rt")) {

        }

        if(strcmp($_POST['org'], "thw")) {

        }
    }

}
?>