<?php
require_once 'config/database.php';

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    $first_name = trim($_POST['first_name'] ?? '');
    $last_name = trim($_POST['last_name'] ?? '');
    $email = trim($_POST['email'] ?? '');

    if (empty($username) || empty($password) || empty($first_name) || empty($last_name) || empty($email)) {
        $error = 'সব ফিল্ড পূরণ করুন';
    } else {
        try {
            $pdo->beginTransaction();

            // Create user account
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, 'teacher')");
            $stmt->execute([$username, $hashed_password, $email]);
            $user_id = $pdo->lastInsertId();

            // Create teacher record
            $stmt = $pdo->prepare("INSERT INTO teachers (user_id, employee_id, first_name, last_name, email) VALUES (?, ?, ?, ?, ?)");
            $employee_id = 'TCH' . str_pad($user_id, 4, '0', STR_PAD_LEFT);
            $stmt->execute([$user_id, $employee_id, $first_name, $last_name, $email]);

            $pdo->commit();
            $message = "শিক্ষক অ্যাকাউন্ট সফলভাবে তৈরী হয়েছে!";

        } catch (Exception $e) {
            $pdo->rollBack();
            $error = 'অ্যাকাউন্ট তৈরী করতে সমস্যা: ' . $e->getMessage();
        }
    }
}
?>

<!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">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h3><i class="fas fa-user-plus"></i> শিক্ষক অ্যাকাউন্ট তৈরী</h3>
                    </div>
                    <div class="card-body">
                        <?php if ($message): ?>
                            <div class="alert alert-success">
                                <?php echo $message; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ($error): ?>
                            <div class="alert alert-danger">
                                <?php echo $error; ?>
                            </div>
                        <?php endif; ?>

                        <form method="POST">
                            <div class="mb-3">
                                <label class="form-label">ইউজারনেম</label>
                                <input type="text" name="username" class="form-control" required>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">পাসওয়ার্ড</label>
                                <input type="password" name="password" class="form-control" required>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">নামের প্রথম অংশ</label>
                                <input type="text" name="first_name" class="form-control" required>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">নামের শেষ অংশ</label>
                                <input type="text" name="last_name" class="form-control" required>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">ইমেইল</label>
                                <input type="email" name="email" class="form-control" required>
                            </div>

                            <button type="submit" class="btn btn-primary">অ্যাকাউন্ট তৈরী করুন</button>
                        </form>

                        <hr>
                        <div class="text-center">
                            <a href="login.php" class="btn btn-secondary">লগইন পেজে যান</a>
                            <a href="check_db.php" class="btn btn-info ms-2">ডেটাবেস চেক করুন</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
