<?php
/**
 * Copyright © 2019 Magenest. All rights reserved.
 */
// @codingStandardsIgnoreFile
/** @var \Magenest\SagePay\Block\Adminhtml\Profile\View $block */
$profile = $block->getProfile();
$order = $block->getOrder();
$product = $block->getProduct();

$total_cycles = $profile->getData('total_cycles');
$remaining_cycles = $profile->getData('remaining_cycles');
$nextOrders = $profile->getData('sequence_order_ids') ? explode("-", $profile->getData('sequence_order_ids')) : [];
$orderUrl = $block->getOrderUrl($order);
$cancelUrl = $block->getCancelUrl($profile);
?>

<section class="admin__page-section order-view-account-information">
    <div class="admin__page-section-title">
        <span class="title"><?php echo $block->escapeHtml(__('Profile Information')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-information">
            <div class="admin__page-section-item-title">
                <span class="title"><?php echo __('Recurring Detail'); ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <table class="admin__table-secondary order-information-table">
                    <tr>
                        <th><?php echo __('Status'); ?></th>
                        <td><?php echo $profile->getStatus(); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo __('Frequency'); ?></th>
                        <td><?php echo $profile->getFrequency(); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo __('Total Cycles'); ?></th>
                        <?php if ($total_cycles == 0) { ?>
                            <td><?php echo __('Ongoing Subscription'); ?></td>
                        <?php } else { ?>
                            <td><?php echo $total_cycles; ?></td>
                        <?php } ?>
                    </tr>
                    <tr>
                        <th><?php echo __('Remaining Cycles'); ?></th>
                        <?php if ($remaining_cycles == 0) { ?>
                            <td><?php echo __('Ongoing'); ?></td>
                        <?php } else { ?>
                            <td><?php echo $remaining_cycles; ?></td>
                        <?php } ?>
                    </tr>
                    <tr>
                        <th><?php echo __('Start Date'); ?></th>
                        <td><?php echo $profile->getData('start_date'); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo __('Last Billed Date'); ?></th>
                        <td><?php echo $profile->getData('last_billed'); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo __('Next Billing Date'); ?></th>
                        <td><?php echo $profile->getData('next_billing'); ?></td>
                    </tr>
                </table>
            </div>
            <br>
            <div class="admin__page-section-item-title">
                <span class="title"><?php echo __('Order Detail'); ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <table class="admin__table-secondary order-information-table">
                    <tr>
                        <th><?php echo __('Order ID'); ?></th>
                        <td><a href="<?php echo $orderUrl ?>"><?php echo $order->getIncrementId(); ?></a></td>
                    </tr>
                    <tr>
                        <th><?php echo __('Grand Total'); ?></th>
                        <td><?php echo number_format($order->getGrandTotal(), 2); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo __('Product Name'); ?></th>
                        <td><?php echo $product->getName(); ?></td>
                    </tr>
                    <tr>
                        <th><?php echo __('Customer Name'); ?></th>
                        <td><?php echo $order->getCustomerName(); ?></td>
                    </tr>
                    <?php if ($profile->canCancel()) { ?>
                        <tr>
                            <th><?php echo __('Action'); ?></th>
                            <td><a href="<?php echo $cancelUrl; ?>">Cancel</a></td>
                        </tr>
                    <?php } ?>
                </table>
            </div>
        </div>

        <div class="admin__page-section-item order-account-information">
            <div class="admin__page-section-item-title">
                <span class="title"><?php echo __('Subsequent Orders'); ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <table class="admin__table-secondary order-information-table">
                    <?php foreach ($nextOrders as $nextOrder) { ?>
                        <tr>
                            <?php if ($nextOrder != '0') { ?>
                                <td><a href="<?php echo $block->getOrderUrl($nextOrder); ?>"><?php echo $nextOrder ?></a></td>
                            <?php } else { ?>
                                <th><?php echo __('Failed Payment'); ?></th>
                            <?php } ?>
                        </tr>
                    <?php } ?>
                </table>
            </div>

        </div>
    </div>
</section>
