<?php
include('includes/functions.php');
$table_name = "user";

$log_check = $db->select($table_name, '*', 'id = :id', '', [':id' => 1]);
$loggedinuser = !empty($log_check) ? $log_check[0]['username'] : null;

if (!empty($loggedinuser) && isset($_SESSION['name']) && $_SESSION['name'] === $loggedinuser) {
    header("Location: main.php");
    exit;
}

$data = ['id' => '1','username' => 'admin','password' => 'admin',];
$db->insertIfEmpty($table_name, $data);

if (isset($_POST["login"])){
	$username = $_POST["username"];
	$userData = $db->select($table_name, '*', 'username = :username', '', [':username' => $username]);
	if ($userData) {
		$storedPassword = $userData[0]['password'];
		$enteredPassword = $_POST["password"];
		if ($enteredPassword == $storedPassword) {
			session_regenerate_id();
			$_SESSION['loggedin'] = TRUE;
			$_SESSION['name'] = $_POST['username'];
			if ($_POST['username'] == 'admin'){
				header('Location: user.php');
			}else{
				header('Location: main.php');
			}
		}else{
			header('Location: ./api/index.php');
		}
	}else{
		header('Location: ./api/index.php');
	}
	$db->close();
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="manifest" href="./img/site.webmanifest">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
	<link rel="stylesheet" href="./css/css.css">
    <title>Nova TiVI - Panel</title>
</head>
<style>
body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
}

.form {
  width: 360px;
  padding: 40px;
  border-radius: 20px;
  backdrop-filter: blur(15px);
  background: rgba(255, 255, 255, 0.05);
  box-shadow: 0 15px 35px rgba(0,0,0,0.5);
}

.form h2 {
  color: #fff;
  margin-bottom: 30px;
  letter-spacing: 2px;
}

.inputBox {
  margin-bottom: 20px;
}

.inputBox label {
  color: #aaa;
  font-size: 14px;
}

.inputBox input {
  width: 100%;
  padding: 12px 15px;
  border-radius: 10px;
  border: none;
  outline: none;
  background: rgba(255,255,255,0.1);
  color: #fff;
  transition: 0.3s;
}

.inputBox input:focus {
  background: rgba(255,255,255,0.2);
  box-shadow: 0 0 10px #03a9f4;
}

.inputBox input::placeholder {
  color: #ccc;
}

input[type="submit"] {
  width: 100%;
  background: linear-gradient(45deg, #03a9f4, #00e5ff);
  border: none;
  color: white;
  font-weight: bold;
  cursor: pointer;
  transition: 0.3s;
}

input[type="submit"]:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.4);
}
</style>

<br><br>
<div class="form">
  <h2>Nova TiVI</h2>
  <form method="post">
    <div class="input">
      <div class="inputBox">
        <label>Login</label>
        <input type="text" name="username" placeholder="Login"/>
      </div>
      <div class="inputBox">
        <label>Hasło</label>
        <input type="password" name="password" placeholder="&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;"/>
      </div>
      <div class="inputBox">
        <input type="submit" name="login" value="Zaloguj się!"/>
      </div>
    </div>
  </form>
</div>
<br><br>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>


