<?php
    session_start();
    include("../scripts/connection.php");
    include("../scripts/functions.php");
    $user_data = check_admin($con);

    if($_SERVER['REQUEST_METHOD'] == "GET") {
        $row = get_station_all($con, $_GET['s_id'])->fetch();
        include("header_footer/header.php");
    }

    if($_SERVER['REQUEST_METHOD'] == "POST") {
        $station = get_station_all($con, $_POST['station_id'])->fetch();
        $s_id = intval($station['s_id']);
        $name = strval($station['name']);
        $standort = strval($station['standort']);
        if($name != $_POST['station_name']) {
            update_station_name($con, $s_id, $_POST['station_name']);
        }

        if($standort != $_POST['station_pos']) {
            update_station_pos($con, $s_id, $_POST['station_pos']);
        }
        header("Location: stationen.php");
        die;
    }
?>

<body>
    <div class="center">
        <div class="headline">
            <h2>Station bearbeiten</h2>
        </div>
        <div class="form_div">
            <form method="post">
                <div class="txt_field">
                    <input type="text" name="station_name" <?php echo "value=\"" . $row['name'] . "\"" ?> />
                    <span></span>
                    <label for="station_name">Stations Namen:</label>
                </div>
                <div class="txt_field">
                    <input type="text" name="station_pos" <?php echo "value=\"" . $row['standort'] . "\""?>/>
                    <span></span>
                    <label for="station_pos">Stations Standort:</label>
                </div>
                <input type="hidden" name="station_id" <?php echo "value=\"" . $row ['s_id'] . "\""?>/>
                <input type="submit" value="Speichern" class="btn-confirm"/>
            </form>
        </div>
    </div>
</body>

<?php
    $con = null;
    echo file_get_contents("header_footer/footer.html");
?>