<?php
$conn = new mysqli("localhost", "root", "", "rs_lab");
if ($conn->connect_error) {
    die("DB Connection failed");
}

/* GET VIEW FILTER */
$view = $_GET['view'] ?? 'active';

/* COUNTS */
$total   = $conn->query("SELECT COUNT(*) c FROM institutions WHERE deleted_at IS NULL")->fetch_assoc()['c'];
$active  = $conn->query("SELECT COUNT(*) c FROM institutions WHERE status='active' AND deleted_at IS NULL")->fetch_assoc()['c'];
$inactive = $conn->query("SELECT COUNT(*) c FROM institutions WHERE status='inactive' AND deleted_at IS NULL")->fetch_assoc()['c'];
$deleted = $conn->query("SELECT COUNT(*) c FROM institutions WHERE deleted_at IS NOT NULL")->fetch_assoc()['c'];

/* FILTER QUERY */
if($view == 'active'){
    $result = $conn->query("SELECT * FROM institutions WHERE status='active' AND deleted_at IS NULL");
}
elseif($view == 'inactive'){
    $result = $conn->query("SELECT * FROM institutions WHERE status='inactive' AND deleted_at IS NULL");
}
elseif($view == 'deleted'){
    $result = $conn->query("SELECT * FROM institutions WHERE deleted_at IS NOT NULL");
}
else{
    $result = $conn->query("SELECT * FROM institutions WHERE deleted_at IS NULL");
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Founder Dashboard | RS Learning Lab</title>

<style>
body {
    margin: 0;
    font-family: Arial;
    background: radial-gradient(circle at top, #020617, #0f172a);
    color: #e5e7eb;
}

.container {
    max-width: 1200px;
    margin: 40px auto;
}

.cards {
    display: flex;
    gap: 20px;
    margin-bottom: 40px;
}

.card {
    flex: 1;
    padding: 25px;
    border-radius: 16px;
    background: #020617;
    cursor: pointer;
    text-align:center;
}

.total { border-left: 4px solid #60a5fa; }
.active { border-left: 4px solid #22c55e; }
.inactive { border-left: 4px solid #facc15; }
.deleted { border-left: 4px solid #ef4444; }

table {
    width:100%;
    border-collapse: collapse;
}

th,td {
    padding:12px;
    border-bottom:1px solid #1e293b;
}

.badge {
    cursor:pointer;
    font-weight:bold;
}

.badge.active { color:#22c55e; }
.badge.inactive { color:#ef4444; }

.delete-btn{
    background:red;
    color:white;
    border:none;
    padding:6px 10px;
    border-radius:6px;
    cursor:pointer;
}
</style>

</head>
<body>

<div class="container">

<h1>Founder Dashboard</h1>

<div class="cards">

<a href="?view=all" style="flex:1;text-decoration:none;color:white;">
<div class="card total">
<h2><?= $total ?></h2>
<p>Total</p>
</div>
</a>

<a href="?view=active" style="flex:1;text-decoration:none;color:white;">
<div class="card active">
<h2><?= $active ?></h2>
<p>Active</p>
</div>
</a>

<a href="?view=inactive" style="flex:1;text-decoration:none;color:white;">
<div class="card inactive">
<h2><?= $inactive ?></h2>
<p>Inactive</p>
</div>
</a>

<a href="?view=deleted" style="flex:1;text-decoration:none;color:white;">
<div class="card deleted">
<h2><?= $deleted ?></h2>
<p>Deleted</p>
</div>
</a>

</div>

<table>

<tr>
<th>Name</th>
<th>Contact</th>
<th>Phone</th>
<th>Email</th>
<th>Status</th>
<th>Action</th>
</tr>

<?php while($row = $result->fetch_assoc()): ?>

<tr>

<td>
<a href="institution_detail.php?id=<?= $row['id'] ?>" 
   style="color:#60a5fa;text-decoration:none;">
   <?= htmlspecialchars($row['institution_name']) ?>
</a>
</td>
<td><?= $row['contact_person'] ?></td>
<td><?= $row['phone'] ?></td>
<td><?= $row['email'] ?></td>

<td>
<?php if($row['deleted_at']): ?>
<span style="color:red;">Deleted</span>
<?php else: ?>
<a href="toggle_institution_status.php?id=<?= $row['id'] ?>&view=<?= $view ?>">
<span class="badge <?= $row['status'] ?>">
<?= ucfirst($row['status']) ?>
</span>
</a>
<?php endif; ?>
</td>

<td>
<?php if(!$row['deleted_at']): ?>
<form method="POST" action="delete_institution.php">
<input type="hidden" name="id" value="<?= $row['id'] ?>">
<input type="hidden" name="view" value="<?= $view ?>">
<button class="delete-btn">Delete</button>
</form>
<?php else: ?>
—
<?php endif; ?>
</td>

</tr>

<?php endwhile; ?>

</table>

</div>

</body>
</html>

<?php $conn->close(); ?>