<?php
/**
 * @var $auctionCommission int
 * @var $displayVatField string
 * @var $lotHelper LotHelper
 */

use auctionforge\inc\helpers\LotHelper;

?>
<script type="text/template" id="modal-presale-template">
    <header class="modal__header">
        <h2 class="modal__title modal__title_without-logo" id="modal-pre-sale-title">
            <?= __('Confirm purchase', 'auctionforge') ?>:
        </h2>
    </header>
    <main class="modal__content bp-mb-30">
        <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
        <p class="modal__content__item bp-d-flex"><strong><?= __('Lot number', 'auctionforge') ?>:</strong>&nbsp;<span><%- index %></span></p>
        <p class="modal__content__item bp-d-flex"><strong><?= __('Lot name', 'auctionforge') ?>:</strong>&nbsp;
            <span><%= name %></span></p>
        <% if (buyoutAction) { %>
            <p class="modal__content__item bp-d-flex">
                <strong><?= __('Buyout price', 'auctionforge') ?>:</strong>&nbsp;
                <span class="bp-text-red"><%- formattedPrice(price, currentCurrency) %></span>
            </p>
        <% } else { %>
            <p class="modal__content__item bp-d-flex">
                <strong><?= __('Price', 'auctionforge') ?>:</strong>&nbsp;
                <span class="bp-text-red"><%- formattedPrice(price, currentCurrency) %></span>
            </p>
        <% } %>
        <% if (auctionCommission !== undefined) { %>
            <% if (!(auctionCommission === 0 || auctionCommission === '0' || auctionCommission === '0%')) { %>
            <p class="modal__content__item bp-d-flex">
                <strong><?= __("Buyer's Premium", 'auctionforge') ?>:</strong>
                <span><%- auctionCommission %>%</span>
            </p>
            <% } %>
        <% } %>
        <%
        var _taxPercent = (typeof settings !== 'undefined' && settings && settings.vat) ? parseFloat(settings.vat) : 0;
        var _houseState = (typeof window.bs_data !== 'undefined' && window.bs_data.auction_house_state) ? window.bs_data.auction_house_state : '';
        var _userState = '';
        if (typeof user !== 'undefined' && user && user.address && user.address.countryCode === 'US') {
            _userState = user.address.stateCode || user.address.state || '';
        }
        var _sameState = (_houseState !== '' && _userState !== '' && _houseState.toUpperCase() === _userState.toUpperCase());
        var _useSalesTax = (_houseState !== '');
        if (_taxPercent > 0) {
            if (_useSalesTax) {
                if (_sameState) { %>
        <p class="vat-field modal__content__item bp-d-flex">
            <strong>Sales Tax:</strong>
            <span><%- _taxPercent %>% on item price &amp; buyer's premium</span>
        </p>
        <%      } else { %>
        <p class="vat-field modal__content__item bp-d-flex">
            <strong>Sales Tax:</strong>
            <span>0%</span>
        </p>
        <%      }
            } else {
        %>
        <?php if ($displayVatField == 'on') { ?>
            <% if (auctionHouseData.houseCommissionVatRule === 'SHOULD_ADD_VAT') { %>
                <p class="vat-field modal__content__item bp-d-flex">
                    <strong><?= __('VAT', 'auctionforge') ?>:</strong>
                    <% if (fullPriceVat === true) { %>
                    <span><?= __('On the full lot\'s price and commission', 'auctionforge') ?></span>
                    <% } else { %>
                    <span><?= __('On commission only', 'auctionforge') ?></span>
                    <% } %>
                </p>
            <% } else if (auctionHouseData.houseCommissionVatRule === 'EXEMPTED_FROM_VAT') { %>
                <% if (fullPriceVat === true) { %>
                <p class="vat-field modal__content__item bp-d-flex">
                    <strong><?= __('VAT', 'auctionforge') ?>:</strong>
                    <span><?= __('On lot\'s price, no vat on commission', 'auctionforge') ?></span>
                </p>
                <% } %>
            <% } %>
        <?php } ?>
        <%
            }
        } else {
            // No tax configured — show VAT section if applicable
        %>
        <?php if ($displayVatField == 'on') { ?>
            <% if (auctionHouseData.houseCommissionVatRule === 'SHOULD_ADD_VAT') { %>
                <p class="vat-field modal__content__item bp-d-flex">
                    <strong><?= __('VAT', 'auctionforge') ?>:</strong>
                    <% if (fullPriceVat === true) { %>
                    <span><?= __('On the full lot\'s price and commission', 'auctionforge') ?></span>
                    <% } else { %>
                    <span><?= __('On commission only', 'auctionforge') ?></span>
                    <% } %>
                </p>
            <% } else if (auctionHouseData.houseCommissionVatRule === 'EXEMPTED_FROM_VAT') { %>
                <% if (fullPriceVat === true) { %>
                <p class="vat-field modal__content__item bp-d-flex">
                    <strong><?= __('VAT', 'auctionforge') ?>:</strong>
                    <span><?= __('On lot\'s price, no vat on commission', 'auctionforge') ?></span>
                </p>
                <% } %>
            <% } %>
        <?php } ?>
        <% } %>
        <?php if (0) { //Disabled ?>
        <p class="modal__content__item bp-d-flex">
            <strong><?= __('Price including commission', 'auctionforge') ?>:</strong>
            <span class="bp-text-strong"><%- formattedPrice(commissionPrice, currentCurrency) %></span>
        </p>
        <?php } ?>

        <% if (isShop !== undefined && isShop) { %>
            <br>
            <p><?= __('You will receive an email with your invoice, including shipping, and a secure payment link.', 'auctionforge') ?></p>
        <% } else { %>
            <% if (auctionHouseData.noVatOnCommission !== true || fullPriceVat === true) { %>
            <p class="users-from-foreign-countries bp-mb-0" data-block="0.2">
                <?= __("Users from foreign countries may be exempted from tax payments, according to the relevant tax regulations", "auctionforge") ?>
            </p>
            <% } %>
            <br>
            <p>
                <?= sprintf(__('By clicking the "I agree" button you signify your agreement to the terms and conditions of auction house %s', 'auctionforge'), get_option('blogname')) ?>
            </p>
        <% } %>
    </main>
    <footer class="modal__footer bp-d-flex">
        <% if (isShop === undefined || !isShop) { %>
        <div class="bp-d-flex bp-f-start bp-mr-auto bp-md-f-center">
            <a target="_blank" class="bp-btn bp-btn-default bp-btn-link" href="<%- linkTerms %>"><?= __('Terms of sale', 'auctionforge') ?></a>
        </div>
        <% } else { %>
        <div class="bp-d-flex bp-f-start bp-mr-auto bp-md-f-center"></div>
        <% } %>
        <div class="bp-d-flex bp-f-end bp-md-f-center">
            <% if (isShop !== undefined && isShop) { %>
                <a id="dontAgreeSaleBid" class="bp-btn bp-btn-border bp-btn-link bp-mr-10" href="#"><?= __('Cancel', 'auctionforge') ?></a>
                <a id="agreeSaleBid" class="bp-btn bp-btn-primal" href="#"><?= __('Buy', 'auctionforge') ?></a>
            <% } else { %>
                <a id="dontAgreeSaleBid" class="bp-btn bp-btn-border bp-btn-link bp-mr-10" href="#"><?= __('I don\'t Agree', 'auctionforge') ?></a>
                <a id="agreeSaleBid" class="bp-btn bp-btn-primal" href="#"><?= __('I Agree', 'auctionforge') ?></a>
            <% } %>
        </div>
    </footer>
</script>
