<?php 
include ('includes/header.php');

//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	AUTOINCREMENT  NOT NULL, title VARCHAR(100), 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='{$_POST['id']}'");
	$db->close();
	//header("Location: {$base_file}");
	echo "<script>window.location.href='dns.php';</script>";
    exit;
}

//submit new
if (isset($_POST['submit'])){
	$db->exec("INSERT INTO {$table_name}(title, url) VALUES('{$_POST['title']}', '{$_POST['url']}')");
	//header("Location: {$base_file}");
	echo "<script>window.location.href='dns.php';</script>";
    exit;
}

//delete row
if(isset($_GET['delete'])){
	$db->exec("DELETE FROM {$table_name} WHERE id={$_GET['delete']}");
	//header("Location: {$base_file}");
	echo "<script>window.location.href='dns.php';</script>";
    exit;
}

?>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content" style="background-color: black;">
			<div class="modal-header">
				<h2 style="color: white;">Confirm</h2>
			</div>
			<div class="modal-body" style="color: white;">
				Do you really want to delete?
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
				<a style="color: white;" class="btn btn-danger btn-ok">Delete</a>
			</div>
		</div>
	</div>
</div>
<?php if (isset($_GET['create'])){?>
<div class="login-box">
  <h2>Add DNS</h2>
  <form method="post">
    <div class="user-box">
      <input type="text" name="title">
      <label>Tile</label>
    </div>
    <div class="user-box">
      <input type="text" name="url">
      <label>DNS Url</label>
    </div>  
    <div class="col-12"> <button type="submit" name="submit" class="btn btn-primary btn-block logn-btn">Submit</button> </div>
  </form>
</div>
<?php }else if (isset($_GET['update'])){ ?>
<div class="login-box">
  <h2>Edit DNS</h2>
  <form method="post">
      <input type="hidden" name="id" value="<?=$_GET['update'] ?>">
    <div class="user-box">
      <input type="text" name="title" value="<?=$rowU['title'] ?>">
      <label>Tile</label>
    </div>
    <div class="user-box">
      <input type="text" name="url" value="<?=$rowU['url'] ?>">
      <label>DNS Url</label>
    </div>  
    <div class="col-12"> <button type="submit" name="submitU" class="btn btn-primary btn-block logn-btn">Submit</button> </div>
  </form>
</div>
<?php }else{ ?>
	<div class="col-md-12 mx-auto">
		<center>
			<h1 class="colorboard"></i>Current DNSs 🌐</h1>
			<a id="button" href="./<?php echo $base_file ?>?create" class="btn btn-primary">New DNS</a>
		</center>
		<br>
		<div class="table-responsive">
			<table class="table table-striped table-sm">
			<thead style="color:white!important">
				<tr>
				<th>Index</th>
				<th>Title</th>
				<th>DNS</th>
				<th>Edit&nbsp&nbsp&nbspDelete</th>
				</tr>
			</thead>
			<?php while ($row = $res->fetchArray()) {?>
			<tbody>
				<tr>
				<td><?=$row['id'] ?></td>
				<td><?=$row['title'] ?></a></td>
				<td><?=$row['url'] ?></td>
				<td>
				<a class="btn btn-info btn-ok" href="<?php echo $base_file ?>?update=<?=$row['id'] ?>"><i class="fa fa-pencil-square-o"></i></a>
				&nbsp&nbsp&nbsp
				<a class="btn btn-danger btn-ok" href="#" data-href="<?php echo $base_file ?>?delete=<?=$row['id'] ?>" data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash-o"></i></a>
				</td>
				</tr>
			</tbody>
			<?php }?>
			</table>
		</div>
	</div>
<?php }?>
<?php include ('includes/footer.php');?>
</body>
</html>