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

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

    if($_SERVER['REQUEST_METHOD'] == "POST") {
        if(isset($_POST['save'])){
            $id = sanitize_input($_POST['id']);
            $user = get_user($con, $id)->fetch();
            $user_group = sanitize_input($_POST['user_group']);
            $user_name = sanitize_input($_POST['user_name']);
            $bind_station = sanitize_input($_POST['bind_station']);
            $password = sanitize_input($_POST['password']);
            
            if($user['user_name'] != $user_name) {
                change_user_name($con, $id, $user_name);
            }
        
            if($user['user_group'] != $user_group) {
                if($user_group == "station") {
                    change_user_group($con, $id, $user_group, $bind_station);
                } else {
                    change_user_group($con, $id, $user_group, NULL);
                }
            }
        
            if($user['s_id'] != $bind_station) {
                if ($user_group == "station") {
                    change_s_id($con, $id, $bind_station);
                }
            }
        
            if(!empty($password)) {
                $phash = generate_password_hash($password, $user['salt']);
                change_password($con, $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="Speichern" class="btn-confirm" name="save"/>
                <input type="submit" value="Schließen" class="btn-confirm" name="close"/>
            </form>
        </div>
    </div>
</body>
<?php
    $con = null;
    echo file_get_contents("header_footer/footer.html");
?>