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

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

    if($_SERVER['REQUEST_METHOD'] == "POST") {
        if(!isset($_POST['close'])){
            $station_id = sanitize_input($_POST['station_id']);
            $station_name = sanitize_input($_POST['station_name']);
            $station_pos = sanitize_input($_POST['station_pos']);
            $station_gewertet = sanitize_input($_POST['gewertet']);
            $station_direct_points = sanitize_input($_POST['direkte_punkte']);
            $station = get_station_all($con, $station_id)->fetch();
            $s_id = intval($station['s_id']);
            $name = strval($station['name']);
            $standort = strval($station['standort']);
            $gewertet = intval($station['gewertet']);
            $direct_points = intval($station['direkte_punkte']);
            if($name != $station_name) {
                update_station_name($con, $s_id, $station_name);
            }

            if($standort != $station_pos) {
                update_station_pos($con, $s_id, $station_pos);
            }

            if($gewertet != $station_gewertet) {
                update_station_gewertet($con, $s_id, $station_gewertet);
            }

            if($direct_points != $station_direct_points) {
                update_station_direct_points($con, $s_id, $station_direct_points);
            }
        }
        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>
                <div class="dropdown">
                    <label for="gewertet">Wertung:</label>
                    <select name="gewertet" id="gewertet">
                        <option value="1" <?php if($row['gewertet'] == '1'){echo " selected";}?>>Ja</option>
                        <option value="0" <?php if($row['gewertet'] == '0'){echo " selected";}?>>Nein</option>
                    </select>
                </div>
                <div class="dropdown">
                    <label for="direkte_punkte">Punkte eintragen</label>
                    <select name="direkte_punkte" id="direkte_punkte">
                        <option value="1" <?php if($row['direkte_punkte'] == '1'){echo " selected";}?>>Ja</option>
                        <option value="0" <?php if($row['direkte_punkte'] == '0'){echo " selected";}?>>Nein</option>
                    </select>
                </div>
                <input type="hidden" name="station_id" <?php echo "value=\"" . $row ['s_id'] . "\""?>/>
                <div>
                    <input type="submit" value="Speichern" class="btn-confirm"/>
                    <input type="submit" value="Schließen" class="btn-confirm" name="close">
                </div>
            </form>
        </div>
    </div>
</body>

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