<?php 

$db = new SQLite3("./api/db/studiolivecode_snoop.db");
$res = $db->query("SELECT * FROM logs");

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

//table name
$table_name = "logs";

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

//create if not
$db->exec("CREATE TABLE IF NOT EXISTS snoop (id INTEGER PRIMARY KEY, username TEXT, macaddress TEXT, section TEXT, section_category TEXT, report_title TEXT, report_sub_title TEXT, report_cases TEXT, report_custom_message TEXT, stream_name TEXT, stream_id INTEGER)");

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

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

if(isset($_GET['deletetbl'])){
	$db->exec("DELETE FROM {$table_name}");
	header("Location: snoop.php?r=excluido_all");
}

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

?>
<?php include './includes/header.php';?>
<div class="card radius-10">
<div class="card-body">
<h4 class="card-title">Snoop Logs</h4>
					<div class="col-12">
                	<center>
						
	        			<a class="text-light btn btn-danger btn-ok" data-bs-toggle="modal" data-bs-target="#deletetbl"><i class="fa fa-trash-o"></i>&nbspDelete all records!!!</a>
	        		</center>
	        		<!-- Modal -->
										<div class="modal fade" id="deletetbl" 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 todos Registros?</div>
													<div class="modal-footer">
														<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Não</button>
                <a href="./<?php echo $base_file ?>?deletetbl" class="btn btn-danger btn-ok">Sim</a>
													</div>
												</div>
											</div>
										</div>
    			</div>
				<br>                                     				    
                                        <div class="table-responsive">
                                            <table class="table mb-0 align-middle">
                                                <thead>
                                                    <tr>
                                                        <th>Date</th>
                                                        <th>Type Access</th>
                                                        <th>IP Address</th>
                                                        <th>ISP</th>
                                                        <th>State</th>
                                                        <th>Country</th>
                                                        <th>Delete</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                <?php
                                                while ($row = $res->fetchArray()) {
                                                ?>
                                                <tr>
                                                    <td><?php echo $row["date"]; ?></td>
                                                    <td><?php echo $row["access"]; ?></td>
                                                    <td><?php echo $row["ipaddress"]; ?></td>
                                                    <td><?php echo $row["isp"]; ?></td>
                                                    <td><?php echo $row["state"]; ?></td>
                                                    <td>
                                                        <?php
                                                        if (empty($row["country"])) {
                                                            echo "Desconhecido";
                                                        } else {
                                                            echo $row["country"];
                                                        }
                                                        ?>
                                                    </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="snoop.php?delete=<?=$row['id'] ?>" class="btn btn-danger btn-ok">Sim</a>
													</div>
												</div>
											</div>
										</div>
										</td>
                                                </tr>
                                                <?php
                                                }
                                                ?>
                                                </tbody>
                                            </table>
                                            
						</div>
					</div>
				</div>
				
<script type="text/javascript">
//delete modal
    $('#confirm-delete').on('show.bs.modal', function(e) {
        $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
    });
</script>
<?php 
 if (isset($_GET["r"])) {
          $result = $_GET["r"];
switch ($result) {
                case "excluido":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'Log Excluído com Sucesso!'
})</script>";
                break;
                case "excluido_all":
            echo "<script>
            const Toast = Swal.mixin({
  toast: true,
  position: 'bottom',
  showConfirmButton: false,
  timer: 2000,
})
Toast.fire({
  icon: 'success',
  title: 'Todos Logs Excluídos com Sucesso!'
})</script>";
                break;
              }
            }
            ?>
				
<?php include './includes/footer.php';?>