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

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    
    // Validation
    if (empty($name) || empty($email) || empty($password)) {
        $error = 'Name, email and password are required';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Invalid email address';
    } elseif (strlen($password) < 6) {
        $error = 'Password must be at least 6 characters';
    } elseif ($password !== $confirm_password) {
        $error = 'Passwords do not match';
    } else {
        // Check if email exists
        $check = $conn->query("SELECT id FROM affiliates WHERE email = '$email'");
        if ($check && $check->num_rows > 0) {
            $error = 'Email already registered. Please login.';
        } else {
            // Generate unique referral code
            do {
                $referral_code = strtoupper(substr(md5(uniqid()), 0, 8));
                $code_check = $conn->query("SELECT id FROM affiliates WHERE referral_code = '$referral_code'");
            } while ($code_check && $code_check->num_rows > 0);
            
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $insert = $conn->query("INSERT INTO affiliates (name, email, phone, password, referral_code) 
                                    VALUES ('$name', '$email', '$phone', '$hashed_password', '$referral_code')");
            
            if ($insert) {
                $success = 'Registration successful! Your referral code is: <strong>' . $referral_code . '</strong>';
                // Auto login
                $_SESSION['affiliate_id'] = $conn->insert_id;
                header('Location: dashboard.php');
                exit;
            } else {
                $error = 'Registration failed. Please try again.';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Affiliate Registration | <?= 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:480px;width:100%;animation:fadeInUp .6s ease}
        @keyframes fadeInUp{from{opacity:0;transform:translateY(30px)}to{opacity:1;transform:translateY(0)}}
        .btn-primary{background:#2d6a4f;border:none;font-weight:bold}
        .btn-primary:hover{background:#1b4332}
    </style>
</head>
<body>
<div class="box text-center">
    <div style="font-size:3rem">🤝</div>
    <h3>Become an Affiliate</h3>
    <p class="text-muted">Earn commissions by referring customers</p>
    
    <?php if($error): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>
    <?php if($success): ?>
        <div class="alert alert-success"><?= $success ?></div>
    <?php endif; ?>
    
    <form method="POST">
        <div class="mb-2"><input type="text" name="name" class="form-control" placeholder="Full Name *" required></div>
        <div class="mb-2"><input type="email" name="email" class="form-control" placeholder="Email *" required></div>
        <div class="mb-2"><input type="tel" name="phone" class="form-control" placeholder="Phone"></div>
        <div class="mb-2"><input type="password" name="password" class="form-control" placeholder="Password (min 6 chars) *" required></div>
        <div class="mb-3"><input type="password" name="confirm_password" class="form-control" placeholder="Confirm Password *" required></div>
        <button type="submit" class="btn btn-primary w-100 btn-lg">Register as Affiliate</button>
    </form>
    <div class="mt-3">
        <a href="login.php">Already have an account? Login</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>