<?php
/**
 * Flex @package VirtueMart
 * Template Name - Flex
 * @author Aplikko http://www.aplikko.com
 * @copyright Copyright (c) 2024 Aplikko
*/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die;
$product = $viewData['product'];
$currency = $viewData['currency'];

?>
<div class="product-price mt-0 pt-0" id="productPrice<?php echo $product->virtuemart_product_id ?>">
	<?php

	if ($product->prices['salesPrice']<=0 and VmConfig::get ('askprice', 1) and isset($product->images[0]) and !$product->images[0]->file_is_downloadable) {
		$askquestion_url = JRoute::_('index.php?option=com_virtuemart&view=productdetails&task=askquestion&virtuemart_product_id=' . $product->virtuemart_product_id . '&virtuemart_category_id=' . $product->virtuemart_category_id . '&tmpl=component', FALSE);
		?>
		<a class="ask-a-question bold" href="<?php echo $askquestion_url ?>" rel="nofollow" ><?php echo vmText::_ ('COM_VIRTUEMART_PRODUCT_ASKPRICE') ?></a>
		<?php
	} else {
	
	if (round($product->prices['basePrice']) > round($product->prices['salesPrice'])) {
		echo '<div class="price-without-discount mt-0 mb-2">' . $currency->createPriceDiv ('basePrice', 'COM_VIRTUEMART_PRODUCT_BASEPRICE', $product->prices) . '</div>';
	}
		
	echo $currency->createPriceDiv ('basePriceVariant', 'COM_VIRTUEMART_PRODUCT_BASEPRICE_VARIANT', $product->prices);
		
	echo $currency->createPriceDiv ('variantModification', 'COM_VIRTUEMART_PRODUCT_VARIANT_MOD', $product->prices);
	
	if (round($product->prices['basePriceWithTax'],$currency->_priceConfig['salesPrice'][1]) != round($product->prices['salesPrice'],$currency->_priceConfig['salesPrice'][1])) {
		echo '<div class="price-without-discount"><span class="price-crossed">' . $currency->createPriceDiv ('basePriceWithTax', 'COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX', $product->prices) . "</span></div>";
	}
	if (round($product->prices['salesPriceWithDiscount'],$currency->_priceConfig['salesPrice'][1]) != round($product->prices['salesPrice'],$currency->_priceConfig['salesPrice'][1])) {
		echo $currency->createPriceDiv ('salesPriceWithDiscount', 'COM_VIRTUEMART_PRODUCT_SALESPRICE_WITH_DISCOUNT', $product->prices);
	}

	echo $currency->createPriceDiv ('salesPrice', 'COM_VIRTUEMART_PRODUCT_SALESPRICE', $product->prices);
	echo $currency->createPriceDiv ('salesPriceTt', 'COM_VIRTUEMART_PRODUCT_SALESPRICE_TT', $product->prices);
	
	if ($product->prices['discountedPriceWithoutTax'] != $product->prices['priceWithoutTax']) {
		echo $currency->createPriceDiv ('discountedPriceWithoutTax', 'COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX', $product->prices);
		echo $currency->createPriceDiv ('discountedPriceWithoutTaxTt', 'COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX_TT', $product->prices);
	} else {
		echo $currency->createPriceDiv ('priceWithoutTax', 'COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX', $product->prices);
		echo $currency->createPriceDiv ('priceWithoutTaxTt', 'COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX_TT', $product->prices);
	}

	// Discount "Percent OFF"
	// First calculate percentage:
	$percentDiscount = (1 - $product->prices['discountedPriceWithoutTax'] / $product->prices['basePrice']) * 100;
	
	(VmConfig::get ('discountAmountText', 1) == 1) ? $discountAmountText = vmText::sprintf('COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT') : $discountAmountText = '';
	
	
	if (VmConfig::get ('discountAmount', 1)) {
		if (!empty($product->prices['discountAmount']) && round($product->prices['basePrice']) > round($product->prices['salesPrice'])) {
			echo '<div style="font-size:12px;vertical-align:top;margin:-1px 3px 0 7px;" class="percent-off">'. $discountAmountText . round($percentDiscount, 0) . '%<span class="discount_off">' . JText::_('VM_DISCOUNT_OFF') . '</span></div>';
		}
	}
		
	// New in 2024: Tax + Delivery	
	$infotax = vmConfig::get('vm_prices_info_tax', 0);
	$infodelivery = vmConfig::get('vm_prices_info_delivery', 0);
	if ($infotax == 1 or $infodelivery == 1) { ?>
		<div class="vm-prices-info d-inline-block mt-1<?php if (!empty($product->prices['discountAmount']) && round($product->prices['basePrice']) > round($product->prices['salesPrice'])) { ?> mx-0<?php } else { ?> mx-2<?php } ?>">
		<?php
		if ($infotax == 1) {
			if ($product->prices['priceWithoutTax'] == $product->prices['salesPrice']) {
				$taxclusive = 'COM_VM_TAX_EXCLUSIVE';
			} else {
				$taxclusive = 'COM_VM_TAX_INCLUSIVE';
			}
			echo '<div class="border border-light-subtle py-1 px-2 me-1 rounded-1 d-inline-flex">' .vmText::_($taxclusive) . '</div>';
		}
		if ($infotax == 1 and $infodelivery == 1) {
			echo vmText::_('COM_VM_PRICES_INFO_DIVIDER');
		}
		if ($infodelivery == 1) {
			echo '<div class="border border-light-subtle py-1 px-2 rounded-1 d-inline-flex">' . vmText::_('COM_VM_PRICES_INFO_DELIVERY') . '</div>';
		} ?>
		</div>
		<?php
	}
	
	echo $currency->createPriceDiv ('discountAmountTt', 'COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT_TT', $product->prices);
	echo $currency->createPriceDiv ('taxAmount', 'COM_VIRTUEMART_PRODUCT_TAX_AMOUNT', $product->prices);
	echo $currency->createPriceDiv ('taxAmountTt', 'COM_VIRTUEMART_PRODUCT_TAX_AMOUNT_TT', $product->prices);
	$unitPriceDescription = vmText::sprintf ('COM_VIRTUEMART_PRODUCT_UNITPRICE', vmText::_('COM_VIRTUEMART_UNIT_SYMBOL_'.$product->product_unit));
	echo $currency->createPriceDiv ('unitPrice', $unitPriceDescription, $product->prices);
	}
	?>
</div>

