<?php


$user_db = './api/.user.db';
$db = new SQLite3($user_db);

//table name
$table_name = "users";

//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, user VARCHAR(100), pass TEXT, expire 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 user='".$_POST['user']."',
										pass='".$_POST['pass']."',
										expire='".$_POST['expire']."'
									WHERE
										id='".$_GET['update']."'");
	$db->close();
	header("Location: {$base_file}");
}

//submit new
if (isset($_POST['submit'])){
	$db->exec("INSERT INTO {$table_name}(user, pass, expire) VALUES('".$_POST['user']."', '".$_POST['pass']."', '".$_POST['expire']."')");
	header("Location: {$base_file}");
	}

//delete row
if(isset($_GET['delete'])){
	$db->exec("DELETE FROM {$table_name} WHERE id=".$_GET['delete']);
	header("Location: ". basename($_SERVER["SCRIPT_NAME"])."?status=1");
}


//premiumizeme key

$db->exec("CREATE TABLE IF NOT EXISTS key(id INTEGER PRIMARY KEY NOT NULL, api VARCHAR(50))");
$rowsc = $db->query("SELECT COUNT(*) as count FROM key");
$rowk = $rowsc->fetchArray();
$numRowsk = $rowk['count'];
if ($numRowsk == 0){
	$db->exec("INSERT INTO key(id, api) VALUES('1' ,'API Key')");
	}

$resk = $db->query("SELECT * FROM key WHERE id='1'");
$rowK = $resk->fetchArray();

if(isset($_POST['updatek'])){
$db->exec("UPDATE key SET  api='".$_POST['api']."' WHERE id='1'");
$db->close();
header("Location: {$base_file}");
}

include ('includes/header.php');
?>
<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>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-4 mx-auto">
			<div class="card-body">
				<div class="card bg-dark text-white">
					<div class="card-header card-header-warning">
						<center>
							<h2><i class="icon icon-commenting"></i> Users</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="user">Username</label>
										<input class="form-control" id="description" name="user" type="text"/>

								</div>
								<div class="form-group">
									<label class="form-label " for="pass">Password</label>
										<input class="form-control" id="description" name="pass" type="text"/>
								</div>
								<div class="form-group ">
									<label class="form-label" for="message">Expiration </label>
									<input type="date" autocomplete="off" autocomplete="off" class="date-time-picker form-control" name="expire" placeholder="YYYY-MM-DD"/>
								</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-4 mx-auto">
			<div class="card-body">
				<div class="card bg-dark text-white">
					<div class="card-header card-header-warning">
						<center>
							<h2><i class="icon icon-bullhorn"></i> User</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="user">Username</label>
										<input class="form-control" id="description" name="user" value="<?=$rowU['user'] ?>" type="text"/>
								</div>
								<div class="form-group">
									<label class="form-label " for="pass">Password</label>
										<input class="form-control" id="description" name="pass" value="<?=$rowU['pass'] ?>" type="text"/>
								</div>
								<div class="form-group ">
									<label class="form-label" for="message">Expiration </label>
									<input type="text" autocomplete="off" readonly id="form_datetime" value="<?=$rowU['expire'] ?>" autocomplete="off" class="date-time-picker form-control" name="expire" placeholder="YYYY-MM-DD"/>
								</div>
								<div class="form-group">
									<center>
										<button class="btn btn-info " name="submitU" value="submit">
											<button type="submit" name="submit" class="btn btn-primary ">Submit</button>
										</button>
									</center>
								</div>
							</form>
					</div>
				</div>
			</div>
		</div>
<?php
 }else{
//main table/form
	 ?>

		<form  method="post">
			<div class="form-line col-md-4 px-4">
			<div class="form-group">
				<h4>https://www.premiumize.me/</h4>
				<label class="bmd-label-floating">ENTER API KEY</label>
			</div>

			<div class="input-group">
			<input type="text" class="form-control" name="api" value="<?php echo $rowK['api'] ?>">
				<span class="input-group-btn">
				<button name="updatek" class="btn btn-warning" type="submit">Submit</button>
				</span>
			</div>
		</div>
		</form>
		<br>
		<hr style="background-color:white">
		<div class="col-md-12 mx-auto">
			<div class="card-body">
				<div class="card bg-dark text-white">
					<div class="card-header card-header-warning">
						<center>
							<h2><i class="icon icon-commenting"></i> Current Users</h2>
							<a id="button" href="./<?php echo $base_file ?>?create" class="btn btn-primary">New User</a>
							</center>
						</div>
					<div class="card-body">
						<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>Expire</th>
								<th>Edit&nbsp&nbsp&nbspDelete</th>
								</tr>
							</thead>
							<?php while ($row = $res->fetchArray()) {?>
							<tbody>
								<tr>
								<td><?=$row['id'] ?></td>
								<td><?=$row['user'] ?></a></td>
								<td><?=$row['pass'] ?></td>
								<td><?=$row['expire'] ?></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>
				</div>
			</div>
		</div>
<?php }?>

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

</body>
</html>
