<?php 

//db call
$db = new SQLite3("./api/db/studiolivecode_dns.db");

//table name
$table_name = "dns";

//current file var
$base_file = basename($_SERVER["SCRIPT_NAME"]);

//create if not
$db->exec("CREATE TABLE IF NOT EXISTS {$table_name}(id INTEGER PRIMARY KEY NOT NULL, title TEXT, url TEXT)");

//table call
$res = $db->query("SELECT * FROM {$table_name}");

//update call
@$resU = $db->query("SELECT * FROM {$table_name} WHERE id='".$_GET['update']."'");
@$rowU=$resU->fetchArray();
if(isset($_POST['submitU'])){
	$db->exec("UPDATE {$table_name} SET title='".$_POST['title']."',
										url='".$_POST['url']."'
									WHERE 
										id='".$_GET['update']."'");
	$db->close();
	header("Location: dns.php?r=atualizado");
}

//submit new
if (isset($_POST['submit'])){
	
	$db->exec("INSERT INTO {$table_name}(title, url) VALUES('".$_POST['title']."', '".$_POST['url']."')");
	header("Location: dns.php?r=adicionado");
	}

//delete row
if(isset($_GET['delete'])){
	$db->exec("DELETE FROM {$table_name} WHERE id=".$_GET['delete']);
	header("Location: dns.php?r=excluido");
}

?>
<?php include './includes/header.php';?>
<div class="card radius-10">
<?php
if (isset($_GET['create'])){

//create form
?>
                  <div class="card-body">
                    <h4 class="card-title">Adicionar DNS</h4>
                    <form class="forms-sample" method="post" enctype="multipart/form-data">
                      <div class="form-group mb-4">
                        <label for="title"><strong>Servidor</strong></label>
                        <input type="text" placeholder="Digite o nome do Servidor" class="form-control" name="title" id="title">
                      </div>
                      <div class="form-group mb-4">
                        <label for="url"><strong>DNS</strong></label>
                        <input type="text" class="form-control" placeholder="Digite o DNS" name="url" id="url">
                      </div>
                      <button type="submit" name="submit" class="btn btn-primary mr-2"><i class='bx bx-check mr-1'></i>Enviar</button>
                    </form>
                  </div>
</div>

<?php 
}else if (isset($_GET['update'])){ 

//update form
?>
                  <div class="card-body">
                    <h4 class="card-title">Editar DNS</h4>
                    <form class="forms-sample" method="post" enctype="multipart/form-data">
                      <div class="form-group mb-4">
                        <label for="title"><strong>Servidor</strong></label>
                        <input type="text" class="form-control" placeholder="Digite o nome do Servidor" name="title" id="title" value="<?=$rowU['title'] ?>">
                      </div>
                      <div class="form-group mb-4">
                        <label for="url"><strong>DNS</strong></label>
                        <input type="text" class="form-control" placeholder="Digite o DNS" name="url" id="url" value="<?=$rowU['url'] ?>">
                      </div>
                      <button type="submit" name="submitU" class="btn btn-primary mr-2"><i class='bx bx-check mr-1'></i>Enviar</button>
                    </form>
                  </div>
</div>
<?php
 }else{
//main table/form
	 ?>
        <div class="card-header border-bottom-0 bg-transparent">
						<div class="d-flex align-items-center">
							<div>
								<h5 class="font-weight-bold mb-0">DNS Cadastrados</h5>
							</div>
							<div class="ms-auto">
								<a href="./dns.php?create"><button type="button" class="btn btn-white radius-10">Adicionar DNS</button><a/>
							</div>
						</div>
					</div>
					<div class="card-body">
						<div class="table-responsive">
							<table class="table mb-0 align-middle table-sm">
								<thead>
                          <tr>
							<!--	<th>Index</th> -->
								<th><strong>Servidor</strong></th>
								<th><strong>DNS</strong></th>
								<th><strong>Edit</strong></th>
								    
								<th><strong>Delete</strong></th>
								</tr>
                        </thead>
                        <?php while ($row = $res->fetchArray()) {?>
							<tbody>
								<tr>
								<!-- <td><?=$row['id'] ?></td> -->
								<td><?=$row['title'] ?></a></td>
								<td><?=$row['url'] ?></td>
								<td>
                           <div class="d-flex order-actions">
                          <a href="dns.php?update=<?=$row['id'] ?>" class="text-light bg-primary border-0"><i class='bx bxs-edit' ></i></a></div></td>
								<td>
                           <div class="d-flex order-actions">
                          <a class="text-light bg-danger border-0 btn-ok" data-bs-toggle="modal" data-bs-target="#confirm-delete-<?=$row['id']?>"><i class="bx bxs-trash"></i></a></div>
                          <!-- Modal -->
										<div class="modal fade" id="confirm-delete-<?=$row['id']?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
											<div class="modal-dialog">
												<div class="modal-content">
													<div class="modal-header">
														<h5 class="modal-title" id="exampleModalLabel">Confirmar</h5>
													</div>
													<div class="modal-body">Você realmente deseja excluir?</div>
													<div class="modal-footer">
														<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Não</button>
                <a href="dns.php?delete=<?=$row['id'] ?>" class="btn btn-danger btn-ok">Sim</a>
													</div>
												</div>
											</div>
										</div>
										</td>
								</tr>
							</tbody>
							<?php }?>
							</table>
						</div>
					</div>
				</div>
<?php }?>

<?php 
 if (isset($_GET["r"])) {
          $result = $_GET["r"];
switch ($result) {
            case "adicionado":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'DNS Adicionado com Sucesso!'
})</script>";
                break;
                case "excluido":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'DNS Excluído com Sucesso!'
})</script>";
                break;
                case "status_on":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'Banner Desativado com Sucesso!'
})</script>";
                break;
            case "atualizado":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'DNS Atualizado com Sucesso!'
})</script>";
                break;
              }
            }
            ?>
				
<?php include './includes/footer.php';?>