<?php
/** @var Oney\ThreeByFour\Block\Checkout\Cart $block */
$simulations = $block->getSimulations();
$cc = $this->addCountryCodeTranslation();
$merchantGuid = $this->getMerchantGuid();
$simulationAmont = 0;
$businessTransactionCodes = [];

foreach ($simulations as $key => $simulation) {
    $simulationAmont = $simulation['payment_amount'];
    $businessTransactionCodes[] = $simulation['business_transaction_details']['business_transaction_code'];
}

if (!empty($simulations)) :
?>
    <div class="oney-threebyfour-cart">
        <div id="threebyfourOneyCartSimulationContainer"></div>
        <div class="oney-threebyfour-cart-container">
            <div class="oney-threebyfour-logo-container">
                <img src="<?= $block->getViewFileUrl('Oney_ThreeByFour::images/oney-threebyfour-logo.png') ?>" alt="3x4x Oney Logo"/>
            </div>
            <div class="oney-threebyfour-cart-content">
                <?php if ($block->isFree()) : ?>
                    <p><?= __('Or pay in %1 free' . $cc, $block->renderInstalments()) ?></p>
                <?php else: ?>
                    <p><?= __('Or pay in %1' . $cc, $block->renderInstalments()) ?></p>
                <?php endif; ?>
                <p class="bold"><?= __('by credit card' . $cc) ?></p>
            </div>
            <div class="oney-threebyfour-cart-button simulate-button open-simulation" onClick="showOneyWidget();">
                <?= __('Simulate' . $cc) ?>
            </div>
        </div>
    </div>

    <script>
        function showOneyWidget() {
            let options = {
                country: "<?= $block->escapeHtml($block->getCountryCode() ?: 'ES') ?>",
                language: "<?= $block->escapeHtml($block->getLanguageCode()) ?>",
                merchant_guid: "<?= $merchantGuid ?>",
                payment_amount: <?= number_format($simulationAmont, 2, '.', '') ?>,
                filter_by: "business_transaction_codes",
                business_transaction_codes: <?= json_encode($businessTransactionCodes) ?>,
                short_simulation_placeholder: "#threebyfourOneyCartSimulationContainer"
            };

            loadOneyWidget(function () {
                oneyMerchantApp.loadSimulationPopin({ options });
            });
        }
    </script>

<?php endif; ?>
