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

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

$search = trim($_GET['q'] ?? '');

$page  = max(1, (int)($_GET['page'] ?? 1));
$limit = 10;
$offset = ($page - 1) * $limit;

/* WHERE */
$where = "WHERE e.role='sales_executive'";

if ($search !== '') {
    $s = $conn->real_escape_string($search);
    $where .= " AND (e.name LIKE '%{$s}%' OR e.email LIKE '%{$s}%')";
}

/* --------------------------
   MAIN QUERY
---------------------------*/

$sql = "
SELECT 
e.id,
e.name,
e.email,
m.name AS manager_name

FROM users e

LEFT JOIN users m 
ON e.requested_manager_id = m.id

$where

ORDER BY e.id DESC
LIMIT $limit OFFSET $offset
";

$res = $conn->query($sql);

/* COUNT */

$countRow = $conn->query("
SELECT COUNT(*) as total
FROM users e
$where
")->fetch_assoc();

$count = $countRow['total'];
$totalPages = max(1, ceil($count / $limit));
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Sales Executives</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>

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

.container{
margin-left:260px;
padding:80px;
}

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

.page-title{
font-size:22px;
font-weight:700;
margin-bottom:20px;
color:#111;
}

.search{
display:flex;
gap:10px;
margin-bottom:20px;
}

.search input{
padding:10px 12px;
border-radius:8px;
border:1px solid #ddd;
width:780px;
font-size:16px;
}

.search input:focus{
outline:none;
border-color:#3b82f6;
}

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

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

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

tr:hover{
background:#fafafa;
}

.pagination{
margin-top:20px;
}

.pagination button{
padding:7px 12px;
margin-right:6px;
border:none;
background:#f2f4f8;
border-radius:6px;
cursor:pointer;
}

.pagination button:hover{
background:#3b82f6;
color:white;
}
.btn{
padding:10px 14px;
border:none;
border-radius:6px;
cursor:pointer;
font-size:13px;
}

.btn-primary{
background:#000;
color:white;
}
</style>
</head>

<body>

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

<div class="container">

<div class="card">

<div class="page-title" style="color:#000">Sales Executives</div>

<div class="search">
<form method="get" id="searchForm">

<input
type="text"
name="q"
placeholder="Search executive..."
value="<?php echo htmlspecialchars($search); ?>"
onkeyup="autoSearch()"
autocomplete="off">
<button class="btn btn-primary">Search</button>
</form>
</div>

<table>

<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Sales Manager</th>
</tr>
</thead>

<tbody>

<?php if ($res && $res->num_rows > 0): ?>

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

<tr>

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

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

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

<td>
<?php
echo $row['manager_name']
? htmlspecialchars($row['manager_name'])
: "<span style='color:#888'>Not Assigned</span>";
?>
</td>

</tr>

<?php endwhile; ?>

<?php else: ?>

<tr>
<td colspan="4" style="color:#666">No sales executives found.</td>
</tr>

<?php endif; ?>

</tbody>

</table>

<div class="pagination">

<?php for ($i = 1; $i <= $totalPages; $i++): ?>

<a href="?page=<?php echo $i; ?>&q=<?php echo urlencode($search); ?>">

<button <?php if ($i == $page) echo 'style="background:#3b82f6;color:#fff"'; ?>>

<?php echo $i; ?>

</button>

</a>

<?php endfor; ?>

</div>

</div>

</div>

<script>

function autoSearch(){
clearTimeout(window.timer);
window.timer = setTimeout(function(){
document.getElementById('searchForm').submit();
},400);
}

</script>

</body>
</html>