<?php
/*$link = mysqli_connect("localhost", "01_user", "01_password", "01_db");
$admin_name = 'admin';
$admin_password = password_hash('admin123', PASSWORD_DEFAULT);
$sql = "INSERT INTO admin (admin_name, admin_password) VALUES (?, ?)";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, 'ss', $admin_name, $admin_password);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
echo "Admin user created!";*/
?>

<?php
session_start();
//require_once 'admin-config.php';
require_once '../admin/admin-config.php';

// Enable error logging
ini_set('log_errors', 1);
ini_set('error_log', '../error.log');


$admin_name = $admin_password = '';
$admin_name_err = $admin_password_err = $login_err = '';
$show_success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	// Log login attempt
	error_log("Login attempt for user: " . ($_POST['admin_name'] ?? 'empty'));
	
	// Validate admin name
	if (empty(trim($_POST['admin_name']))) {
		$admin_name_err = 'Please enter admin name.';
	} else {
		$admin_name = trim($_POST['admin_name']);
	}

	// Validate password
	if (empty(trim($_POST['admin_password']))) {
		$admin_password_err = 'Please enter your password.';
	} else {
		$admin_password = trim($_POST['admin_password']);
	}

	// Validate credentials
	if (empty($admin_name_err) && empty($admin_password_err)) {
		error_log("Attempting database lookup for admin: " . $admin_name);
		
		$sql = 'SELECT id, admin_name, admin_password FROM admin WHERE admin_name = ?';
		if ($stmt = mysqli_prepare($link, $sql)) {
			mysqli_stmt_bind_param($stmt, 's', $admin_name);
			mysqli_stmt_execute($stmt);
			mysqli_stmt_store_result($stmt);
			
			error_log("Query executed, rows found: " . mysqli_stmt_num_rows($stmt));
			
			if (mysqli_stmt_num_rows($stmt) == 1) {
				mysqli_stmt_bind_result($stmt, $id, $admin_name, $hashed_password);
				if (mysqli_stmt_fetch($stmt)) {
					error_log("Admin found in database, verifying password");
					if (password_verify($admin_password, $hashed_password)) {
						// Password is correct, start session
						$_SESSION['loggedin'] = true;
						$_SESSION['admin_id'] = $id;
						$_SESSION['admin_name'] = $admin_name;
						
						// Log successful login
						error_log("Admin login successful: " . $admin_name);
						
						header('Location: admin-dashboard.php');
						exit;
					} else {
						error_log("Password verification failed for admin: " . $admin_name);
						$login_err = 'Invalid username or password.';
					}
				}
			} else {
				error_log("Admin not found in database: " . $admin_name);
				$login_err = 'Invalid username or password.';
			}
			mysqli_stmt_close($stmt);
		} else {
			error_log("Database prepare statement failed: " . mysqli_error($link));
			$login_err = 'Database error occurred.';
		}
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Portal | Shubank Gaming</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 1rem;
            overflow-x: hidden;
        }

        .login-container {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            border: 1px solid rgba(255, 255, 255, 0.2);
            padding: 3rem 2rem;
            width: 100%;
            max-width: 420px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            position: relative;
            overflow: hidden;
        }

        .login-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
        }

        .logo-section {
            text-align: center;
            margin-bottom: 2.5rem;
        }

        .logo {
            font-size: 2rem;
            font-weight: 800;
            color: white;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 0.5rem;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
        }

        .logo i {
            color: #4facfe;
            font-size: 2.2rem;
        }

        .subtitle {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1rem;
            font-weight: 500;
        }

        .form-group {
            margin-bottom: 1.5rem;
            position: relative;
        }

        .form-label {
            display: block;
            color: white;
            font-weight: 600;
            margin-bottom: 0.5rem;
            font-size: 0.9rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .form-input {
            width: 100%;
            padding: 1rem;
            padding-left: 3rem;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 12px;
            color: white;
            font-size: 1rem;
            font-weight: 500;
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }

        .form-input:focus {
            outline: none;
            border-color: #4facfe;
            background: rgba(255, 255, 255, 0.15);
            box-shadow: 0 0 20px rgba(79, 172, 254, 0.3);
        }

        .form-input::placeholder {
            color: rgba(255, 255, 255, 0.6);
            font-weight: 400;
        }

        .input-icon {
            position: absolute;
            left: 1rem;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.7);
            font-size: 1rem;
            z-index: 1;
            margin-top: 1.25rem;
        }

        .password-toggle {
            position: absolute;
            right: 1rem;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.7);
            cursor: pointer;
            font-size: 1rem;
            z-index: 2;
            margin-top: 1.25rem;
            transition: color 0.3s ease;
        }

        .password-toggle:hover {
            color: #4facfe;
        }

        .login-btn {
            width: 100%;
            padding: 1rem;
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            border: none;
            border-radius: 12px;
            color: white;
            font-size: 1rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 8px 20px rgba(79, 172, 254, 0.3);
            margin-top: 1rem;
            position: relative;
            overflow: hidden;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 12px 25px rgba(79, 172, 254, 0.4);
        }

        .login-btn:active {
            transform: translateY(0);
        }

        .login-btn.loading {
            color: transparent;
        }

        .login-btn .spinner {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 20px;
            height: 20px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-top: 2px solid white;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            display: none;
        }

        .login-btn.loading .spinner {
            display: block;
        }

        @keyframes spin {
            0% { transform: translate(-50%, -50%) rotate(0deg); }
            100% { transform: translate(-50%, -50%) rotate(360deg); }
        }

        .alert {
            padding: 1rem;
            border-radius: 12px;
            margin-bottom: 1.5rem;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            animation: slideIn 0.3s ease;
        }

        .alert-error {
            background: rgba(239, 68, 68, 0.2);
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #fecaca;
        }

        .alert-success {
            background: rgba(34, 197, 94, 0.2);
            border: 1px solid rgba(34, 197, 94, 0.3);
            color: #bbf7d0;
        }

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

        .footer-text {
            text-align: center;
            color: rgba(255, 255, 255, 0.6);
            font-size: 0.85rem;
            margin-top: 2rem;
            font-weight: 400;
        }

        .security-badge {
            position: absolute;
            top: 1rem;
            right: 1rem;
            background: rgba(34, 197, 94, 0.2);
            color: #bbf7d0;
            padding: 0.5rem 1rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            display: flex;
            align-items: center;
            gap: 0.25rem;
        }

        /* Mobile Responsiveness */
        @media (max-width: 480px) {
            body {
                padding: 0.5rem;
            }

            .login-container {
                padding: 2rem 1.5rem;
                margin: 0.5rem;
            }

            .logo {
                font-size: 1.75rem;
            }

            .logo i {
                font-size: 1.9rem;
            }

            .form-input {
                padding: 0.875rem;
                padding-left: 2.75rem;
                font-size: 0.95rem;
            }

            .input-icon {
                left: 0.875rem;
                font-size: 0.9rem;
            }

            .password-toggle {
                right: 0.875rem;
                font-size: 0.9rem;
            }

            .login-btn {
                padding: 0.875rem;
                font-size: 0.95rem;
            }

            .security-badge {
                position: static;
                margin-bottom: 1rem;
                justify-content: center;
            }
        }

        @media (max-width: 320px) {
            .login-container {
                padding: 1.5rem 1rem;
            }

            .logo {
                font-size: 1.5rem;
            }

            .form-input {
                padding: 0.75rem;
                padding-left: 2.5rem;
            }
        }

        /* Dark mode enhancements */
        @media (prefers-color-scheme: dark) {
            .login-container {
                background: rgba(17, 24, 39, 0.8);
                border-color: rgba(255, 255, 255, 0.1);
            }
        }

        /* Focus styles for accessibility */
        .form-input:focus-visible,
        .login-btn:focus-visible,
        .password-toggle:focus-visible {
            outline: 2px solid #4facfe;
            outline-offset: 2px;
        }

        /* High contrast mode support */
        @media (prefers-contrast: high) {
            .login-container {
                border: 2px solid white;
            }
            
            .form-input {
                border: 2px solid rgba(255, 255, 255, 0.5);
            }
        }

        /* Reduced motion support */
        @media (prefers-reduced-motion: reduce) {
            .login-btn,
            .form-input,
            .alert,
            .spinner {
                animation: none;
                transition: none;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="security-badge">
            <i class="fas fa-shield-alt"></i>
            Secure Login
        </div>

        <div class="logo-section">
            <div class="logo">
                <i class="fas fa-user-shield"></i>
                Admin Portal
            </div>
            <div class="subtitle">Shubank Gaming Administration</div>
        </div>

        <?php if (!empty($login_err)): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-triangle"></i>
                <?php echo htmlspecialchars($login_err); ?>
            </div>
        <?php endif; ?>

        <?php if (!empty($admin_name_err)): ?>
            <div class="alert alert-error">
                <i class="fas fa-user"></i>
                <?php echo htmlspecialchars($admin_name_err); ?>
            </div>
        <?php endif; ?>

        <?php if (!empty($admin_password_err)): ?>
            <div class="alert alert-error">
                <i class="fas fa-lock"></i>
                <?php echo htmlspecialchars($admin_password_err); ?>
            </div>
        <?php endif; ?>

        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id="loginForm">
            <div class="form-group">
                <label for="admin_name" class="form-label">Admin Username</label>
                <div style="position: relative;">
                    <i class="fas fa-user input-icon"></i>
                    <input 
                        type="text" 
                        name="admin_name" 
                        id="admin_name" 
                        class="form-input"
                        placeholder="Enter admin username"
                        value="<?php echo htmlspecialchars($admin_name); ?>"
                        autocomplete="username"
                        required
                    >
                </div>
            </div>

            <div class="form-group">
                <label for="admin_password" class="form-label">Password</label>
                <div style="position: relative;">
                    <i class="fas fa-lock input-icon"></i>
                    <input 
                        type="password" 
                        name="admin_password" 
                        id="admin_password" 
                        class="form-input"
                        placeholder="Enter your password"
                        autocomplete="current-password"
                        required
                    >
                    <i class="fas fa-eye password-toggle" id="togglePassword" onclick="togglePasswordVisibility()"></i>
                </div>
            </div>

            <button type="submit" class="login-btn" id="loginBtn">
                <span id="btnText">Sign In</span>
                <div class="spinner"></div>
            </button>
        </form>

        <div class="footer-text">
            Protected by advanced security protocols<br>
            <i class="fas fa-lock"></i> SSL Encrypted Connection
        </div>
    </div>

    <script>
        // Password visibility toggle
        function togglePasswordVisibility() {
            const passwordInput = document.getElementById('admin_password');
            const toggleIcon = document.getElementById('togglePassword');
            
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                toggleIcon.classList.remove('fa-eye');
                toggleIcon.classList.add('fa-eye-slash');
            } else {
                passwordInput.type = 'password';
                toggleIcon.classList.remove('fa-eye-slash');
                toggleIcon.classList.add('fa-eye');
            }
        }

        // Form submission with loading state
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            const loginBtn = document.getElementById('loginBtn');
            const btnText = document.getElementById('btnText');
            
            loginBtn.classList.add('loading');
            loginBtn.disabled = true;
            btnText.textContent = 'Signing In...';
        });

        // Auto-focus on first empty field
        document.addEventListener('DOMContentLoaded', function() {
            const adminNameInput = document.getElementById('admin_name');
            const adminPasswordInput = document.getElementById('admin_password');
            
            if (!adminNameInput.value) {
                adminNameInput.focus();
            } else {
                adminPasswordInput.focus();
            }
        });

        // Enhanced security: Clear form on page unload
        window.addEventListener('beforeunload', function() {
            document.getElementById('admin_password').value = '';
        });

        // Keyboard shortcuts
        document.addEventListener('keydown', function(e) {
            // Alt + L to focus on login button
            if (e.altKey && e.key === 'l') {
                e.preventDefault();
                document.getElementById('loginBtn').focus();
            }
        });
    </script>
</body>
</html>