<?php
// =========================================================
// ADMIN LOGIN PAGE
// =========================================================
if (session_status() === PHP_SESSION_NONE) session_start();

// If already logged in, redirect to admin panel
if (isset($_SESSION['admin'])) {
    header('Location: opd/admin/admin.php');
    exit;
}

require_once 'config.php';

$error = '';
$brand = getBrand();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (empty($username) || empty($password)) {
        $error = 'Please enter username and password';
    } else {
        $username_esc = $conn->real_escape_string($username);
        $user = $conn->query("SELECT * FROM admin_users WHERE username = '$username_esc' AND is_active = 1")->fetch_assoc();
        
        if ($user && password_verify($password, $user['password'])) {
            // Set admin session
            $_SESSION['admin'] = $user['username'];
            $_SESSION['admin_id'] = $user['id'];
            $_SESSION['admin_role'] = $user['role'];
            
            // Redirect to admin panel
            header('Location: admin.php');
            exit;
        } else {
            $error = 'Invalid username or password';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login | <?= htmlspecialchars($brand['name'] ?? 'ওয়ান হোমিওপ্যাথি') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        /* =========================================================
           LOGIN PAGE STYLES
           ========================================================= */
        body {
            background: linear-gradient(135deg, #1b4332, #2d6a4f, #40916c);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', 'Hind Siliguri', sans-serif;
            padding: 20px;
            margin: 0;
        }
        
        .login-box {
            background: #fff;
            border-radius: 24px;
            padding: 45px 40px;
            max-width: 420px;
            width: 100%;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            animation: fadeInUp 0.6s ease;
            position: relative;
            overflow: hidden;
        }
        
        .login-box::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #2d6a4f, #52b788, #2d6a4f);
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .brand-icon {
            font-size: 4rem;
            margin-bottom: 10px;
            display: block;
        }
        
        .brand-name {
            font-weight: 700;
            color: #2d6a4f;
            font-size: 1.5rem;
            margin-bottom: 5px;
        }
        
        .brand-sub {
            color: #6c757d;
            font-size: 0.9rem;
            margin-bottom: 25px;
        }
        
        .form-control {
            border-radius: 12px;
            padding: 12px 15px;
            border: 1px solid #ddd;
            transition: all 0.3s;
        }
        
        .form-control:focus {
            border-color: #2d6a4f;
            box-shadow: 0 0 0 3px rgba(45,106,79,0.15);
        }
        
        .input-group-text {
            background: #f8f9fa;
            border-radius: 12px 0 0 12px;
            border: 1px solid #ddd;
            border-right: none;
            color: #6c757d;
        }
        
        .input-group .form-control {
            border-radius: 0 12px 12px 0;
        }
        
        .btn-login {
            background: #2d6a4f;
            color: #fff;
            font-weight: 700;
            padding: 14px;
            border-radius: 30px;
            transition: all 0.3s;
            border: none;
            font-size: 1.1rem;
            width: 100%;
            cursor: pointer;
        }
        
        .btn-login:hover {
            background: #1b4332;
            color: #fff;
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(45,106,79,0.3);
        }
        
        .btn-login:active {
            transform: translateY(0);
        }
        
        .alert {
            border-radius: 12px;
            padding: 12px 15px;
            font-size: 0.95rem;
        }
        
        .alert-danger {
            background: #f8d7da;
            border-color: #f5c6cb;
            color: #721c24;
        }
        
        .login-footer {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #eee;
            text-align: center;
        }
        
        .login-footer a {
            color: #6c757d;
            text-decoration: none;
            transition: color 0.3s;
            font-size: 0.9rem;
        }
        
        .login-footer a:hover {
            color: #2d6a4f;
        }
        
        .login-footer .home-link {
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        
        .login-footer .home-link i {
            font-size: 0.9rem;
        }
        
        /* Password toggle */
        .password-toggle {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: #6c757d;
            cursor: pointer;
            padding: 5px;
        }
        
        .password-toggle:hover {
            color: #2d6a4f;
        }
        
        .password-wrapper {
            position: relative;
        }
        
        .password-wrapper .form-control {
            padding-right: 45px;
        }
        
        /* Responsive */
        @media (max-width: 576px) {
            .login-box {
                padding: 30px 20px;
            }
            
            .brand-icon {
                font-size: 3rem;
            }
            
            .brand-name {
                font-size: 1.2rem;
            }
        }
    </style>
</head>
<body>
<div class="login-box text-center">
    <span class="brand-icon">🏥</span>
    <div class="brand-name"><?= htmlspecialchars($brand['name'] ?? 'ওয়ান হোমিওপ্যাথি') ?></div>
    <div class="brand-sub">Admin Login</div>
    
    <?php if ($error): ?>
        <div class="alert alert-danger text-start">
            <i class="fas fa-exclamation-circle me-2"></i> <?= htmlspecialchars($error) ?>
        </div>
    <?php endif; ?>
    
    <form method="POST" action="" autocomplete="off">
        <div class="mb-3">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-user"></i></span>
                <input type="text" name="username" class="form-control" placeholder="Username" required autofocus>
            </div>
        </div>
        
        <div class="mb-3">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-lock"></i></span>
                <div class="password-wrapper flex-grow-1">
                    <input type="password" name="password" id="password" class="form-control" placeholder="Password" required>
                    <button type="button" class="password-toggle" onclick="togglePassword()" tabindex="-1">
                        <i class="fas fa-eye" id="passwordIcon"></i>
                    </button>
                </div>
            </div>
        </div>
        
        <button type="submit" class="btn-login">
            <i class="fas fa-sign-in-alt me-2"></i> Login
        </button>
    </form>
    
    <div class="login-footer">
        <a href="../index.php" class="home-link">
            <i class="fas fa-home"></i> Back to Home
        </a>
    </div>
</div>

<script>
// =========================================================
// PASSWORD TOGGLE
// =========================================================
function togglePassword() {
    const passwordInput = document.getElementById('password');
    const icon = document.getElementById('passwordIcon');
    
    if (passwordInput.type === 'password') {
        passwordInput.type = 'text';
        icon.className = 'fas fa-eye-slash';
    } else {
        passwordInput.type = 'password';
        icon.className = 'fas fa-eye';
    }
}
</script>

</body>
</html>