@extends(templates/layout_normal)

@section(main)
    <h2>
        <a href="<?= Container::$routeCollection->getRoute('community')->generateLink(['communitySlug' => $community->getSlug()]) ?>"><?= $community->getName() ?></a> »
        <?php if (isset($event)): ?>
            <a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug()]) ?>">Events</a> »
            <a href="<?= Container::$routeCollection->getRoute('community.event')->generateLink(['communitySlug' => $community->getSlug(), 'eventSlug' => $event->getSlug()]) ?>"><?= $event->getTitle() ?></a> »
        <?php endif; ?>
        Transactions
    </h2>

    <p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.transactions.new')->generateLink(['communitySlug' => $community->getSlug(), 'event' => isset($event) ? $event->getSlug() : null]) ?>">New transaction</a></p>

    <?php if ($numberOfTransactions > 0): ?>
        <?php
        $paginationRouteId = 'community.transactions';
        $paginationRouteParams = ['communitySlug' => $community->getSlug()];
        ?>

        <?php if ($pages > 1): ?>
            @include(templates/pagination)
        <?php endif; ?>

        <?php foreach ($transactions as $transaction): ?>
            <a class="block" href="<?= Container::$routeCollection->getRoute('community.transactions.edit')->generateLink(['communitySlug' => $community->getSlug(), 'transactionId' => $transaction->getId()]) ?>">
            <div class="box transaction">
                <div>
                    <?php if ($transaction->getEvent()): ?>
                        <p><span class="label"><?= $transaction->getEvent()->getTitle() ?></span></p>
                    <?php endif; ?>
                    <p style="font-weight: bold;"><?= $transaction->getDescription() ?></p>
                    <p class="small">
                        <?= $transaction->getPayerUser()->getDisplayName() ?>
                        <i class="fa-solid fa-caret-right"></i>
                        <?php foreach ($members as $member): ?>
                            <?php
                            if (count($transaction->getPayees()) > 0) {
                                $found = false;
                                foreach ($transaction->getPayees() as $payee) {
                                    if ($member->getUserId() === $payee->getUserId()) {
                                        $found = true;
                                    }
                                }
                            } else {
                                $found = true;
                            }
                            ?>
                            <?php if ($found): ?>
                                <?= $member->getUser()->getDisplayName() ?>
                            <?php else: ?>
                                <span class="gray" style="text-decoration: line-through;"><?= $member->getUser()->getDisplayName() ?></span>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </p>
                    <p class="small"><?= $transaction->getTimeDate()->format('Y-m-d H:i') ?></p>
                </div>
                <div style="text-align: right;">
                    <h3><?= number_format($exchangeRateCalculator->calculate($transaction->getSum(), $transaction->getCurrency(), $transaction->getTimeDate()), $community->getMainCurrency()->getRoundDigits()) ?> <?= $community->getMainCurrency()->getCode() ?></h3>
                    <?php if ($community->getMainCurrencyId() !== $transaction->getCurrencyId()): ?>
                        <p style="color: #8e8e8e; font-weight: bold;"><?= number_format($transaction->getSum(), $transaction->getCurrency()->getRoundDigits()) ?> <?= $transaction->getCurrency()->getCode() ?></p>
                    <?php endif; ?>
                </div>
            </div>
            </a>
        <?php endforeach; ?>

        <?php if ($pages > 1): ?>
            @include(templates/pagination)
        <?php endif; ?>

        <p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.transactions.new')->generateLink(['communitySlug' => $community->getSlug(), 'event' => isset($event) ? $event->getSlug() : null]) ?>">New transaction</a></p>
    <?php else: ?>
        <div class="box">
            <p>There are no transactions yet.</p>
        </div>
    <?php endif; ?>
@endsection