<?php

include ('includes/header.php');

//table name
$table_name = "playlist";

//table call
$res = $db->select($table_name, '*', '', '');
$query = "SELECT p.id, p.username, p.password, p.mac_address, p.dns_id, d.url 
          FROM playlist p
          LEFT JOIN dns d ON p.dns_id = d.id";
$res = $db->customQuery($query);

//update call
@$resU = $db->select($table_name, '*', 'id = :id', '', [':id' => $_GET['update']]);

if(isset($_POST['submitU'])){
	unset($_POST['submitU']);
	$updateData = $_POST;
	$db->update($table_name, $updateData, 'id = :id',[':id' => $_GET['update']]);
	echo "<script>window.location.href='". basename($_SERVER["SCRIPT_NAME"])."?status=1'</script>";
}

//submit new
if (isset($_POST['submit'])){
	unset($_POST['submit']);
	$db->insert($table_name, $_POST);
	$db->close();
	echo "<script>window.location.href='". basename($_SERVER["SCRIPT_NAME"])."?status=1'</script>";
}

//delete row
if(isset($_GET['delete'])){
	$db->delete($table_name, 'id = :id',[':id' => $_GET['delete']]);
	echo "<script>window.location.href='". basename($_SERVER["SCRIPT_NAME"])."?status=2'</script>";
}

//delete modal
?>
<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">
            <div class="modal-header">
                <h2>Confirm</h2>
            </div>
            <div class="modal-body">
                Do you really want to delete?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>
<?php
if (isset($_GET['create'])){

//create form
?>
        <div class="col-md-8 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-user"></i> User's</h2>
                        </center>
                    </div>
                    
                    <div class="card-body">
                        <div class="col-12">
                            <h3>Create User</h3>
                        </div>
                            <form method="post">
                                <div class="form-group">
                                    <label class="form-label " for="username">Username</label>
                                        <input class="form-control" id="description" name="username" placeholder="Username" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="password">Password</label>
                                        <input class="form-control" id="description" name="password" placeholder="Password" type="text"/>
                                </div>
								<div class="form-group">
									<label class="form-label" for="mac_address">MAC Address</label>
										<input class="form-control" id="description" name="mac_address" placeholder="00:1A:2B:3C:4D:5E" type="text" pattern="([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})" title="Please enter a valid MAC address (e.g., 00:1A:2B:3C:4D:5E)" />
								</div>

                                <div class="form-group">
                                    <center>
                                        <button class="btn btn-info " name="submit" type="submit">
                                            <i class="icon icon-check"></i> Submit
                                        </button>
                                    </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>

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

//update form
?>
        <div class="col-md-8 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-user"></i> User's</h2>
                        </center>
                    </div>
                    
                    <div class="card-body">
                        <div class="col-12">
                            <h3>Edit User</h3>
                        </div>
                            <form method="post">
                                <div class="form-group">
                                    <label class="form-label " for="username">Username</label>
                                        <input class="form-control" id="description" name="username" placeholder="Username" value="<?=$resU[0]['username'] ?>" type="text"/>
                                </div>
                                <div class="form-group">
                                    <label class="form-label " for="password">Password</label>
                                        <input class="form-control" id="description" name="password" placeholder="Password" value="<?=$resU[0]['password'] ?>" type="text"/>
                                </div>
								 <div class="form-group">
                                    <label class="form-label " for="mac_address">Mac Address</label>
                                        <input class="form-control" id="description" name="mac_address" placeholder="00:1A:2B:3C:4D:5E" value="<?= isset($resU[0]['mac_address']) ? $resU[0]['mac_address'] : '' ?>"  type="text" pattern="([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})" title="Please enter a valid MAC address (e.g., 00:1A:2B:3C:4D:5E)" />
                                </div>
                                <div class="form-group">
                                    <center>
                                        <button class="btn btn-info " name="submitU" type="submit">
                                            <i class="icon icon-check"></i> Submit
                                        </button>
                                    </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>
<?php
 }else{
//main table/form
	 ?>
        <div class="col-md-12 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-commenting"></i> DNS's</h2>
                        </center>
                    </div>

                    <div class="card-body">
                        <div class="col-12">
                        	<center>
	        					<a id="button" href="./<?=basename($_SERVER["SCRIPT_NAME"]) ?>?create" class="btn btn-info">New User</a>
	        				</center>
    					</div>
						<br>
						<div class="table-responsive">
							<table class="table table-striped table-sm">
							<thead style="color:white!important">
								<tr>
								<th>Index</th>
								<th>Username</th>
								<th>Password</th>
								<th>Mac Address</th>
								<th>DNS</th>
								<th>Edit&nbsp&nbsp&nbspDelete</th>
								</tr>
							</thead>
							<?php foreach ($res as $row) {?>
							<tbody>
								<tr>
								<td><?=$row['id'] ?></td>
								<td><?=$row['username'] ?></a></td>
								<td>••••••••</td>
								<td><?=$row['mac_address'] ?></td>
								<td><?=$row['url'] ?></td>
								<td>
								<a class="btn btn-info btn-ok" href="./<?=basename($_SERVER["SCRIPT_NAME"]) ?>?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="./<?=basename($_SERVER["SCRIPT_NAME"]) ?>?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>
				</div>
			</div>
		</div>
<?php }?>

<?php include ('includes/footer.php');?>

</body>
</html>