<?php
/**
 * @package     JGive
 * @subpackage  com_jgive
 *
 * @author      Techjoomla <extensions@techjoomla.com>
 * @copyright   Copyright (C) 2009 - 2020 Techjoomla. All rights reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access
defined('_JEXEC') or die;

use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('bootstrap.tooltip');
HTMLHelper::_('formbehavior.chosen', 'select');
$itemId              = (isset($this->itemId))?$this->itemId:0;
$jgiveFrontendHelper = new jgiveFrontendHelper;

// Jomsocial toolbar
if (isset($this->jomsocialToolbarHtml))
{
	echo $this->jomsocialToolbarHtml;
}
?>
<script>
	var tjListFilters = [];

	jQuery(document).ready(function(){
		jQuery("#limit").attr('onchange', 'jgiveCommon.filters.submitFilters(\'adminForm\')');
		jQuery("#payment_status").attr('onchange', 'jgiveCommon.filters.submitFilters(\'adminForm\')');

		<?php
		foreach ($this->availableFilters as $availableFilter)
		{
			?>
			tjListFilters.push('<?php echo $availableFilter; ?>');
			<?php
		}
		?>
	});

	var jgive_baseurl = "<?php echo Uri::root(); ?>";
</script>
<div id="jgive_my_donations" class="row">
	<div class="col-xs-12">
		<div class="page-header">
			<h1><?php echo Text::_('COM_JGIVE_MY_DONATIONS');?></h1>
		</div>
		<form action="" name="adminForm" id="adminForm" class="form-validate" method="POST">
			<!-- show pagination limit box and filters -->
			<div class="btn-toolbar pull-right">
				<div class="btn-group clearfix">
					<?php echo HTMLHelper::_('select.genericlist', $this->sstatus, "payment_status", 'size="1"
						onchange="" name="payment_status"', "value", "text", $this->lists['payment_status']);?>
						&nbsp;
				</div>
				<div class="btn-group pull-right">
					<label for="limit" class="element-invisible">
						<?php echo Text::_('JFIELD_PLG_SEARCH_SEARCHLIMIT_DESC'); ?>
					</label>
					<?php echo $this->pagination->getLimitBox(); ?>
				</div>
			</div>
			<div class="clearfix">&nbsp;</div>
			<!-- show message if no items found -->
			<?php
			if (empty($this->myDonations))
			{
			?>
				<div class="alert alert-warning">
					<?php echo Text::_('COM_JGIVE_NO_DATA_FOUND');?>
				</div>
			<?php
			}
			else
			{
			?>
				<div class="no-more-tables">
					<table class="table table-striped table-bordered table-hover">
						<thead>
							<tr>
								<th class="center nowrap">
									<?php echo Text::_('COM_JGIVE_NO');?>
								</th>

								<th class="center nowrap">
									<a
									href="<?php echo $jgiveFrontendHelper->getTableSortUrl($this->currentUrl, 'id', $this->sortDirection);?>"
									class="hasPopover"
									data-content="<?php echo Text::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN');?>"
									data-placement="top"
									data-original-title="<?php echo Text::_('COM_JGIVE_DONATION_ID');?>"
									>
									<?php echo Text::_('COM_JGIVE_DONATION_ID');?>
									</a>
								</th>

								<th class="center nowrap">
									<a
									href="<?php echo $jgiveFrontendHelper->getTableSortUrl($this->currentUrl, 'processor', $this->sortDirection);?>"
									class="hasPopover"
									data-content="<?php echo Text::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN');?>"
									data-placement="top"
									data-original-title="<?php echo Text::_('COM_JGIVE_GATEWAY');?>"
									>
									<?php echo Text::_('COM_JGIVE_GATEWAY');?>
									</a>
								</th>

								<th class="center nowrap">
									<a
									href="<?php echo $jgiveFrontendHelper->getTableSortUrl($this->currentUrl, 'status', $this->sortDirection);?>"
									class="hasPopover"
									data-content="<?php echo Text::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN');?>"
									data-placement="top"
									data-original-title="<?php echo Text::_('COM_JGIVE_DONATION_STATUS');?>"
									>
									<?php echo Text::_('COM_JGIVE_DONATION_STATUS');?>
									</a>
								</th>

								<th class="center nowrap">
									<a
									href="<?php echo $jgiveFrontendHelper->getTableSortUrl($this->currentUrl, 'cdate', $this->sortDirection);?>"
									class="hasPopover"
									data-content="<?php echo Text::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN');?>"
									data-placement="top"
									data-original-title="<?php echo Text::_('COM_JGIVE_DONATION_DATE');?>"
									>
									<?php echo Text::_('COM_JGIVE_DONATION_DATE');?>
									</a>
								</th>
								<th class="center nowrap">
									<a
									href="<?php echo $jgiveFrontendHelper->getTableSortUrl($this->currentUrl, 'payment_received_date', $this->sortDirection);?>"
									class="hasPopover"
									data-content="<?php echo Text::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN');?>"
									data-placement="top"
									data-original-title="<?php echo Text::_('COM_JGIVE_PAYMENT_RECEIVED_DATE');?>"
									>
									<?php echo Text::_('COM_JGIVE_PAYMENT_RECEIVED_DATE');?>
									</a>
								</th>

								<th class="center nowrap">
									<a
									href="<?php echo $jgiveFrontendHelper->getTableSortUrl($this->currentUrl, 'original_amount', $this->sortDirection);?>"
									class="hasPopover"
									data-content="<?php echo Text::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN');?>"
									data-placement="top"
									data-original-title="<?php echo Text::_('COM_JGIVE_AMOUNT');?>"
									>
									<?php echo Text::_('COM_JGIVE_AMOUNT');?>
									</a>
								</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$id = 1;
							$totalPaid = 0;

							foreach ($this->myDonations as $donation)
							{
								$totalPaid = $totalPaid + $donation->original_amount;
							?>
								<tr>
									<td class="center nowrap" data-title="<?php echo Text::_("COM_JGIVE_NO"); ?>">
										<?php echo $id++;?>
									</td>
									<td class="center nowrap small" data-title="<?php echo Text::_("COM_JGIVE_DONATION_ID"); ?>">
										<a href="<?php echo Route::_('index.php?option=com_jgive&view=donation&layout=default&donationid=' . $donation->id . '&Itemid=' . $itemId);?>">
											<?php echo htmlspecialchars($donation->order_id?$donation->order_id:$donation->id, ENT_COMPAT, 'UTF-8');?>
										</a>
									</td>
									<td class="center nowrap small" data-title="<?php echo Text::_("COM_JGIVE_GATEWAY"); ?>">
										<?php
										$plugin = PluginHelper::getPlugin('payment', $donation->processor);
										$plgname = (json_decode($plugin->params)->plugin_name) ? (json_decode($plugin->params)->plugin_name) : $donation->processor;
										echo htmlspecialchars($plgname, ENT_COMPAT, 'UTF-8');
										?>
									</td>
									<td class="center nowrap small" data-title="<?php echo Text::_("COM_JGIVE_DONATION_STATUS"); ?>">
										<?php
										$statusArray = JGive::utilities()->getOrderStatusText($donation->status);
										?>
										<span class="small center badge badge-<?php echo $statusArray['statusClass'];?>">
											<?php echo htmlspecialchars($statusArray['statusText'], ENT_COMPAT, 'UTF-8');?>
										</span>
										<?php
										if ($donation->status == 'P' || $donation->status == 'E')
										{
										?>
											<a
												class="btn btn-primary btn-xs"
												href="<?php echo Route::_('index.php?option=com_jgive&view=donation&donationid=' .
												$donation->id . '&Itemid=' . $itemId);?>">
												<small>
													<?php echo (($donation->ctype == 'donation') ? Text::_('COM_JGIVE_RETRY_DONATION') : Text::_('COM_JGIVE_RETRY_INVESTMENT'));?>
												</small>
											</a>
										<?php
										}?>
									</td>
									<td class="center nowrap small" data-title="<?php echo Text::_("COM_JGIVE_DONATION_DATE"); ?>">
										<?php echo HTMLHelper::_('date', $donation->cdate, $this->params->get('date_format', 'j  M  Y')); ?>
									</td>
									<td class="center nowrap small" data-title="<?php echo Text::_("COM_JGIVE_DONATION_DATE"); ?>">
										<?php

										if ($donation->payment_received_date == 0)
										{
											echo "-";
										}
										else
										{
											echo HTMLHelper::_('date', $donation->payment_received_date, $this->params->get('date_format', 'j  M  Y'));
										}?>
									</td>
									<td class="center nowrap small" data-title="<?php echo Text::_("COM_JGIVE_AMOUNT"); ?>">
										<?php echo $this->jgiveFrontendHelper->getFormattedPrice($donation->original_amount);
										?>
									</td>
								</tr>
							<?php
							}
							?>
						</tbody>
					</table>
				</div>
				<div class="col-xs-12">
					<span class="pull-right">
						<strong><?php echo Text::_('COM_JGIVE_TOTAL_DONATION');?></strong>
						<?php echo $this->jgiveFrontendHelper->getFormattedPrice($totalPaid);?>
					</span>
				</div>
				<hr class="hr hr-condensed"/>
			<?php
			}
			?>
			<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
				<?php echo $this->pagination->getListFooter(); ?>
			</div>

			<input type="hidden" name="option" value="com_jgive" />
			<input type="hidden" name="view" value="donations" />
			<input type="hidden" name="layout" value="default" />
			<input type="hidden" name="task" id="task" value="" />
			<?php echo HTMLHelper::_('form.token'); ?>
		</form>
	</div>
</div>
