<?php
/**
 * That is template for an invoice in member/payment-history.phtml\
 * @var Invoice $invoice
 */
?>
<div class="am-active-invoice" id="invoice-<?php p($invoice->public_id) ?>">
    <div class="am-active-invoice-header">
        <span class="am-active-invoice-date"><?php p(amDate($invoice->tm_started)) ?></span>
        <span class="am-active-invoice-num">#<?php p($invoice->public_id) ?></span>
        <span class="am-active-invoice-paysys"><?php __e($invoice->_paysysName) ?></span>
        <span class="am-active-invoice-terms"><?= $invoice->getTerms() ?></span>
        <?php if ($invoice->status>1): ?>
            <div class="am-active-invoice-rebill-info">
                <?php __e('Next Rebill') ?>:
                <span class="am-active-invoice-rebill-date <?php if($invoice->rebill_date < $di->sqlDate) echo 'am-error'; ?>" >
                    <?php p(amDate($invoice->rebill_date)) ?>
                </span>
                <?php if ($invoice->isCancelled()): ?>
                    <span class="am-active-invoice-cancel">
                        <?php __e("Cancelled") ?> (<?php p(amDate($invoice->tm_cancelled)) ?>)
                    </span>
                <?php endif ?>
                <?php if ($invoice->isFailed()): ?>
                    <span class="am-active-invoice-failed">
                        <?php __e("Failed") ?>
                    </span>
                <?php endif ?>
                <?php if (!empty($invoice->_manualRebillUrl)): ?>
                    <span class="am-active-invoice-manual-rebill">
                        <a
                            class="manual-rebill local"
                            href="<?php p($invoice->_manualRebillUrl)?>"
                            title="<?php __e('Submit recurring payment for this subscription manually');?>"><?php __e("Renew Manually") ?></a>
                    </span>
                <?php endif; ?>

                <?php if (!empty($invoice->_cancelUrl)): ?>
                    <span class="am-active-invoice-cancel">
                        <a
                            class="cancel-subscription local-link"
                            href="<?php p($invoice->_cancelUrl)?>"
                            title="<?php __e('Cancel future recurring payments');?>"
                            data-public_id="<?php p($invoice->public_id) ?>"
                        ><?php __e("Cancel Subscription") ?></a>
                    </span>
                <?php endif; ?>

                <?php if(!empty($invoice->_restoreUrl)) : ?>
                    <span class="am-active-invoice-restore">
                        <a
                            class="restore-subscription local-link"
                            href="<?php p($invoice->_restoreUrl)?>"
                            title="<?php __e("Resume cancelled or failed subscription");?>"><?php __e("Resume Subscription") ?></a>
                    </span>
                <?php endif; ?>
                <?php if(!empty($invoice->_updateCcUrl)) : ?>
                    <span class="am-active-invoice-update-cc">
                        <a
                            class="update-cc local"
                            href="<?php p($invoice->_updateCcUrl)?>"
                            title="<?php __e('Update Credit Card Information for this subscription');?>"><?php __e("Update Credit Card") ?></a>
                    </span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
    <ul class="am-active-invoice-product-list">
    <?php foreach ($invoice->getItems() as $item): ?>
        <li class="am-active-invoice-product">
            <span class="am-active-invoice-item-title"><?php p($item->item_title) ?><?php if ($item->qty>1) : ?> &times; <?php p($item->qty) ?><?php endif; ?></span>

            <?php if (!empty($item->_upgrades)): ?>
                <span class="am-active-invoice-upgrade">
                    <a href="javascript:" class="upgrade-subscription local-link" data-invoice_item_id="<?php p($this->obfuscate($item->invoice_item_id)) ?>"><?php __e("upgrade") ?></a>
                </span>
                <div class="upgrade-subscription-popup upgrade-subscription-popup-<?php p($this->obfuscate($item->invoice_item_id)) ?>"
                     style="display:none" data-popup-title="<?php __e('Change Subscription') ?>">
                    <?php include $this->_script('member/_upgrade.phtml') ?>
                </div>
            <?php endif ?>
            <!-- options -->
            <?php foreach ($item->getOptions() as $optKey => $opt): ?>
                <br /><b><i><?php p($opt['optionLabel']) ?>:</i></b>
                     <i><?php p(is_array($opt['valueLabel']) ? implode(',', $opt['valueLabel']) : $opt['valueLabel']) ?></i>
                     <?php if ($opt['first_price'] || $opt['second_price']) : ?>
                     <i style='color: gray;'><?= $opt['first_price'] > 0 ? '+' : '&minus;' ?> <?php
                            $o = new stdclass;
                            $o->first_price = abs(floatval($opt['first_price']));
                            $o->second_price = abs(floatval($opt['second_price']));
                            $o->first_period = $invoice->first_period;
                            $o->second_period = $invoice->second_period;
                            $o->rebill_times = $invoice->rebill_times;
                            $o->currency = $invoice->currency;
                            $t = new Am_TermsText($o);
                        echo $t->getStringForOption(); ?></i>
                     <?php endif ?>
            <?php endforeach // item->getOptions() ?>
        </li>
    <?php endforeach ?>
    </ul>
    <?php echo $this->blocks('member/invoice/footer', '<div class="am-active-invoice-footer">%s</div>', ['invoice' => $invoice]); ?>
</div>