<?php
require_once 'db_config.php';

/* ===============================
   GET → SHOW REGISTRATION FORM
   =============================== */
if ($_SERVER['REQUEST_METHOD'] === 'GET') {

    $type = $_GET['type'] ?? '';

    if ($type !== 'school' && $type !== 'college') {
        die("Invalid access");
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Register Institution | RS Learning Lab</title>
        <style>
            body {
                background: radial-gradient(circle at top, #0c1a2b, #050b14);
                color: #eaf2ff;
                font-family: Inter, Arial;
            }
            .box {
                width: 420px;
                margin: 80px auto;
                background: rgba(255,255,255,0.05);
                padding: 30px;
                border-radius: 14px;
                border: 1px solid rgba(255,255,255,0.08);
            }
            h2 { margin-bottom: 20px; }
            input, textarea, button {
                width: 100%;
                padding: 12px;
                margin-top: 12px;
                border-radius: 8px;
                border: none;
            }
            input, textarea {
                background: #0f172a;
                color: #fff;
            }
            textarea {
                resize: none;
                height: 80px;
            }
            button {
                background: linear-gradient(135deg, #1ec9ff, #1b9cff);
                font-weight: bold;
                cursor: pointer;
            }
        </style>
    </head>
    <body>

    <div class="box">
        <h2><?php echo ucfirst($type); ?> Registration</h2>

        <form method="POST">
            <input type="hidden" name="institution_type" value="<?php echo $type; ?>">

            <input type="text" name="institution_name" placeholder="Institution Name" required>
            <input type="text" name="contact_person" placeholder="Contact Person Name" required>
            <textarea name="institution_address" placeholder="School / College Address" required></textarea>
            <input type="email" name="email" placeholder="Official Email" required>
            <input type="text" name="phone" placeholder="Contact Number">

            <button type="submit">Submit Registration</button>
        </form>
    </div>

    </body>
    </html>
    <?php
    exit;
}

/* ===============================
   POST → INSERT INTO DB
   =============================== */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $institution_name    = trim($_POST['institution_name'] ?? '');
    $contact_person      = trim($_POST['contact_person'] ?? '');
    $institution_address = trim($_POST['institution_address'] ?? '');
    $email               = trim($_POST['email'] ?? '');
    $phone               = trim($_POST['phone'] ?? '');

    if ($institution_name === '' || $contact_person === '' || $institution_address === '' || $email === '') {
        die("Missing required fields");
    }

    /* AUTO GENERATE USERNAME */
    $username = strtolower(str_replace(" ", "", $institution_name)) . rand(100,999);

    /* TEMP PASSWORD */
    $tempPassword = substr(str_shuffle("ABCDEFGHJKLMNPQRSTUVWXYZ23456789"), 0, 6);

    /* HASH */
    $hashedPassword = password_hash($tempPassword, PASSWORD_DEFAULT);

    /* 🔥 UPDATED SQL (ADDED temp_password COLUMN) */
    $sql = "INSERT INTO institutions 
    (institution_name, contact_person, institution_address, email, phone, username, password, password_hash, temp_password)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt = $pdo->prepare($sql);

    if ($stmt->execute([
        $institution_name,
        $contact_person,
        $institution_address,
        $email,
        $phone,
        $username,
        $tempPassword,
        $hashedPassword,
        $tempPassword   // 🔥 IMPORTANT
    ])) {
        ?>
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>Registration Successful</title>
        </head>
        <body>

        <div style="text-align:center; margin-top:100px;">
            <h2>Registration Successful ✅</h2>
            <p>We will share login credentials shortly.</p>
        </div>

        </body>
        </html>
        <?php
    } else {
        echo "Registration failed.";
    }
    exit;
}