<?php
session_start();  
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Authentication Check</title>
    <link rel="stylesheet" href="../css/bootstrap/css/bootstrap.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;600;700&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #0ff;
            --primary-dark: #00ccff;
            --primary-glow: rgba(0, 255, 255, 0.5);
            --danger: #ff2c2c;
            --danger-glow: rgba(255, 44, 44, 0.3);
            --dark: #0a0a0f;
        }

        body {
            background: var(--dark);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Inter', sans-serif;
            margin: 0;
            padding: 20px;
        }

        .alert {
            background: rgba(255, 44, 44, 0.1);
            border: 1px solid var(--danger);
            border-radius: 12px;
            color: #fff;
            padding: 20px;
            text-align: center;
            max-width: 500px;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            box-shadow: 0 0 20px rgba(255, 44, 44, 0.1);
            animation: fadeIn 0.3s ease-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .alert i {
            color: var(--danger);
            font-size: 24px;
            filter: drop-shadow(0 0 8px var(--danger-glow));
        }

        .alert-text {
            font-size: 16px;
            letter-spacing: 0.5px;
            line-height: 1.5;
        }

        .loading-indicator {
            margin-top: 15px;
            color: var(--text-secondary);
            font-size: 14px;
            opacity: 0.8;
        }
    </style>
</head>
<body>

<?php 
include("../config.php");

if(isset($_POST['sub'])){
    $username = mysqli_real_escape_string($condb, $_POST['Username']);
    $password = mysqli_real_escape_string($condb, md5($_POST['Password']));
    
    if(empty($username) || empty($_POST['Password'])) {
        echo '<div class="alert">
                <i class="fas fa-shield-alt"></i>
                <span class="alert-text">Please provide both username and password to proceed with authentication.</span>
              </div>';
        echo '<meta http-equiv="refresh" content="2;url=login.php" />';
    } else {
        $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";   		 
        $query = mysqli_query($condb, $sql);
        $rows = mysqli_num_rows($query); 
    
        if($rows == 1){
            $data = mysqli_fetch_array($query);
            $_SESSION['id_user'] = $data['id_user'];
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            header("Location: index.php");
            echo '<div class="alert" style="background: rgba(0, 255, 255, 0.1); border-color: var(--primary);">
                    <i class="fas fa-shield-alt" style="color: var(--primary);"></i>
                    <span class="alert-text">Authentication successful. Establishing secure session...</span>
                  </div>';
            echo '<meta http-equiv="refresh" content="2;url=index.php" />';
        } else {
            echo '<div class="alert">
                    <i class="fas fa-shield-alt"></i>
                    <span class="alert-text">Access Denied: Invalid credentials provided. Please verify your authentication details and try again.</span>
                  </div>';
            echo '<meta http-equiv="refresh" content="2;url=login.php" />';
        }  
    }
} else {
    header("Location: login.php");
}  
?>
</body>
</html>