<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

JHtml::fetch('vrehtml.assets.select2');
JHtml::fetch('vrehtml.assets.fontawesome');
JHtml::fetch('vrehtml.scripts.selectflags', '#vre-lang-sel', array('width' => '90%', 'allowClear' => true));

$args = $this->args;

$vik = VREApplication::getInstance();

?>

<div class="ratestest" style="padding: 10px;">

	<form action="index.php" method="post" name="adminForm" id="adminForm">

		<!-- MAIN -->

		<div class="row-fluid">

			<!-- SEARCH -->

			<div class="span6 full-width">
				<?php echo $vik->openEmptyFieldset(); ?>

					<!-- TAX - Select -->

					<?php
					$taxes = JHtml::fetch('vrehtml.admin.taxes', $blank = '');

					echo $vik->openControl(JText::translate('VRETAXFIELDSET') . '*'); ?>
						<select name="id_tax" id="vre-taxes-sel" class="required">
							<?php echo JHtml::fetch('select.options', $taxes, 'value', 'text', $args['id_tax']); ?>
						</select>
					<?php echo $vik->closeControl(); ?>

					<!-- AMOUNT -->

					<?php echo $vik->openControl(JText::translate('VRMANAGETKORDDISC5') . '*'); ?>
						<div class="input-prepend currency-field">
							<button type="button" class="btn"><?php echo VREFactory::getCurrency()->getSymbol(); ?></button>

							<input type="number" name="amount" value="<?php echo (float) $args['amount']; ?>" min="0" step="any" class="required" />
						</div>
					<?php echo $vik->closeControl(); ?>

					<!-- LANGUAGE -->

					<?php
					$options = JHtml::fetch('contentlanguage.existing');
					
					echo $vik->openControl(JText::translate('VRMANAGELANG4')); ?>
						<select name="langtag" id="vre-lang-sel">
							<option></option>
							<?php echo JHtml::fetch('select.options', $options, 'value', 'text', $args['langtag']); ?>
						</select>
					<?php echo $vik->closeControl(); ?>

					<!-- SUBMIT - Button -->

					<?php echo $vik->openControl(''); ?>
						<button type="button" class="btn" id="test-tax-btn">
							<?php echo JText::translate('VRETESTTAXES'); ?>
						</button>
					<?php echo $vik->closeControl(); ?>

				<?php echo $vik->closeEmptyFieldset(); ?>
			</div>

			<!-- RESULT -->

			<div class="span6" id="result-wrap" style="display: none;">
				<?php echo $vik->openEmptyFieldset(); ?>

					<table class="rates-table table" id="tax-table" style="display: none;">
						<thead>
							<tr>
								<th><?php echo JText::translate('JDETAILS'); ?></th>
								<th style="text-align: right;"><?php echo JText::translate('VRMANAGETKORDDISC5'); ?></th>
								<th style="text-align: right;"><?php echo JText::translate('VRMANAGEBILL2'); ?></th>
							</tr>
						</thead>

						<tbody></tbody>

						<tfoot></tfoot>
					</table>

				<?php echo $vik->closeEmptyFieldset(); ?>
			</div>

		</div>

	</form>

</div>

<?php
JText::script('VRSYSTEMCONNECTIONERR');
JText::script('VRINVTOTAL');
JText::script('VRINVTAXES');
JText::script('VRINVGRANDTOTAL');
?>

<script>
	(function($) {
		'use strict';

		let validator;

		$(function() {
			validator = new VikFormValidator('#adminForm');

			$('#vre-taxes-sel').select2({
				placeholder: '--',
				allowClear: false,
				width: '90%',
			});

			$('#test-tax-btn').on('click', function() {
				$('#result-wrap').show();
				$('#tax-table').hide();

				if (!validator.validate()) {
					return false;
				}

				$(this).attr('disabled', true);

				UIAjax.do(
					'<?php echo $vik->ajaxUrl('index.php?option=com_vikrestaurants&task=tax.testajax'); ?>',
					jQuery('#adminForm').serialize(),
					(obj) => {
						// for debug purposes
						console.log(obj);

						// build tax table
						buildTaxTable(obj);

						$(this).attr('disabled', false);
					},
					(err) => {
						alert(Joomla.JText._('VRSYSTEMCONNECTIONERR'));

						$(this).attr('disabled', false);
					}
				);
			});

			// helper function used to build the resulting table
			const buildTaxTable = (data) => {
				var table = $('#tax-table');
				var tbody = table.find('tbody');
				var tfoot = table.find('tfoot');

				// reset body and footer contents
				tbody.html('');
				tfoot.html('');

				// append total net to table body
				tbody.append(createTableRow(Joomla.JText._('VRINVTOTAL'), null, data.net));

				let total = data.net;

				// iterate breakdowns
				data.breakdown.forEach((bd) => {
					// increment net by the given taxes
					total += parseFloat(bd.tax);

					// append breakdown to body
					tbody.append(createTableRow(bd.name, bd.tax, total, {class: 'rate-child'}));
				});

				// append total taxes to footer
				tfoot.append(createTableRow(Joomla.JText._('VRINVTAXES'), null, data.tax, {class: 'final-cost'}));
				// append total gross to footer
				tfoot.append(createTableRow(Joomla.JText._('VRINVGRANDTOTAL'), null, data.gross, {class: 'final-cost'}));

				table.show();
			}

			// helper function used to build a table row
			const createTableRow = (details, cost, total, attrs) => {
				const tr = $('<tr></tr>');

				if (attrs) {
					// set up row attributes
					for (var k in attrs) {
						tr.attr(k, attrs[k]);
					}
				}

				const currency = VRECurrency.getInstance();

				// create details column
				const detailsTD = $('<td></td>');
				detailsTD.html(details);
				detailsTD.css('text-align', 'left');

				// create cost column
				const costTD = $('<td></td>');
				costTD.css('text-align', 'right');
				
				if (cost !== null && cost !== undefined) {
					costTD.html(currency.format(cost));
				}

				// create total column
				const totalTD = $('<td></td>');
				totalTD.css('text-align', 'right');
				totalTD.addClass('rate-price');
				
				if (total !== null && total !== undefined) {
					totalTD.html(currency.format(total));
				}

				// build row
				tr.append(detailsTD);
				tr.append(costTD);
				tr.append(totalTD);

				return tr;
			}
		});
	})(jQuery);
</script>
