<?php
$db = new SQLite3("./api/db/studiolivecode_bot.db");
$table_name = "bot";
$db->exec("CREATE TABLE IF NOT EXISTS " . $table_name . "(id INTEGER PRIMARY KEY  AUTOINCREMENT, bot_url TEXT, bot_dns TEXT, bot_status TEXT, bot_name TEXT)");

$res = $db->query("SELECT COUNT(*) as count FROM " . $table_name);
$row = $res->fetchArray();
$numRows = $row["count"];

if ($numRows == 0) {
    $db->exec("INSERT INTO " . $table_name . "(id, bot_url, bot_dns, bot_status, bot_name) VALUES('1', '', '', '0', 'SLC IBO')");
}

$res = $db->query("SELECT * FROM " . $table_name . " WHERE id='1'");
$row = $res->fetchArray();
$bot_url = $row["bot_url"];
$bot_dns = $row["bot_dns"];
$bot_status = $row["bot_status"];
$bot_name = $row["bot_name"];

if (isset($_POST["submit"])) {
    $db->exec("UPDATE " . $table_name . " SET bot_url='" . $_POST["bot_url"] . "', bot_dns='" . $_POST["bot_dns"] . "', bot_status='" . $_POST["bot_status"] . "', bot_name='" . $_POST["bot_name"] . "' WHERE id='1'");
    header("Location: teste.php?r=atualizado");
    exit;
}

?>
<?php include './includes/header.php';?>
<div class="card radius-10">
                  <div class="card-body">
                    <form method="post">
                    <div class="form-group mb-4">
                        <label for="url"><strong>Status Atual: <b style="color: green;"><?php echo $row['bot_status'] == 1 ? "Ativo" : "Inativo";?></b></strong></label>
                        <select name="bot_status" class="form-select">
                          <option value="1" <?php echo $row['bot_status'] == 1 ? "selected" : "";?>>Ativo</option>
                          <option value="0" <?php echo $row['bot_status'] == 0 ? "selected" : "Inativo";?>>Inativo</option>
                        </select>
                      </div>
                        <div class="form-group mb-4">
                            <h6 class="m-0 font-weight-bold"><i class="fa fa-user"></i> Configuração Teste Automático</h6>
                    <br>No servidor configure uma mensagem com o texto: <strong>api_cadastro</strong> e uma resposta assim: <strong>{USERNAME}|{PASSWORD}</strong> usando a barra vertical para separar o usuário da senha
                        </div>
                        <div class="form-group mb-4">
                            <label class="control-label" for="name"><strong>Link do Chatbot</strong></label>
                            <div class="input-group">
                                <input type="text" class="form-control" name="bot_url" value="<?php echo $bot_url; ?>" placeholder="Link do Chatbot">
                            </div>
                        </div>
                        <div class="form-group mb-4">
                            <label class="control-label" for="username"><strong>DNS do Servidor</strong></label>
                            <div class="input-group">
                                <input type="text" class="form-control" name="bot_dns" value="<?php echo $bot_dns; ?>" placeholder="DNS do Servidor">
                            </div>
                        </div>
                        <div class="form-group mb-4">
                            <label class="control-label" for="username"><strong>Nome da Lista</strong></label>
                            <div class="input-group">
                                <input type="text" class="form-control" name="bot_name" value="<?php echo $bot_name; ?>" placeholder="Nome da Lista">
                            </div>
                        </div>
                        <button type="submit" name="submit" class="btn btn-primary mr-2"><i class='bx bx-check mr-1'></i>Editar</button>
                    </form>
                </div>
                </div>
</div>

<?php 
 if (isset($_GET["r"])) {
          $result = $_GET["r"];
switch ($result) {
            case "atualizado":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'Teste Configurado com Sucesso!'
})</script>";
              }
            }
            ?>
				
<?php include './includes/footer.php';?>