<?php
$env = "";
if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'boasearch' ) {
    $env = "dev";
}

if ((!isset($_REQUEST['recordType']) || empty($_REQUEST['recordType'])) &&
    (!isset($_REQUEST['license']) || empty($_REQUEST['license']))    ) {
    echo '';
    exit;
}
$license = htmlspecialchars(stripslashes(trim($_REQUEST['license'])));
$recordType = strtolower(htmlspecialchars(stripslashes(trim($_REQUEST['recordType']))));
$status = strtolower(htmlspecialchars(stripslashes(trim($_REQUEST['licenseStatus']))));

require_once '../lib/Licensees.php';
$ldb = new Licensees($env);


if ($recordType == "firm") {
    $firmData = $ldb->fetchFirmLicensee($license);

    //$firmLicensees = $ldb->fetchFirmIndividuals($license);
    $firmLicensees = $ldb->fetchFirmIndividualsByFirmName($firmData[0]['name']);

    $firm = new stdClass();
    if (isset($firmData) && is_array($firmData)) {
        $firm->data = $firmData[0];
        $firm->licensees = $firmLicensees;
    }

    $licenseeData = json_encode($firm);
} else {
    if (!empty($status)) {
       $licenseeData = generateJsonData($ldb->fetchIndividualLicenseeWithStatus($license, $status));
    } else {
       $licenseeData = generateJsonData($ldb->fetchIndividualLicensee($license));
    }
	
}
echo $licenseeData;


function generateJsonData($licensee) {
    if (isset($licensee) && is_array($licensee)) {
        return json_encode($licensee[0]);
    }

}






/*
foreach ($firmLicensees as $fl) {
    echo json_encode($fl);
//    echo "fff".$recordType."---".$license;
//    echo "<pre>"; print_r($firm);
//    $firm = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($firm));

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
        break;
    case JSON_ERROR_DEPTH:
        echo ' <h1>- Maximum stack depth exceeded</h1>';
        break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' <h1>- Underflow or the modes mismatch</h1>';
        break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' <h1>- Unexpected control character found</h1>';
        break;
    case JSON_ERROR_SYNTAX:
        echo ' <h1>- Syntax error, malformed JSON</h1>';
        break;
    case JSON_ERROR_UTF8:
        print_r($fl);
        echo ' <h1>- Malformed UTF-8 characters, possibly incorrectly encoded</h1>';
        break;
    default:
        echo ' <h1>- Unknown error</h1>';
        break;
}
    }
*/
