<?php
/**
 * Simple Parent Registration - Minimal Version
 */

// Enable all error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Database connection
try {
    $pdo = new PDO("mysql:host=localhost;dbname=school_management;charset=utf8mb4", "root", "root");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db_status = "✅ Database connected successfully";
} catch (Exception $e) {
    $db_status = "❌ Database connection failed: " . $e->getMessage();
    die("Database connection error: " . $e->getMessage());
}

$message = '';
$message_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get form data
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    $father_name = trim($_POST['father_name'] ?? '');
    
    // Simple validation
    if (empty($username) || empty($email) || empty($password) || empty($father_name)) {
        $message = 'সবগুলো তারকা চিহ্নিত ফিল্ড পূরণ করুন!';
        $message_type = 'error';
    } elseif ($password !== $confirm_password) {
        $message = 'পাসওয়ার্ড এবং নিশ্চিতকরণ পাসওয়ার্ড মেলে না!';
        $message_type = 'error';
    } elseif (strlen($password) < 6) {
        $message = 'পাসওয়ার্ড অন্তত ৬ অক্ষরের হতে হবে!';
        $message_type = 'error';
    } else {
        try {
            // Check if username exists
            $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
            $stmt->execute([$username]);
            if ($stmt->fetch()) {
                $message = 'এই ইউজারনেমটি ইতিমধ্যে ব্যবহৃত হয়েছে!';
                $message_type = 'error';
            } else {
                // Check if email exists
                $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
                $stmt->execute([$email]);
                if ($stmt->fetch()) {
                    $message = 'এই ইমেইলটি ইতিমধ্যে ব্যবহৃত হয়েছে!';
                    $message_type = 'error';
                } else {
                    // Create user account
                    $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                    $stmt = $pdo->prepare("INSERT INTO users (username, password, email, role, status) VALUES (?, ?, ?, 'parent', 'active')");
                    $stmt->execute([$username, $hashed_password, $email]);
                    $user_id = $pdo->lastInsertId();
                    
                    // Create parent profile
                    $stmt = $pdo->prepare("INSERT INTO parents (user_id, father_name) VALUES (?, ?)");
                    $stmt->execute([$user_id, $father_name]);
                    
                    $message = 'অভিভাবক একাউন্ট সফলভাবে তৈরি হয়েছে! আপনি এখন লগইন করতে পারবেন।';
                    $message_type = 'success';
                    
                    // Clear form
                    $username = $email = $father_name = '';
                }
            }
        } catch (Exception $e) {
            $message = 'রেজিস্ট্রেশন ত্রুটি: ' . $e->getMessage();
            $message_type = 'error';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>অভিভাবক নিবন্ধন - Simple Version</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">
</head>
<body class="bg-light">
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-primary text-white text-center">
                        <h3><i class="fas fa-user-plus"></i> অভিভাবক নিবন্ধন</h3>
                        <p class="mb-0">সরল ভার্সন</p>
                    </div>
                    <div class="card-body">
                        <!-- Database Status -->
                        <div class="alert alert-info">
                            <strong>ডাটাবেস স্ট্যাটাস:</strong> <?= $db_status ?>
                        </div>
                        
                        <!-- Messages -->
                        <?php if ($message): ?>
                            <div class="alert alert-<?= $message_type === 'success' ? 'success' : 'danger' ?>">
                                <?= htmlspecialchars($message) ?>
                            </div>
                        <?php endif; ?>
                        
                        <!-- Registration Form -->
                        <form method="POST" action="">
                            <div class="mb-3">
                                <label class="form-label">ইউজারনেম <span class="text-danger">*</span></label>
                                <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="ইউজারনেম" value="<?= htmlspecialchars($username ?? '') ?>" required>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">ইমেইল <span class="text-danger">*</span></label>
                                <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="ইমেইল" value="<?= htmlspecialchars($email ?? '') ?>" required>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">পাসওয়ার্ড <span class="text-danger">*</span></label>
                                <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">
                                <label class="form-label">পাসওয়ার্ড নিশ্চিতকরণ <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-lock"></i></span>
                                    <input type="password" name="confirm_password" class="form-control" placeholder="পাসওয়ার্ড নিশ্চিতকরণ" required>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">বাবার নাম <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-user"></i></span>
                                    <input type="text" name="father_name" class="form-control" placeholder="বাবার নাম" value="<?= htmlspecialchars($father_name ?? '') ?>" required>
                                </div>
                            </div>
                            
                            <button type="submit" class="btn btn-primary w-100 btn-lg">
                                <i class="fas fa-user-plus"></i> নিবন্ধন করুন
                            </button>
                        </form>
                        
                        <div class="text-center mt-3">
                            <a href="parent_login.php" class="btn btn-outline-secondary">লগইন পেজে ফিরে যান</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>