<?php

$providerName = $_SESSION['provider']['name'] ?? 'Provider';
$providerPhone = $_SESSION['provider']['phone'] ?? '';

require_once '../config/constants.php';

/* -----------------------------
GET LATEST 5 LEADS FOR NOTIFICATION
----------------------------- */

$notifications = [];

$api = API_URL . "providers/leads.php?page=1&limit=5";

$ch = curl_init();

curl_setopt_array($ch,[
CURLOPT_URL => $api,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-KEY: '.API_KEY,
'Authorization: Bearer '.$_SESSION['provider_token']
]
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response,true);

if(isset($data['data']['data'])){
$notifications = $data['data']['data'];
}

$notificationCount = count($notifications);

?>


<div class="navbar-header">

<div class="row align-items-center justify-content-between">

<div class="col-auto">

<div class="d-flex flex-wrap align-items-center gap-4">

<button type="button" class="sidebar-toggle">
<iconify-icon icon="heroicons:bars-3-solid" class="icon text-2xl non-active"></iconify-icon>
<iconify-icon icon="iconoir:arrow-right" class="icon text-2xl active"></iconify-icon>
</button>

<button type="button" class="sidebar-mobile-toggle">
<iconify-icon icon="heroicons:bars-3-solid" class="icon"></iconify-icon>
</button>

<form class="navbar-search">
<input type="text" name="search" placeholder="Search">
<iconify-icon icon="ion:search-outline" class="icon"></iconify-icon>
</form>

</div>
</div>


<div class="col-auto">

<div class="d-flex flex-wrap align-items-center gap-3">

<button type="button" data-theme-toggle class="w-40-px h-40-px bg-neutral-200 rounded-circle d-flex justify-content-center align-items-center"></button>


<!-- NOTIFICATION DROPDOWN -->

<div class="dropdown">

<button class="has-indicator w-40-px h-40-px bg-neutral-200 rounded-circle d-flex justify-content-center align-items-center"
type="button" data-bs-toggle="dropdown">

<iconify-icon icon="iconoir:bell" class="text-primary-light text-xl"></iconify-icon>

<?php if($notificationCount > 0): ?>
<span class="badge bg-danger"><?php echo $notificationCount; ?></span>
<?php endif; ?>

</button>


<div class="dropdown-menu to-top dropdown-menu-lg p-0">

<div class="m-16 py-12 px-16 radius-8 bg-primary-50 mb-16 d-flex align-items-center justify-content-between">

<h6 class="text-lg text-primary-light fw-semibold mb-0">
New Leads
</h6>

<span class="text-primary-600 fw-semibold text-lg w-40-px h-40-px rounded-circle bg-base d-flex justify-content-center align-items-center">
<?php echo $notificationCount; ?>
</span>

</div>


<div class="max-h-400-px overflow-y-auto scroll-sm pe-4">


<?php if($notifications): ?>

<?php foreach($notifications as $lead): ?>

<a href="leads.php"
class="px-24 py-12 d-flex align-items-start gap-3 mb-2 justify-content-between">

<div class="text-black d-flex align-items-center gap-3">

<span class="w-44-px h-44-px bg-success-subtle text-success-main rounded-circle d-flex justify-content-center align-items-center">

<iconify-icon icon="solar:user-linear"></iconify-icon>

</span>

<div>

<h6 class="text-md fw-semibold mb-2">
<?php echo htmlspecialchars($lead['name']); ?>
</h6>

<p class="mb-0 text-sm text-secondary-light">

<?php echo htmlspecialchars($lead['phone']); ?>
• <?php echo htmlspecialchars($lead['category']); ?>

</p>

</div>

</div>

<span class="text-sm text-secondary-light">

<?php echo date("H:i",strtotime($lead['sent_at'])); ?>

</span>

</a>

<?php endforeach; ?>

<?php else: ?>

<div class="text-center py-20">
No New Leads
</div>

<?php endif; ?>

</div>


<div class="text-center py-12 px-16">
<a href="leads.php" class="text-primary-600 fw-semibold text-md">
View All Leads
</a>
</div>

</div>

</div>



<!-- PROVIDER PROFILE DROPDOWN -->

<div class="dropdown">

<button class="d-flex justify-content-center align-items-center rounded-circle"
type="button" data-bs-toggle="dropdown">

<img src="../admin/assets/images/user.png"
class="w-40-px h-40-px object-fit-cover rounded-circle">

</button>


<div class="dropdown-menu to-top dropdown-menu-sm">


<div class="py-12 px-16 radius-8 bg-primary-50 mb-16 d-flex align-items-center justify-content-between">

<div>

<h6 class="text-lg text-primary-light fw-semibold mb-2">
<?php echo htmlspecialchars($providerName); ?>
</h6>

<span class="text-secondary-light fw-medium text-sm">
Provider
</span>

</div>

</div>


<ul class="to-top-list">

<li>
<a class="dropdown-item text-black px-0 py-8 d-flex align-items-center gap-3"
href="profile.php">

<iconify-icon icon="solar:user-linear"></iconify-icon>

My Profile

</a>
</li>


<li>
<a class="dropdown-item text-black px-0 py-8 d-flex align-items-center gap-3"
href="settings.php">

<iconify-icon icon="icon-park-outline:setting-two"></iconify-icon>

Settings

</a>
</li>


<li>
<a class="dropdown-item text-black px-0 py-8 hover-text-danger d-flex align-items-center gap-3"
href="logout.php">

<iconify-icon icon="lucide:power"></iconify-icon>

Log Out

</a>
</li>

</ul>

</div>

</div>


</div>

</div>

</div>

</div>