<?php
if(empty($errorMsg) && isset($_REQUEST['originId']) && !empty($_REQUEST['originId']))
{
    $originId = intval($_REQUEST['originId']);
    
    if(isset($_REQUEST['country_id']) && !empty($_REQUEST['country_id']))
    {
        $country_id = intval($_REQUEST['country_id']);
        if ($result = $mysqli->query("SELECT Destination.Id, Destination.Name, CASE WHEN Haulage.DestinationId IS NULL THEN 0 ELSE 1 END AS Haulage
        FROM Destination
        INNER JOIN OriginDestination ON Destination.Id = OriginDestination.DestinationId
        INNER JOIN Location ON Destination.LocationCode = Location.Code
        INNER JOIN Country ON Country.Code = Location.CountryCode
        INNER JOIN MarketCountry ON Country.Id = MarketCountry.CountryId
        INNER JOIN Market ON Market.Id = MarketCountry.MarketId
        LEFT OUTER JOIN (SELECT Destination.Id AS DestinationId
        FROM HaulageLocation
        INNER JOIN HaulageZone ON HaulageZone.Id = HaulageLocation.HaulageZoneId
        INNER JOIN Country ON Country.Id = HaulageZone.DestinationCountryId AND HaulageZone.DestinationCountryId = '" . $country_id . "'
        INNER JOIN Location ON Country.Code = Location.CountryCode
        INNER JOIN Destination ON Destination.LocationCode = Location.Code
        WHERE Destination.WebsiteId = '" . $websiteId . "' AND HaulageLocation.Live = 1 AND Destination.Live = 1
        GROUP BY Destination.Id)
        AS Haulage ON Haulage.DestinationId = Destination.Id
        WHERE Destination.WebsiteId = '" . $websiteId . "' AND Destination.Live = 1 AND OriginDestination.Live = 1
        AND OriginDestination.OriginId = '" . $originId . "' AND MarketCountry.CountryId = '" . $country_id . "'
        AND Market.Live = 1 AND MarketCountry.Live = 1 AND MarketCountry.Import = 1 AND Market.WebsiteId = '" . $websiteId . "'
        GROUP BY Destination.Id, Destination.Name
        ORDER BY CASE WHEN Destination.Name = 'UK Port' THEN 0 ELSE 1 END, Destination.Name;"))
        {
            if($result->num_rows == 0)
            {
                $errorMsg[] = 'No destinations have been found';
            }
            else
            {
                $xmlContent = $xml->addChild('Destinations');
                while ($row = $result->fetch_object())
                {
                    $xmlDestination = $xmlContent->addChild('Destination');
                    $xmlDestination->addChild('Id', intval($row->Id));
                    $xmlDestination->addChild('Name', encodeString($row->Name));
                    $xmlDestination->addChild('Haulage', intval($row->Haulage));
                }
                $result->close();
            }
        }
        else
        {
            print $mysqli->error;
            exit;
        }
    }
    else
    {
        if ($result = $mysqli->query("SELECT Destination.Id, Destination.Name, CASE WHEN Haulage.DestinationId IS NULL THEN 0 ELSE 1 END AS Haulage
        FROM Destination
        INNER JOIN OriginDestination ON Destination.Id = OriginDestination.DestinationId
        INNER JOIN Location ON Destination.LocationCode = Location.Code
        INNER JOIN Country ON Country.Code = Location.CountryCode
        INNER JOIN MarketCountry ON Country.Id = MarketCountry.CountryId
        INNER JOIN Market ON Market.Id = MarketCountry.MarketId
        LEFT OUTER JOIN (SELECT Destination.Id AS DestinationId
        FROM HaulageLocation
        INNER JOIN HaulageZone ON HaulageZone.Id = HaulageLocation.HaulageZoneId
        INNER JOIN Country ON Country.Id = HaulageZone.DestinationCountryId
        INNER JOIN Location ON Country.Code = Location.CountryCode
        INNER JOIN Destination ON Destination.LocationCode = Location.Code
        WHERE Destination.WebsiteId = '" . $websiteId . "' AND HaulageLocation.Live = 1 AND Destination.Live = 1
        GROUP BY Destination.Id)
        AS Haulage ON Haulage.DestinationId = Destination.Id
        WHERE Destination.WebsiteId = '" . $websiteId . "' AND Destination.Live = 1 AND OriginDestination.Live = 1
        AND OriginDestination.OriginId = '" . $originId . "'
        AND Market.Live = 1 AND MarketCountry.Live = 1 AND MarketCountry.Import = 1 AND Market.WebsiteId = '" . $websiteId . "'
        GROUP BY Destination.Id, Destination.Name
        ORDER BY CASE WHEN Destination.Name = 'UK Port' THEN 0 ELSE 1 END, Destination.Name;"))
        {
            if($result->num_rows == 0)
            {
                $errorMsg[] = 'No destinations have been found';
            }
            else
            {
                $xmlContent = $xml->addChild('Destinations');
                while ($row = $result->fetch_object())
                {
                    $xmlDestination = $xmlContent->addChild('Destination');
                    $xmlDestination->addChild('Id', intval($row->Id));
                    $xmlDestination->addChild('Name', encodeString($row->Name));
                    $xmlDestination->addChild('Haulage', intval($row->Haulage));
                }
                $result->close();
            }
        }
        else
        {
            print $mysqli->error;
            exit;
        }
    }
}
?>