<?php
include_once(__DIR__."/api-common.php");
include_once(__DIR__."/../my-optician-wsdl-service/MyOpticianWsdlService.php");
global $service_url, $optician_id;

checkGet();
errorIfNotSet('shop_name');
$shop_name = $_GET['shop_name'];

// Instantiate the MyOpticianWsdlService class
$opticianService = new MyOpticianWsdlService($service_url, $optician_id);

$siteId = null;
// if(isset($_GET['site_id']))
//     $siteId = $_GET['site_id'];
// else{
    $siteInfo = $opticianService->getSiteInfo($optician_id, $password);
    $siteId = $siteInfo->getSite();
    $password = $siteInfo->getPassword();
// }
// Call the getPractitioners method
$practitioners = $opticianService->getPractitioners($siteId, $password, $shop_name, $siteInfo);

$singleView = $siteInfo->getSingleOpticianView();
$singlePractitionerName = $siteInfo->getSingleOpticianName();
$final_practitioners = [];

if($singleView != null && ($singleView === true || strtolower($singleView) === 'true')){
    foreach($practitioners AS $practitioner){
        if($practitioner->getName() == $singlePractitionerName){
            $final_practitioners[] = $practitioner;
        }
    }
} else {
    $final_practitioners = $practitioners;
}

// Output the result as JSON
header('Content-Type: application/json');
echo json_encode(['data'=>$final_practitioners]);
?>