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

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

$msg="";

/* APPROVE SALES EXECUTIVE */
if(isset($_POST['approve_id'])){

$id=(int)$_POST['approve_id'];

$stmt=$conn->prepare("UPDATE users 
                      SET is_approved=1,
                      approver_id=? 
                      WHERE id=? AND role='sales_executive'");

$stmt->bind_param("ii",$_SESSION['user_id'],$id);
$stmt->execute();

$msg="Sales Executive approved successfully.";

}

/* REJECT SALES EXECUTIVE */
if(isset($_POST['reject_id'])){

$id=(int)$_POST['reject_id'];

$stmt=$conn->prepare("UPDATE users 
                      SET is_approved=2,
                      approver_id=? 
                      WHERE id=? AND role='sales_executive'");

$stmt->bind_param("ii",$_SESSION['user_id'],$id);
$stmt->execute();

$msg="Sales Executive rejected.";

}

/* GET SALES EXECUTIVES WHO APPLIED FOR THIS SALES MANAGER */

$stmt=$conn->prepare("SELECT id,name,email
                      FROM users
                      WHERE role='sales_executive'
                      AND is_approved=0
                      AND requested_manager_id=?");

$stmt->bind_param("i",$_SESSION['user_id']);

$stmt->execute();
$result=$stmt->get_result();
?>

<!DOCTYPE html>
<html>
<head>

<title>Sales Executive Approval</title>

<style>

body{
font-family:Inter,Arial;
background:#f4f6fb;
margin:0;
color:#000;
}


.content{
margin-left:260px;
padding:30px;
}
.title{
font-size:22px;
font-weight:700;
margin-bottom:20px;
color:#000;
}

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

.msg{
background:#e8f7ee;
padding:10px;
border-radius:8px;
margin-bottom:15px;
color:#0a7a3d;
}
table{
width:100%;
border-collapse:collapse;
color:#000;
}

th{
background:#f1f5f9;
padding:12px;
text-align:left;
}
td{
padding:12px;
border-bottom:1px solid #eee;
color:#000;
}   

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

.approve{
background:#22c55e;
color:white;
}

.reject{
background:#ef4444;
color:white;
margin-left:6px;
}

.approve:hover{
background:#16a34a;
}

.reject:hover{
background:#dc2626;
}

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

</style>

</head>

<body>

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

<div class="content">

<div class="title">
Sales Executive Signup Requests
</div>

<div class="card">

<?php if($msg!=""){ ?>
<div class="msg"><?php echo $msg; ?></div>
<?php } ?>

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

<div class="empty">
No pending sales executive 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>