<?php

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

$token = $_SESSION['provider_token'];
$provider_id = $_SESSION['provider']['id'];


/* SERVICES API */

$ch = curl_init();

curl_setopt_array($ch,[
CURLOPT_URL => API_URL."providers/services-and-areas.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-KEY: '.API_KEY,
'Authorization: Bearer '.$token
]
]);

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

$data = json_decode($res,true);

$services = $data['data']['services'] ?? [];


/* PROFILE API */

$ch = curl_init();

curl_setopt_array($ch,[
CURLOPT_URL => API_URL."providers/full-profile.php?provider_id=".$provider_id,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-KEY: '.API_KEY,
'Authorization: Bearer '.$token
]
]);

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

$profile = json_decode($res,true);

$subscription = $profile['data']['subscription'] ?? [];

$serviceLimit = $subscription['categories_allowed'] ?? 1;

$currentServices = count($services);

?>

<?php include '../admin/partials/layouts/layoutTop.php' ?>

<div class="dashboard-main-body">

<h6 class="fw-semibold mb-24">My Services</h6>


<div class="card p-24 mb-24">

<div class="d-flex justify-content-between mb-16">

<div>
<strong>Plan:</strong>
<?php echo htmlspecialchars($subscription['plan_name'] ?? 'Trial Plan'); ?>
</div>

<div>
<strong>Services Used:</strong>
<?php echo $currentServices ?> / <?php echo $serviceLimit ?>
</div>

</div>


<div class="row">

<div class="col-md-5">
<select id="category" class="form-select">
<option value="">Select Category</option>
</select>
</div>

<div class="col-md-5">
<select id="subcategory" class="form-select">
<option value="">Select Service</option>
</select>
</div>

<div class="col-md-2">

<button id="addServiceBtn"
class="btn btn-primary w-100"
<?php if($currentServices >= $serviceLimit) echo "disabled"; ?>>

Add

</button>

</div>

</div>


<?php if($currentServices >= $serviceLimit): ?>

<div class="alert alert-warning mt-3">

You have reached your plan limit.
<a href="subscription.php">Upgrade your plan</a>
to add more services.

</div>

<?php endif ?>

</div>



<div class="card p-24">

<table class="table bordered-table">

<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Service</th>
<th>Action</th>
</tr>
</thead>

<tbody>

<?php if($services): ?>

<?php foreach($services as $i=>$s): ?>

<tr>

<td><?php echo $i+1 ?></td>

<td><?php echo htmlspecialchars($s['category']) ?></td>

<td><?php echo htmlspecialchars($s['subcategory']) ?></td>

<td>

<button class="btn btn-danger removeService"
data-id="<?php echo $s['id'] ?>">

Remove

</button>

</td>

</tr>

<?php endforeach ?>

<?php else: ?>

<tr>
<td colspan="4" class="text-center">No services added</td>
</tr>

<?php endif ?>

</tbody>

</table>

</div>

</div>


<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script>

const headers={
'X-API-KEY':'<?php echo API_KEY ?>',
'Authorization':'Bearer <?php echo $_SESSION["provider_token"] ?>'
};


/* CATEGORY LOAD */

$.ajax({

url:"<?php echo BASE_URL ?>api/providers/categories.php",
headers:headers,
dataType:"json",

success:function(res){

res.data.forEach(function(cat){

$("#category").append(
`<option value="${cat.id}">${cat.name}</option>`
);

});

}

});


/* SUBCATEGORY */

$("#category").change(function(){

let id=$(this).val();

$("#subcategory").html('<option value="">Select Service</option>');

if(!id) return;

$.ajax({

url:"<?php echo BASE_URL ?>api/providers/subcategories.php",
headers:headers,
data:{category_id:id},
dataType:"json",

success:function(res){

res.data.forEach(function(sub){

$("#subcategory").append(
`<option value="${sub.id}">${sub.name}</option>`
);

});

}

});

});


/* ADD SERVICE */

$("#addServiceBtn").click(function(){

let sub=$("#subcategory").val();

if(!sub){
alert("Please select service");
return;
}

$.ajax({

url:"<?php echo BASE_URL ?>api/providers/add-service.php",
method:"POST",
headers:headers,
data:{sub_category_id:sub},
dataType:"json",

success:function(res){

if(res.status=="success"){
location.reload();
}else{
alert(res.message);
}

}

});

});


/* REMOVE SERVICE */

$(".removeService").click(function(){

if(!confirm("Remove service?")) return;

let id=$(this).data("id");

$.ajax({

url:"<?php echo BASE_URL ?>api/providers/remove-service.php",
method:"POST",
headers:headers,
data:{id:id},

success:function(){
location.reload();
}

});

});

</script>

<?php include '../admin/partials/layouts/layoutBottom.php' ?>