<?php if ($_GET['layout'] == 'html'): ?>
    <?php $css = $_GET['css'] ?: 'common'; ?>
    <link rel="stylesheet" href="http://52.53.176.164/SC/<?= $css ?>.css" />

    <body>
        <div class="main">
            <h1 class="wait_title">Who's Next?</h1>
            <div id="wait_list">
                <?php if ($tickets): ?>
                    <?php foreach ($XMLReadyTickets as $aTicket): ?>
                        <p class="customer_list">
                            <span class="position"><?= $aTicket['position']; ?>.</span>
                            <span class="customer_name"><?= sprintf('%s %s', $aTicket['firstName'], $aTicket['lastInitial']) ?></span>
                        </p>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </body>
    <?php exit; ?>
<?php endif; ?>
<?php
if (isset($tickets)) {
    $waitListString = <<<XML
<?xml version="1.0"?>
<waitList>
</waitList>
XML;
} else if ($discounts) {
    $waitListString = <<<XML
<?xml version="1.0"?>
<discountList>
</discountList>
XML;
} else if ($services) {
    $waitListString = <<<XML
<?xml version="1.0"?>
<serviceList>
</serviceList>
XML;
}

$itemList = new SimpleXMLElement($waitListString);

//ticket #13656 - always send 10 nodes (default)
$maxNodes = 10;

$nodeCount = 0;
$itemList->addAttribute('count', $maxNodes);

if ($tickets) {
    foreach ($XMLReadyTickets as $aTicket) {
        $nodeCount++;
        if ($nodeCount > $maxNodes) {
            //do not send any more entries, we reached the max
            break;
        }

        if ($aTicket->customer->firstname) {
            $firstname = $aTicket->customer->firstname;
        } else {
            $nameParts = explode(" ", $aTicket->name);
            $firstname = $nameParts[0];
        }

        if ($aTicket->customer->lastname) {
            $lastname = $aTicket->customer->lastname;
        } else {
            $lastname = $aTicket->name;
        }

        $newNode = $itemList->addChild('customer');

        $newNode->addAttribute('position', $aTicket['position']);
        $newNode->addChild('firstName', $aTicket['firstName']);
        $newNode->addChild('lastInitial', $aTicket['lastInitial']);
        $newNode->addChild('requestedStylist', $aTicket['requestedStylist']);
        $newNode->addChild('checkInMethod', $aTicket['checkInMethod']);
        $newNode->addChild('inSalon', $aTicket['inSalon']);
    }

    //if we have less tickets that the max, send empty xml nodes instead
    while ($nodeCount < $maxNodes) {
        $nodeCount++;
        $newNode = $itemList->addChild('customer');
        $newNode->addAttribute('position', $nodeCount);
        $newNode->addChild('firstName', '');
        $newNode->addChild('lastInitial', '');
    }
} else if ($discounts) {
    //ticket #15943
    if (0 < count($discounts)) {
        foreach ($discounts as $aDiscount) {
            $nodeCount++;
            if ($nodeCount > $maxNodes) {
                //do not send any more entries, we reached the max
                break;
            }

            $newNode = $itemList->addChild('discount');
            $newNode->addAttribute('position', $nodeCount);
            $newNode->addChild('description', htmlentities($aDiscount->description));
        }
    }

    //if we have less tickets that the max, send empty xml nodes instead
    while ($nodeCount < $maxNodes) {
        $nodeCount++;
        $newNode = $itemList->addChild('discount');
        $newNode->addAttribute('position', $nodeCount);
        $newNode->addChild('description', '');
    }
} else if ($services) {
    //ticket #15943
    if (0 < count($services)) {
        foreach ($services as $aService) {
            $nodeCount++;
            if ($nodeCount > $maxNodes) {
                //do not send any more entries, we reached the max
                break;
            }

            //#16008
            $price = $aService->price == floor($aService->price) ? floor($aService->price) : m($aService->price);

            $newNode = $itemList->addChild('service');
            $newNode->addAttribute('position', $nodeCount);
            $newNode->addChild('description', htmlentities($aService->description));
            $newNode->addChild('price', $price);
        }
    }

    //if we have less tickets that the max, send empty xml nodes instead
    while ($nodeCount < $maxNodes) {
        $nodeCount++;
        $newNode = $itemList->addChild('service');
        $newNode->addAttribute('position', $nodeCount);
        $newNode->addChild('description', '');
        $newNode->addChild('price', '');
    }
}

header('Content-type: text/xml');
echo $itemList->asXML();
