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

// If already logged in, go to dashboard
if (isset($_SESSION['opd_patient'])) {
    header('Location: dashboard.php');
    exit;
}

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

// Handle registration form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name'] ?? '');
    $mobile = trim($_POST['mobile'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    $email = trim($_POST['email'] ?? '');
    $age = intval($_POST['age'] ?? 0);
    $gender = $_POST['gender'] ?? '';
    $address = trim($_POST['address'] ?? '');

    // Validation
    if (empty($name) || empty($mobile) || empty($password) || empty($confirm_password)) {
        $error = 'নাম, মোবাইল নম্বর, পাসওয়ার্ড এবং পাসওয়ার্ড নিশ্চিতকরণ আবশ্যক।';
    } elseif (!preg_match('/^01[3-9]\d{8}$/', $mobile)) {
        $error = 'সঠিক মোবাইল নম্বর দিন (যেমন: 01712345678)';
    } elseif (strlen($password) < 4) {
        $error = 'পাসওয়ার্ড কমপক্ষে ৪ অক্ষরের হতে হবে।';
    } elseif ($password !== $confirm_password) {
        $error = 'পাসওয়ার্ড এবং নিশ্চিতকরণ পাসওয়ার্ড মিলছে না।';
    } else {
        // Check if mobile already exists
        $mobile_esc = $conn->real_escape_string($mobile);
        $check = $conn->query("SELECT id FROM opd_patients WHERE mobile = '$mobile_esc'");
        if ($check && $check->num_rows > 0) {
            $error = 'এই মোবাইল নম্বরটি ইতিমধ্যে নিবন্ধিত। দয়া করে লগইন করুন।';
        } else {
            // Hash password and insert
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $name_esc = $conn->real_escape_string($name);
            $email_esc = $conn->real_escape_string($email);
            $address_esc = $conn->real_escape_string($address);
            $gender_esc = $conn->real_escape_string($gender);

            $insert = $conn->query("INSERT INTO opd_patients 
                (name, mobile, password, email, age, gender, address, created_at) 
                VALUES 
                ('$name_esc', '$mobile_esc', '$hashed_password', '$email_esc', $age, '$gender_esc', '$address_esc', NOW())");

            if ($insert) {
                $success = '✅ নিবন্ধন সফল! এখন আপনি লগইন করতে পারেন।';
                // Optionally auto-login
                // $_SESSION['opd_patient'] = $conn->insert_id;
                // header('Location: dashboard.php');
                // exit;
            } else {
                $error = 'নিবন্ধন ব্যর্থ হয়েছে। আবার চেষ্টা করুন।';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>রোগী নিবন্ধন | <?= 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, #40916c);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', 'Hind Siliguri', sans-serif;
            padding: 20px;
        }
        .register-box {
            background: #fff;
            border-radius: 24px;
            padding: 35px 30px;
            max-width: 480px;
            width: 100%;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            animation: fadeInUp 0.6s ease;
        }
        @keyframes fadeInUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .brand-icon { font-size: 3.5rem; margin-bottom: 8px; }
        .brand-name { font-weight: 700; color: #2d6a4f; }
        .btn-register {
            background: #2d6a4f;
            color: #fff;
            font-weight: 700;
            padding: 12px;
            border-radius: 30px;
            transition: 0.3s;
            border: none;
        }
        .btn-register:hover {
            background: #1b4332;
            color: #fff;
            transform: translateY(-2px);
        }
        .login-link a {
            color: #2d6a4f;
            font-weight: 600;
            text-decoration: none;
        }
        .login-link a:hover { text-decoration: underline; }
        .alert { border-radius: 12px; }
        .form-control:focus {
            border-color: #2d6a4f;
            box-shadow: 0 0 0 0.2rem rgba(45,106,79,0.25);
        }
        .input-group-text { background: #f8f9fa; }
    </style>
</head>
<body>
<div class="register-box text-center">
    <div class="brand-icon">📝</div>
    <h3 class="brand-name"><?= htmlspecialchars($brand['name'] ?? 'ওয়ান হোমিওপ্যাথি') ?></h3>
    <p class="text-muted">রোগী নিবন্ধন</p>

    <?php if (!empty($error)): ?>
        <div class="alert alert-danger text-start"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>
    <?php if (!empty($success)): ?>
        <div class="alert alert-success text-start"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>

    <?php if (empty($success)): ?>
    <form method="POST" action="">
        <div class="mb-2">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-user"></i></span>
                <input type="text" name="name" class="form-control" placeholder="পুরো নাম *" required>
            </div>
        </div>

        <div class="mb-2">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-phone"></i></span>
                <input type="tel" name="mobile" class="form-control" placeholder="মোবাইল নম্বর *" required>
            </div>
        </div>

        <div class="mb-2">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-envelope"></i></span>
                <input type="email" name="email" class="form-control" placeholder="ইমেইল (ঐচ্ছিক)">
            </div>
        </div>

        <div class="row g-2 mb-2">
            <div class="col-6">
                <input type="number" name="age" class="form-control" placeholder="বয়স" min="1" max="150">
            </div>
            <div class="col-6">
                <select name="gender" class="form-select">
                    <option value="">লিঙ্গ</option>
                    <option value="male">পুরুষ</option>
                    <option value="female">মহিলা</option>
                    <option value="other">অন্যান্য</option>
                </select>
            </div>
        </div>

        <div class="mb-2">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
                <textarea name="address" class="form-control" placeholder="ঠিকানা (ঐচ্ছিক)" rows="2"></textarea>
            </div>
        </div>

        <div class="mb-2">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-lock"></i></span>
                <input type="password" name="password" class="form-control" placeholder="পাসওয়ার্ড (৪+ অক্ষর) *" required>
            </div>
        </div>

        <div class="mb-3">
            <div class="input-group">
                <span class="input-group-text"><i class="fas fa-check-circle"></i></span>
                <input type="password" name="confirm_password" class="form-control" placeholder="পাসওয়ার্ড আবার লিখুন *" required>
            </div>
        </div>

        <button type="submit" class="btn btn-register w-100 btn-lg">নিবন্ধন করুন</button>
    </form>

    <hr>
    <div class="login-link">
        <i class="fas fa-sign-in-alt"></i> ইতিমধ্যে অ্যাকাউন্ট আছে? <a href="index.php">লগইন করুন</a>
    </div>
    <?php endif; ?>

    <div class="home-link mt-2">
        <a href="../index.php" class="text-muted"><i class="fas fa-home"></i> হোমপেজে ফিরুন</a>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>