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

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

    if($_SERVER['REQUEST_METHOD'] == "POST") {
        $user = get_user($con, $_POST['id'])->fetch();

        if($user['user_name'] != $_POST['user_name']) {
            change_user_name($con, $_POST['id'], $_POST['user_name']);
        }

        if($user['user_group'] != $_POST['user_group']) {
            if($_POST['user_group'] == "station") {
                change_user_group($con, $_POST['id'], $_POST['user_group'], $_POST['bind_station']);
            } else {
                change_user_group($con, $_POST['id'], $_POST['user_group'], NULL);
            }
        }

        if(!empty($_POST['password'])) {
            $phash = generate_password_hash($_POST['password'], $user['salt']);
            change_password($con, $_POST['id'], $phash);
        }

        header("Location: manage_user.php");
        die;
    }

?>
<body>
    <div class="center">
        <div class="headline">
            <h2>Benutzer bearbeiten</h2>
        </div>
        <div class="form_div">
            <form method="post">
                <div class="txt_field">
                    <input name="user_name" type="text" value=<?php echo "\"" . $row['user_name'] . "\"";?> required/>
                    <span></span>
                    <label for="user_name">Benutzername</label>
                </div>
                <div class="txt_field_opt">
                    <input placeholder=" " type="password" name="password" />
                    <span></span>
                    <label for="password">Neues Passwort:</label>
                </div>
                <div class="dropdown">
                    <label for="user_group">Benutzergruppe:</label>
                    <select name="user_group" id="user_group">
                        <option value="station" <?php if($row['user_group'] == "station"){echo " selected";}?>>Station</option>
                        <option value="statistics" <?php if($row['user_group'] == "statistics") {echo " selected";}?>>Statistik</option>
                        <option value="admin" <?php if($row['user_group'] == "admin") {echo " selected";}?>>Admin</option>
                    </select>
                </div>
                <div class="dropdown">
                    <label for="bind_station">Gebunden an Station (nur für Gruppe Station)</label>
                    <select name="bind_station" id="bind_station">
                        <?php
                            if($row['s_id'] == null) {
                                load_options_stations($con, "", false);
                            } else {
                                load_options_stations($con, $row['s_id'], false);
                            }
                        ?>
                    </select>
                </div>
                <input type="hidden" value=<?php echo "\"" . $row['id'] . "\""?> name="id"/>
                <input type="submit" value="ändern" class="btn-confirm"/>
            </form>
        </div>
    </div>
</body>
<?php
    $con = null;
    echo file_get_contents("header_footer/footer.html");
?>