<?php
session_start();
require_once("../config/db.php");

/*
 ASSUMPTION:
 - Founder already logged in
 - institutions table has:
   id, institution_name, email, username, password, institution_type, status
*/

// 🔒 BASIC ADMIN PROTECTION (simple)
if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: admin_login.php");
    exit;
}

$message = "";

// 🔑 GENERATE CREDENTIALS
if (isset($_POST['generate_credentials'])) {

    $institution_id = $_POST['institution_id'];

    // fetch institution
    $stmt = $pdo->prepare("
        SELECT institution_name, institution_type 
        FROM institutions 
        WHERE id = ?
    ");
    $stmt->execute([$institution_id]);
    $inst = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($inst) {

        // username pattern
        $username = "inst_" . $institution_id;

        // temporary password
        $temp_password = strtoupper(substr(md5(time()), 0, 6));
        $hashed_password = password_hash($temp_password, PASSWORD_DEFAULT);

        // update institution record
        $update = $pdo->prepare("
            UPDATE institutions 
            SET username = ?, password = ?, status = 'active'
            WHERE id = ?
        ");
        $update->execute([$username, $hashed_password, $institution_id]);

        // login URL (FINAL)
        $login_url = "http://localhost/rs_lab/institution/login.php";

        // activation message
        $message = "
Hello 👋

Your institution has been activated on RS Learning Lab.

🏫 Institution Type: {$inst['institution_type']}

🔐 Institution Login Details:
🌐 Login URL: {$login_url}
👤 Username: {$username}
🔑 Temporary Password: {$temp_password}

Please log in and change your password immediately after first login.

– RS Learning Lab Team
        ";
    }
}

// fetch all institutions
$list = $pdo->query("
    SELECT id, institution_name, email, institution_type, status 
    FROM institutions 
    ORDER BY id DESC
")->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>
<html>
<head>
    <title>Founder Dashboard | RS Learning Lab</title>
    <style>
        body { font-family: Arial; background:#f4f6f8; }
        table { border-collapse: collapse; width: 100%; background:#fff; }
        th, td { border:1px solid #ddd; padding:10px; text-align:left; }
        th { background:#0b8c9f; color:white; }
        .btn { padding:6px 12px; cursor:pointer; }
        .btn-gen { background:#ff7a00; color:white; border:none; }
        textarea { width:100%; height:180px; }
    </style>
</head>

<body>

<h2>🧠 RS Learning Lab – Founder Dashboard</h2>

<table>
    <tr>
        <th>ID</th>
        <th>Institution</th>
        <th>Email</th>
        <th>Type</th>
        <th>Status</th>
        <th>Action</th>
    </tr>

    <?php foreach ($list as $row): ?>
    <tr>
        <td><?= $row['id'] ?></td>
        <td><?= htmlspecialchars($row['institution_name']) ?></td>
        <td><?= htmlspecialchars($row['email']) ?></td>
        <td><?= ucfirst($row['institution_type']) ?></td>
        <td><?= $row['status'] ?></td>
        <td>
            <form method="POST" style="margin:0;">
                <input type="hidden" name="institution_id" value="<?= $row['id'] ?>">
                <button class="btn btn-gen" name="generate_credentials">
                    Generate Credentials
                </button>
            </form>
        </td>
    </tr>
    <?php endforeach; ?>
</table>

<?php if ($message): ?>
    <h3>📩 Activation Message</h3>
    <textarea readonly><?= trim($message) ?></textarea><br><br>
    <button onclick="copyMsg()">Copy Message</button>
<?php endif; ?>

<script>
function copyMsg() {
    const ta = document.querySelector("textarea");
    ta.select();
    document.execCommand("copy");
    alert("Message copied!");
}
</script>

</body>
</html>
