<?php
define('_ISVALID', true);
include_once 'f_core/config.core.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if ($username && $password) {
        $sql = "SELECT * FROM db_accountuser WHERE usr_user = ? AND usr_password = MD5(?) AND usr_active = 1";
        $user = $class_database->execute($sql, [$username, $password]);
        
        if ($user && count($user) > 0) {
            $_SESSION['usr_id'] = $user[0]['usr_id'];
            $_SESSION['usr_user'] = $user[0]['usr_user'];
            $_SESSION['usr_key'] = $user[0]['usr_key'];
            
            header('Location: /');
            exit;
        } else {
            $error = 'Invalid username or password';
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Sign In - EasyStream</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; }
        .container { max-width: 400px; margin: 50px auto; background: white; padding: 40px; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
        .logo { text-align: center; margin-bottom: 30px; }
        .logo img { max-height: 60px; }
        h1 { text-align: center; color: #333; margin-bottom: 30px; }
        .form-group { margin-bottom: 20px; }
        label { display: block; margin-bottom: 8px; font-weight: bold; color: #555; }
        input[type="text"], input[type="password"] { width: 100%; padding: 15px; border: 2px solid #ddd; border-radius: 8px; font-size: 16px; box-sizing: border-box; }
        input:focus { border-color: #007bff; outline: none; }
        .btn { width: 100%; padding: 15px; background: #007bff; color: white; border: none; border-radius: 8px; font-size: 18px; font-weight: bold; cursor: pointer; }
        .btn:hover { background: #0056b3; }
        .error { background: #f8d7da; color: #721c24; padding: 15px; border-radius: 8px; margin-bottom: 20px; text-align: center; }
        .test-info { background: #e7f3ff; padding: 20px; border-radius: 8px; margin-top: 25px; text-align: center; }
        .links { text-align: center; margin-top: 20px; }
        .links a { color: #007bff; text-decoration: none; font-weight: bold; }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">
            <img src="/f_scripts/fe/img/logo-header-blue.svg" alt="EasyStream">
        </div>
        <h1>🔑 Sign In to EasyStream</h1>
        
        <?php if (isset($error)): ?>
            <div class="error">❌ <?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        
        <form method="post">
            <div class="form-group">
                <label>👤 Username</label>
                <input type="text" name="username" required placeholder="Enter your username">
            </div>
            <div class="form-group">
                <label>🔒 Password</label>
                <input type="password" name="password" required placeholder="Enter your password">
            </div>
            <button type="submit" class="btn">🚀 Sign In</button>
        </form>
        
        <div class="test-info">
            <h4>🧪 Test Accounts</h4>
            <strong>admin</strong> / admin123 (Administrator)<br>
            <strong>creator1</strong> / creator123 (Creator)<br>
            <strong>viewer1</strong> / viewer123 (Viewer)
        </div>
        
        <div class="links">
            <a href="/">🏠 Back to Homepage</a>
        </div>
    </div>
</body>
</html>