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

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

$search = $_GET['q'] ?? "";

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

$where = "WHERE m.role='sales_manager'";

if($search!=""){
$where.=" AND (m.name LIKE '%$search%' OR m.email LIKE '%$search%')";
}

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

$sql="
SELECT m.id,
       m.name,
       m.email,
       a.name AS agency_name
FROM users m
LEFT JOIN users a
ON m.requested_agency_id = a.id
$where
ORDER BY m.id DESC
LIMIT $limit OFFSET $offset
";

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

/* COUNT */

$count=$conn->query("
SELECT COUNT(*) as total
FROM users m
$where
")->fetch_assoc()['total'];

$totalPages = ceil($count/$limit);

?>

<!DOCTYPE html>
<html>
<head>

<title>Sales Managers</title>

<style>

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

.container{
margin-left:240px;
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;
}

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

.btn-primary{
background:#000;
color:white;
}

.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;
}

</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 Managers
</div>

<div class="search">

<form method="get" id="searchForm">

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

</div>

<table>

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

<?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['agency_name']
? htmlspecialchars($row['agency_name'])
: "<span style='color:#888'>Not Assigned</span>";
?>
</td>

</tr>

<?php } ?>

</table>

<br>

<div class="pagination">

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

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

<button><?php echo $i;?></button>

</a>

<?php } ?>

</div>

</div>

</div>

</body>
<script>
function autoSearch() {
    clearTimeout(window.searchTimer);

    window.searchTimer = setTimeout(function () {
        document.getElementById("searchForm").submit();
    }, 500); // delay 0.5s
}
</script>
</html>