<?php
session_start();

$db = new SQLite3('./api/.db.db');
$res = $db->query("SELECT * 
                  FROM USERS 
                  WHERE ID='1'");
$row = $res->fetchArray(SQLITE3_ASSOC);

$message = '<div class="alert alert-success" id="flash-msg"><h4><i class="icon fa fa-check"></i>altercação foi um sucesso!</h4></div>';
if (isset($_POST['submit'])) {
    $db->exec("UPDATE USERS 
                SET USERNAME='" . $_POST['username'] . "', 
                    PASSWORD='" . $_POST['password'] . "' 
                WHERE ID='1'");
    session_regenerate_id();
    $_SESSION['loggedinLaucheV'] = TRUE;
    $_SESSION['name'] = $_POST['username'];
    header("Location: user_update.php?message=" . urlencode($message));
    exit();
}

include('includes/header.php');
?>
<div class="content-wrapper">
    <div class="container pt-5">
        <?php if (isset($_GET['message'])) { echo urldecode($_GET['message']); } ?>
        <div class="card mb-3">
            <div class="card-header">
                <i class="fa fa-address-book"></i>Alterar nome de usuário e senha</div>
            <div class="card-body">
                <div>
                    <div class="row">
                        <div class="col-12 text-center">
                            <h5>editar Usuário e Senha</h5>
                        </div>
                        <div class="col-md-8 mx-auto">
                            <form method="post">
                                <div class="form-group">
                                    <label class="control-label" for="username">
                                        Usuario
                                    </label>
                                    <div class="input-group">
                                        <input type="text" class="form-control" id="description" name="username" value="<?= isset($row['USERNAME']) ? htmlspecialchars($row['USERNAME']) : '' ?>" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="password">
                                        Senha
                                    </label>
                                    <div class="input-group">
                                        <input type="text" class="form-control" name="password" id="date" value="<?= isset($row['PASSWORD']) ? htmlspecialchars($row['PASSWORD']) : '' ?>" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div>
                                        <button class="btn btn-primary" name="submit" type="submit">
                                            <i class="fa fa-check"></i> Salvar Nova Configuração
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <br><br><br>
<?php include('includes/footer.php'); ?>