<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$db = new SQLite3('./api/.db.db');
$res = $db->query('SELECT * ' . "\r\n\t\t\t\t" . '  FROM ibo ' . "\r\n\t\t\t\t" . '  WHERE id=\'' . $_GET['update'] . '\'');
$row = $res->fetchArray();
$id = $row['id'];
$mac_address = $row['mac_address'];
$expire_date = $row['expire_date'];
$username = $row['username'];
$password = $row['password'];

if (isset($_POST['submit'])) {
	$we = strtotime($_POST['expire_date']);
	$ne = date('Y-m-d', $we);
	$start = date('Y-m-d');
	$end = date('h:m:s');
	$full = $start . 'T' . $end . '.000000Z';
	$created_at = date('yyyy-MM-dd\'T\'hh:mm:ss.SSSSSS\'Z\'');
	$db->exec('UPDATE ibo SET mac_address=\'' . $_POST['mac_address'] . '\',' . "\r\n\t\t\t\t\t\t\t\t" . 'expire_date=\'' . $ne . '\',' . "\r\n\t\t\t\t\t\t\t\t" . 'username=\'' . $_POST['username'] . '\',' . "\r\n\t\t\t\t\t\t\t\t" . 'url=\'' . $_POST['url'] . '\',' . "\r\n\t\t\t\t\t\t\t\t" . 'title=\'' . $_POST['title'] . '\',' . "\r\n\t\t\t\t\t\t\t\t" . 'password=\'' . $_POST['password'] . '\',' . "\r\n\t\t\t\t\t\t\t\t" . 'created_at=\'' . $full . '\'' . "\r\n\t\t\t\t\t\t" . '  WHERE ' . "\r\n\t\t\t\t\t\t\t" . '  id=\'' . $_POST['id'] . '\'');
	$db->close();
	header('Location: users.php');
}

include 'includes/header.php';
$userupdate_id = $row['id'];
$userupdate_mac_address = $row['mac_address'];
$userupdate_expire_date = $row['expire_date'];
$userupdate_username = $row['username'];
$userupdate_password = $row['password'];
$userupdate_url = $row['url'];
$userupdate_title = $row['title'];
echo '        <div class="container-fluid">' . "\n";
echo '          <h1 class="h3 mb-1 text-gray-800"> Update User</h1>' . "\n";
echo '                <div class="card border-left-primary shadow h-100 card shadow mb-4">' . "\n";
echo '                <div class="card-header py-3">' . "\n";
echo '                <h6 class="m-0 font-weight-bold text-color"><i class="fa fa-user"></i> Edit User</h6>' . "\n";
echo '                </div>' . "\n";
echo '                <div class="card-body">' . "\n";
echo '                        <form method="post">          ' . "\n";
echo '                        <div class="form-group ">' . "\n";
echo '                                    <label class="control-label " for="mac_address">' . "\n";
echo '                                        <strong>Mac Address</strong> ' . "\n";
echo '                                    </label>' . "\n";
echo '                                    <div class="input-group">' . "\n";
echo '                                        <input type="form-control text-primary" name="id" value="' . $userupdate_id . '" type="hidden">' . "\n";
echo '                                        <input class="form-control text-primary" id="description" name="mac_address" value="' . $userupdate_mac_address . '" type="text"required/>' . "\n";
echo '                                    </div>' . "\n";
echo '                                </div>' . "\n";
echo '                                <div class="form-group ">' . "\n";
echo '                                    <label class="control-label " for="username">' . "\n";
echo '                                        <strong>Username</strong>' . "\n";
echo '                                    </label>' . "\n";
echo '                                    <div class="input-group">' . "\n";
echo '                                        <input type="text" class="form-control text-primary" name="username" value="' . $userupdate_username . '" id="discription" required/>' . "\n";
echo '                                    </div>' . "\n";
echo '                                </div>' . "\n";
echo '                                <div class="form-group ">' . "\n";
echo '                                    <label class="control-label " for="password">' . "\n";
echo '                                        <strong>Password</strong>' . "\n";
echo '                                    </label>' . "\n";
echo '                                    <div class="input-group">' . "\n";
echo '                                        <input type="text" class="form-control text-primary" name="password" value="' . $userupdate_password . '" id="discription" required/>' . "\n";
echo '                                    </div>' . "\n";
echo '                                </div>' . "\n";
echo '                                <div class="form-group ">' . "\n";
echo '                                    <label class="control-label " for="expire_date">' . "\n";
echo '                                        <strong>Expiration</strong>' . "\n";
echo '                                    </label>' . "\n";
echo '                                    <div class="input-group">' . "\n";
echo '                                        <input type="text" class="form-control text-primary" name="expire_date" value="' . $userupdate_expire_date . '" id="datepicker" /> ' . "\n";
echo '                                    </div>' . "\n";
echo "\n";
echo '                                </div>' . "\n";
echo '                                <div class="form-group ">' . "\n";
echo '                                    <label class="control-label " for="dns">' . "\n";
echo '                                        <strong>DNS</strong>' . "\n";
echo '                                    </label>' . "\n";
echo '                                    <div class="input-group">' . "\n";
echo '                                        <input type="text" class="form-control text-primary" name="url" value="' . $userupdate_url . '" id="discription" required/>' . "\n";
echo '                                    </div>' . "\n";
echo '                                </div>' . "\n";
echo '                                <div class="form-group ">' . "\n";
echo '                                    <label class="control-label " for="title">' . "\n";
echo '                                        <strong>Server Name</strong>' . "\n";
echo '                                    </label>' . "\n";
echo '                                    <div class="input-group">' . "\n";
echo '                                        <input type="text" class="form-control text-primary" name="title" value="' . $userupdate_title . '" id="discription" required/>' . "\n";
echo '                                    </div>' . "\n";
echo "\n";
echo '                                </div>' . "\n";
echo '                                <div class="form-group">' . "\n";
echo '                                    <div>' . "\n";
echo '                                        <button class="btn btn-success btn-icon-split" name="submit" type="submit">' . "\n";
echo '                        <span class="icon text-white-50"><i class="fa fa-check"></i></span><span class="text">Submit</span>' . "\n";
echo '                        </button>' . "\n";
echo '                                    </div>' . "\n";
echo "\n";
echo '                                </div>' . "\n";
echo '                            </form>' . "\n";
echo '                        </div>' . "\n";
echo '                    </div>' . "\n";
echo '                </div>' . "\n";
include 'includes/footer.php';
echo '</body>' . "\n";
echo "\n";
echo '</html>';

?>