<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Date\Date;

$item = $this->item;
$params = $this->params;
$countries = CMGiftCardHelper::getCountries();
$billingInfo = array();
$billingInfo[] = $item->first_name . ' ' . $item->last_name;

if ($item->email != '')
	$billingInfo[] = $item->email;

if ($item->address_1 != '')
	$billingInfo[] = $item->address_1;

if ($params->get('street_address_2') && $item->address_2 != '')
	$billingInfo[] = $item->address_2;

if ($params->get('street_address_3') && $item->address_3 != '')
	$billingInfo[] = $item->address_3;

if ($params->get('postal_box') && $item->postal_box != '')
	$billingInfo[] = $item->postal_box;

if ($item->city != '')
	$billingInfo[] = $item->city;

if ($item->state != '')
	$billingInfo[] = $item->state;

if ($params->get('postal_code') && $item->postal_code != '')
	$billingInfo[] = $item->postal_code;

if ($params->get('country') && $item->country != '' && isset($countries[$item->country]))
	$billingInfo[] = $countries[$item->country];

Factory::getDocument()->addStyleSheet('components/com_cmgiftcard/assets/css/styles.css');
?>
<div class="cmgiftcard<?php echo $this->pageclass_sfx?>">
	<div class="page-header">
		<h1> <?php echo $this->escape($params->get('page_heading')); ?> </h1>
	</div>

	<h3 class="border-bottom pb-2 mb-4"><?php echo Text::_('COM_CMGIFTCARD_ORDER_ORDER_DETAIL'); ?></h3>

	<table class="table table-striped">
		<tbody>
			<tr>
				<td class="table-heading">
					<?php echo Text::_('COM_CMGIFTCARD_ORDER_NUMBER'); ?>
				</td>
				<td><?php echo $item->order_number; ?></td>
			</tr>
			<tr>
				<td class="table-heading">
					<?php echo Text::_('COM_CMGIFTCARD_AMOUNT'); ?>
				</td>
				<td>
					<?php echo HTMLHelper::_('cmgiftcard.formatPrice', $item->amount); ?>
				</td>
			</tr>
			<tr>
				<td class="table-heading">
					<?php echo Text::_('COM_CMGIFTCARD_ORDER_BILLING_INFO'); ?>
				</td>
				<td>
					<?php echo implode('<br>', $billingInfo); ?>
				</td>
			</tr>
			<tr>
				<td class="table-heading">
					<?php echo Text::_('COM_CMGIFTCARD_ORDER_PAYMENT_METHOD'); ?>
				</td>
				<td>
					<?php echo HTMLHelper::_('cmgiftcard.getPaymentMethodName', $item->payment_method); ?>
				</td>
			</tr>
			<tr>
				<td class="table-heading">
					<?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS'); ?>
				</td>
				<td>
					<?php
					if ($item->order_status == 'unpaid') :
						echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_UNPAID');
					elseif ($item->order_status == 'paid') :
						echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_PAID');
					elseif ($item->order_status == 'refunded') :
						echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_REFUNDED');
					endif;
					?>
				</td>
			</tr>
			<tr>
				<td class="table-heading">
					<?php echo Text::_('COM_CMGIFTCARD_ORDER_CREATED_DATE'); ?>
				</td>
				<td>
					<?php
					if ($item->created)
					{
						echo HTMLHelper::_('date', $item->created, Text::_('DATE_FORMAT_LC1') . ' H:i');
					}
					?>
				</td>
			</tr>
		</tbody>
	</table>

	<h3 class="border-bottom pb-2 mb-4"><?php echo Text::_('COM_CMGIFTCARD_ORDER_GIFT_CARDS'); ?></h3>
	<?php if (count($item->cards)) : ?>
		<?php $count = 0; ?>
		<?php foreach ($item->cards as $card) : ?>
			<?php
			$count++;
			$shippingInfo = array();
			$shippingInfo[] = $card->to_name;

			if ($card->to_email != '')
				$shippingInfo[] = $card->to_email;

			if ($card->to_address_1 != '')
				$shippingInfo[] = $card->to_address_1;

			if ($params->get('street_address_2') && $card->to_address_2 != '')
				$shippingInfo[] = $card->to_address_2;

			if ($params->get('street_address_3') && $card->to_address_3 != '')
				$shippingInfo[] = $card->to_address_3;

			if ($params->get('postal_box') && $card->to_postal_box != '')
				$shippingInfo[] = $card->to_postal_box;

			if ($card->to_city != '')
				$shippingInfo[] = $card->to_city;

			if ($card->to_state != '')
				$shippingInfo[] = $card->to_state;

			if ($params->get('postal_code') && $card->to_postal_code != '')
				$shippingInfo[] = $card->to_postal_code;

			if ($params->get('country') && $card->to_country != '' && isset($countries[$card->to_country]))
				$shippingInfo[] = $countries[$card->to_country];
			?>

			<h4><?php echo $count; ?>. <?php echo $card->card_code; ?></h4>
			<table class="table table-striped">
				<tbody>
					<tr>
						<td class="table-heading">
							<?php echo Text::_('COM_CMGIFTCARD_CARD_CODE'); ?>
						</td>
						<td><?php echo $card->card_code; ?></td>
					</tr>
					<tr>
						<td class="table-heading">
							<?php echo Text::_('COM_CMGIFTCARD_ORDER_CARD_TYPE'); ?>
						</td>
						<td>
						<?php
						if ($card->card_type == 'physical')
							echo Text::_('COM_CMGIFTCARD_CARD_TYPE_PHYSICAL');
						elseif ($card->card_type == 'electronic')
							echo Text::_('COM_CMGIFTCARD_CARD_TYPE_ELECTRONIC');
						?>
						</td>
					</tr>
					<tr>
						<td class="table-heading">
							<?php echo Text::_('COM_CMGIFTCARD_AMOUNT'); ?>
						</td>
						<td>
							<?php echo HTMLHelper::_('cmgiftcard.formatPrice', $card->amount); ?>
						</td>
					</tr>
					<tr>
						<td class="table-heading">
							<?php echo Text::_('COM_CMGIFTCARD_ORDER_SENDER_NAME'); ?>
						</td>
						<td><?php echo $card->from_name; ?></td>
					</tr>
					<tr>
						<td class="table-heading">
							<?php echo Text::_('COM_CMGIFTCARD_ORDER_RECIPIENT_INFO'); ?>
						</td>
						<td>
							<?php echo implode('<br>', $shippingInfo); ?>
						</td>
					</tr>
					<tr>
						<td class="table-heading">
							<?php echo Text::_('COM_CMGIFTCARD_ORDER_SCHEDULED_DELIVER_DATE'); ?>
						</td>
						<td>
							<?php
							if ($card->deliver_date)
							{
								$deliverDate = new Date($card->deliver_date);
								echo $deliverDate->format(Text::_('DATE_FORMAT_LC1'));
							}
							?>
						</td>
					</tr>
					<tr>
						<td class="table-heading">
							<?php echo Text::_('COM_CMGIFTCARD_ORDER_DELIVERED'); ?>
						</td>
						<td>
							<?php if ($card->delivered) : ?>
							<?php echo HTMLHelper::_('date', $card->delivered, Text::_('DATE_FORMAT_LC1') . ' H:i'); ?>
							<?php else : ?>
								<?php echo Text::_('COM_CMGIFTCARD_ORDER_NOT_DELIVERED'); ?>
							<?php endif; ?>
						</td>
					</tr>
				</tbody>
			</table>
		<?php endforeach; ?>
	<?php else : ?>
		<div class="alert alert-warning">
			<?php echo Text::_('COM_CMGIFTCARD_ORDER_NO_GIFT_CARDS_FOUND'); ?>
		</div>
	<?php endif; ?>
</div>
