<?php
// @codingStandardsIgnoreFile
/**
 * @var \Magento\Paypal\Block\Billing\Agreements $block
 */

$billingAgreements = $block->getBillingAgreements();
$paymentMethods = $block->getWizardPaymentMethodOptions();
?>
<div class="account-billing-agreements">

    <div class="cs-dashboard__block cs-info-block">
        <?php if (count($billingAgreements) > 0): ?>
            <?php echo $block->getChildHtml('pager'); ?>
            <div class="cs-table cs-table--tablet-small | billing-agreements">
                <table id="billing-agreements" class="cs-table__body | table-billing-agreements">
                    <caption class="cs-visually-hidden"><?php echo $block->escapeHtml(__('Billing Agreements')); ?></caption>
                    <thead class="cs-table__head">
                    <tr>
                        <th scope="col" class="cs-table__col-head id"><?php echo $block->escapeHtml(__('Reference ID')); ?></th>
                        <th scope="col" class="cs-table__col-head status"><?php echo $block->escapeHtml(__('Status')); ?></th>
                        <th scope="col" class="cs-table__col-head created"><?php echo $block->escapeHtml(__('Created At')); ?></th>
                        <th scope="col" class="cs-table__col-head updated"><?php echo $block->escapeHtml(__('Updated At')); ?></th>
                        <th scope="col" class="cs-table__col-head payment"><?php echo $block->escapeHtml(__('Payment Method')); ?></th>
                        <th scope="col" class="cs-table__col-head actions">&nbsp;</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($billingAgreements as $item): ?>
                        <tr>
                            <td data-th="<?php echo $block->escapeHtml(__('Reference ID')); ?>" class="cs-table__col id">
                                <?php echo $block->escapeHtml($block->getItemValue($item, 'reference_id')); ?>
                            </td>
                            <td data-th="<?php echo $block->escapeHtml(__('Status')); ?>" class="cs-table__col status">
                                <?php echo $block->escapeHtml($block->getItemValue($item, 'status')); ?>
                            </td>
                            <td data-th="<?php echo $block->escapeHtml(__('Created At')); ?>" class="cs-table__col created">
                                <?php echo $block->escapeHtml($block->getItemValue($item, 'created_at')); ?>
                            </td>
                            <td data-th="<?php echo $block->escapeHtml(__('Updated At')); ?>" class="cs-table__col updated">
                                <?php echo $block->escapeHtml($block->getItemValue($item, 'updated_at')); ?>
                            </td>
                            <td data-th="<?php echo $block->escapeHtml(__('Payment Method')); ?>" class="cs-table__col payment">
                                <?php echo $block->escapeHtml($block->getItemValue($item, 'payment_method_label')); ?>
                            </td>
                            <td data-th="" class="cs-table__col cs-table__col--actions actions">
                                <a href="<?php echo $block->escapeUrl($block->getItemValue($item, 'edit_url')); ?>"
                                   class="cs-link">
                                    <span><?php echo $block->escapeHtml(__('View')); ?></span>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <div class="cs-t-text | message info empty">
                <span><?php echo $block->escapeHtml(__('There are no billing agreements yet.')); ?></span>
            </div>
        <?php endif; ?>
    </div>

    <?php if ($paymentMethods): ?>
        <div class="cs-dashboard__block cs-info-block">
            <form action="<?php echo $block->escapeUrl($block->getCreateUrl()); ?>" method="post"
                  class="form form-new-agreement">
                <fieldset class="fieldset">
                    <legend class="cs-title-block cs-title-block--bordered">
                        <span class="cs-t-headline cs-t-headline--size_2 cs-t-headline--look_light">
                            <?php echo $block->escapeHtml(__('New Billing Agreement')); ?></span>
                    </legend>
                    <br />
                    <p class="cs-t-text">
                        <?php echo $block->escapeHtml(__('You will be redirected to the payment system website.')); ?>
                    </p>

                    <div class="cs-form__field payment method">
                        <div class="control">
                            <select id="payment_method" name="payment_method" class="cs-select">
                                <option value=""><?php echo $block->escapeHtml(__('-- Please Select --'));?></option>
                                <?php foreach ($paymentMethods as $code => $title): ?>
                                    <option value="<?php echo $block->escapeHtml($code); ?>">
                                        <?php echo $block->escapeHtml($title); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="cs-info-block__buttons">
                        <button type="submit" class="cs-info-block__button | cs-button cs-button--size_300 cs-button--look_default">
                            <span class="cs-button__span"><?php echo $block->escapeHtml(__('Create...')); ?></span>
                            <svg class="cs-button__icon">
                                <use xlink:href="#arrow_next"></use>
                            </svg>
                        </button>
                    </div>
                </fieldset>
            </form>
        </div>
    <?php endif; ?>
</div>
