<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once '../config.php';

if (isset($_SESSION['affiliate_id'])) {
    header('Location: dashboard.php');
    exit;
}

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = trim($_POST['email']);
    $password = $_POST['password'];
    
    $user = $conn->query("SELECT * FROM affiliates WHERE email = '$email' AND status = 'active'")->fetch_assoc();
    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['affiliate_id'] = $user['id'];
        $_SESSION['affiliate_name'] = $user['name'];
        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'Invalid email or password';
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Affiliate Login | <?= htmlspecialchars($brand['name'] ?? 'ওয়ান হোমিওপ্যাথি') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body{background:linear-gradient(135deg,#1b4332,#2d6a4f);min-height:100vh;display:flex;align-items:center;justify-content:center;font-family:'Segoe UI',sans-serif;padding:20px}
        .box{background:#fff;border-radius:20px;padding:40px;box-shadow:0 20px 60px rgba(0,0,0,.3);max-width:400px;width:100%;animation:fadeInUp .6s ease}
        .btn-success{background:#2d6a4f;border:none;font-weight:bold}
        .btn-success:hover{background:#1b4332}
    </style>
</head>
<body>
<div class="box text-center">
    <div style="font-size:3rem">🤝</div>
    <h3>Affiliate Login</h3>
    <p class="text-muted">Login to your affiliate dashboard</p>
    
    <?php if($error): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>
    
    <form method="POST">
        <input type="email" name="email" class="form-control mb-2" placeholder="Email" required>
        <input type="password" name="password" class="form-control mb-3" placeholder="Password" required>
        <button type="submit" class="btn btn-success w-100 btn-lg">Login</button>
    </form>
    <div class="mt-3"><a href="register.php">Don't have an account? Register</a></div>
    <div class="mt-2"><a href="../index.php" class="text-muted"><i class="fas fa-home"></i> Back to Home</a></div>
</div>
</body>
</html>