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

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php");
    exit;
}

$msg="";

/* Approve RA */
if(isset($_POST['approve_id'])){
    $id=(int)$_POST['approve_id'];
    $stmt=$conn->prepare("UPDATE users SET is_approved=1 WHERE id=? AND role='ra'");
    $stmt->bind_param("i",$id);
    $stmt->execute();
    $msg="RA Panel account approved successfully.";
}

/* Reject RA */
if(isset($_POST['reject_id'])){
    $id=(int)$_POST['reject_id'];
    $stmt=$conn->prepare("DELETE FROM users WHERE id=? AND role='ra'");
    $stmt->bind_param("i",$id);
    $stmt->execute();
    $msg="RA Panel request rejected and removed.";
}

/* Pending RA accounts */
$stmt=$conn->prepare("SELECT id,name,email FROM users WHERE role='ra' AND is_approved=0 ORDER BY id DESC");
$stmt->execute();
$result=$stmt->get_result();
?>

<!DOCTYPE html>
<html>
<head>

<title>RA Panel Requests</title>

<style>

body{
background:#f4f6fb;
font-family:Inter,Arial;
}

/* MAIN WRAPPER */

.main-content{
margin-left:260px;
padding:30px;
}

/* CARD */

.card{
background:#fff;
border-radius:12px;
padding:25px;
box-shadow:0 10px 25px rgba(0,0,0,0.05);
}

/* HEADER */

.page-title{
font-size:22px;
font-weight:700;
margin-bottom:15px;
}

/* MESSAGE */

.msg{
background:#e8f7ee;
padding:12px;
border-radius:8px;
color:#0a7a3d;
margin-bottom:15px;
}

/* TABLE */

table{
width:100%;
border-collapse:collapse;
margin-top:10px;
}

th{
text-align:left;
background:#f2f4f8;
padding:12px;
font-size:14px;
}

td{
padding:12px;
border-bottom:1px solid #eee;
}

/* BUTTONS */

.btn{
padding:7px 14px;
border:none;
border-radius:6px;
cursor:pointer;
font-size:13px;
}

.approve{
background:#2ecc71;
color:white;
}

.reject{
background:#e74c3c;
color:white;
margin-left:5px;
}

.approve:hover{
background:#27ae60;
}

.reject:hover{
background:#c0392b;
}

/* EMPTY STATE */

.empty{
text-align:center;
padding:40px;
color:#777;
}

</style>

</head>

<body>

<?php include "adminsidebar.php"; ?>
<?php include "../header.php"; ?>

<div class="main-content">

<div class="card">

<div class="page-title" style="color: black;">
  RA Panel Requests
</div>

<?php if($msg!=""){ ?>

<div class="msg">
<?php echo $msg; ?>
</div>

<?php } ?>


<?php if($result->num_rows==0){ ?>

<div class="empty">
No pending RA Panel requests
</div>

<?php } else { ?>

<table>

<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>

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

<tr>

<td><?php echo $row['id']; ?></td>

<td><?php echo htmlspecialchars($row['name']); ?></td>

<td><?php echo htmlspecialchars($row['email']); ?></td>

<td>

<form method="post" style="display:inline;">
<input type="hidden" name="approve_id" value="<?php echo $row['id']; ?>">
<button class="btn approve">Approve</button>
</form>

<form method="post" style="display:inline;">
<input type="hidden" name="reject_id" value="<?php echo $row['id']; ?>">
<button class="btn reject">Reject</button>
</form>

</td>

</tr>

<?php } ?>

</table>

<?php } ?>

</div>

</div>

</body>
</html>