<?php
session_start();
require_once 'config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = mysqli_real_escape_string($conn, trim($_POST['name']));
    $phone = mysqli_real_escape_string($conn, trim($_POST['phone']));
    $email = mysqli_real_escape_string($conn, trim($_POST['email'] ?? ''));
    $password = $_POST['password'];
    $address = mysqli_real_escape_string($conn, trim($_POST['address'] ?? ''));
    
    // Validation
    $errors = [];
    if (empty($name)) $errors[] = "নাম দিন";
    if (empty($phone)) $errors[] = "মোবাইল নম্বর দিন";
    if (strlen($password) < 6) $errors[] = "পাসওয়ার্ড কমপক্ষে ৬ অক্ষরের হতে হবে";
    
    // Check if phone already exists
    $check = mysqli_query($conn, "SELECT id FROM patients WHERE phone = '$phone'");
    if (mysqli_num_rows($check) > 0) {
        $errors[] = "এই মোবাইল নম্বরটি ইতিমধ্যে রেজিস্টার করা আছে";
    }
    
    if (empty($errors)) {
        // Hash password
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        
        $query = "INSERT INTO patients (name, phone, email, password, address) 
                  VALUES ('$name', '$phone', '$email', '$hashed_password', '$address')";
        
        if (mysqli_query($conn, $query)) {
            $patient_id = mysqli_insert_id($conn);
            $_SESSION['patient_id'] = $patient_id;
            $_SESSION['patient_name'] = $name;
            $_SESSION['patient_phone'] = $phone;
            
            header('Location: my_appointments.php');
            exit;
        } else {
            $error = "রেজিস্ট্রেশন ব্যর্থ: " . mysqli_error($conn);
        }
    } else {
        $error = implode('<br>', $errors);
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>রোগী রেজিস্ট্রেশন</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background: linear-gradient(135deg, #1a5276, #2e86c1); min-height: 100vh; display: flex; align-items: center; justify-content: center; }
        .register-card { max-width: 450px; width: 100%; background: white; border-radius: 20px; padding: 40px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
        .register-card h3 { color: #1a5276; text-align: center; margin-bottom: 30px; }
        .btn-primary { background: linear-gradient(135deg, #1a5276, #2e86c1); border: none; width: 100%; padding: 12px; font-weight: 600; border-radius: 12px; }
        .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 25px rgba(26,82,118,0.3); }
        .form-control { border-radius: 12px; padding: 12px 16px; border: 2px solid #e8ecf1; }
        .form-control:focus { border-color: #1a5276; box-shadow: 0 0 0 4px rgba(26,82,118,0.1); }
    </style>
</head>
<body>
    <div class="register-card">
        <h3>📝 রোগী রেজিস্ট্রেশন</h3>
        <?php if (isset($error)): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="post">
            <div class="mb-3">
                <label class="form-label fw-bold">পূর্ণ নাম *</label>
                <input type="text" name="name" class="form-control" required>
            </div>
            <div class="mb-3">
                <label class="form-label fw-bold">মোবাইল নম্বর *</label>
                <input type="text" name="phone" class="form-control" required placeholder="017XXXXXXXX">
            </div>
            <div class="mb-3">
                <label class="form-label fw-bold">ইমেইল</label>
                <input type="email" name="email" class="form-control">
            </div>
            <div class="mb-3">
                <label class="form-label fw-bold">পাসওয়ার্ড *</label>
                <input type="password" name="password" class="form-control" required minlength="6">
                <small class="text-muted">কমপক্ষে ৬ অক্ষর</small>
            </div>
            <div class="mb-3">
                <label class="form-label fw-bold">ঠিকানা</label>
                <textarea name="address" class="form-control" rows="2"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">রেজিস্ট্রেশন করুন</button>
        </form>
        <div class="text-center mt-3">
            <a href="patient_login.php" class="text-decoration-none">ইতিমধ্যে অ্যাকাউন্ট আছে? লগইন করুন</a>
        </div>
    </div>
</body>
</html>