<?php
include("../includes/db_connection.php");
include("../includes/auth.php");
include("partials/header.php");

// Fetch associates
$result = $conn->query("SELECT * FROM associates ORDER BY id DESC");

$limit = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $limit;

$result = $conn->query("SELECT * FROM associates LIMIT $start,$limit");
$total = $conn->query("SELECT COUNT(*) as total FROM associates")->fetch_assoc()['total'];
$pages = ceil($total/$limit);

/* ===============================
   Fetch Associates with Counts
=================================*/

$sql = "
SELECT 
a.*,

-- Client Count
(SELECT COUNT(*) 
 FROM clients c 
 WHERE c.associate_id = a.id) AS total_clients,

-- Certificate Count
(SELECT COUNT(*) 
 FROM certificates cert
 LEFT JOIN clients c2 ON cert.client_id = c2.id
 WHERE c2.associate_id = a.id) AS total_certificates

FROM associates a
ORDER BY a.id DESC
";

$result = $conn->query($sql);
?>

<div class="container-fluid py-4">
<a href="associate_form.php" class="btn btn-primary btn-sm">
+ Add Associate
</a>
<a href="export_associates.php" class="btn btn-success btn-sm">Export CSV</a>
<div class="card shadow-sm">
<div class="card-header bg-dark text-white d-flex justify-content-between 
align-items-center">
<h5 class="mb-0">Manage Associates</h5>
<input type="text" id="searchInput" class="form-control w-25" placeholder="Search...">
</div>

<div class="card-body table-responsive">

<table class="table table-bordered table-hover align-middle" id="associateTable">
<thead class="table-light">
<tr>
<th>ID</th>
<th>Name</th>
<th>Organization</th>
<th>Email</th>
<th>Phone</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>

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

<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['name'] ?><br/>
<!--
<a href="associate_clients.php?id=<?= $row['id'] ?>" 
class="btn btn-sm btn-outline-secondary ms-2">Clients: 
<span class="badge bg-primary">
<?= $row['total_clients'] ?>
</span>
</a> <br/>-->
<b><a href="associate_clients.php?id=<?= $row['id'] ?>">Clients</a>:</b> 
<span class="badge bg-primary">
<?= $row['total_clients'] ?>
</span> <br/><b>Certificates:</b> <span class="badge bg-info text-dark">
<?= $row['total_certificates'] ?>
</span></td>
<td><?= $row['org_name'] ?></td>
<td><?= $row['email'] ?></td>
<td><?= $row['phone'] ?></td>

<td>
<?php
$status = $row['status'];

if($status == 'active'){
    echo "<span class='badge bg-success'>Active</span>";
}
elseif($status == 'inactive'){
    echo "<span class='badge bg-warning text-dark'>Inactive</span>";
}
elseif($status == 'rejected'){
    echo "<span class='badge bg-danger'>Rejected</span>";
}
else{
    echo "<span class='badge bg-secondary'>Under Review</span>";
}
?>

</td>

<td>

<a href="associate_action.php?id=<?= $row['id'] ?>&action=activate"
class="btn btn-sm btn-success">Activate</a>

<a href="associate_action.php?id=<?= $row['id'] ?>&action=deactivate"
class="btn btn-sm btn-warning">Deactivate</a>

<a href="associate_action.php?id=<?= $row['id'] ?>&action=reject"
class="btn btn-sm btn-danger">Reject</a>
<button class="btn btn-sm btn-secondary"
data-bs-toggle="modal"
data-bs-target="#viewModal<?= $row['id'] ?>">
View
</button>
<a href="associate_form.php?id=<?= $row['id'] ?>" 
class="btn btn-sm btn-info">Edit</a>
<a href="associate_delete.php?id=<?= $row['id'] ?>" 
class="btn btn-sm btn-danger"
onclick="return confirm('Are you sure you want to delete this associate?')">
Delete
</a>
</td>
</tr>
<div class="modal fade" id="viewModal<?= $row['id'] ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-dark text-white">
<h5 class="modal-title">Associate Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>

<div class="modal-body">
<p><strong>Name:</strong> <?= $row['name'] ?></p>
<p><strong>Organization:</strong> <?= $row['org_name'] ?></p>
<p><strong>Email:</strong> <?= $row['email'] ?></p>
<p><strong>Phone:</strong> <?= $row['phone'] ?></p>
<p><strong>GST:</strong> <?= $row['tax_id'] ?></p>
<p><strong>Country:</strong> <?= $row['country'] ?></p>
<p><strong>Status:</strong> <?= ucfirst($row['status']) ?></p>
</div>

</div>
</div>
</div>
<?php endwhile; ?>
</tbody>
</table>
<nav>
<ul class="pagination">
<?php for($i=1;$i<=$pages;$i++): ?>
<li class="page-item <?= ($i==$page)?'active':'' ?>">
<a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>
</div>
</div>
</div>

<script>
// Search filter
document.getElementById("searchInput").addEventListener("keyup", function() {
let filter = this.value.toLowerCase();
let rows = document.querySelectorAll("#associateTable tbody tr");

rows.forEach(row => {
row.style.display = row.innerText.toLowerCase().includes(filter) ? "" : "none";
});
});
</script>

<?php include("partials/footer.php"); ?>