<?php
/*
 * Copyright Magmodules.eu. All rights reserved.
 * See COPYING.txt for license details.
 */

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\HeroiconsOutline;
use Magento\Framework\Escaper;
use Mollie\Subscriptions\Block\Frontend\Customer\Account\ActiveSubscriptions;

/** @var ViewModelRegistry $viewModels */
/** @var Escaper $escaper */
/** @var ActiveSubscriptions $block */

/** @var HeroiconsOutline $heroicons */
$heroicons = $viewModels->require(HeroiconsOutline::class);

$subscriptions = $block->getSubscriptions();

$modalViewModel = $viewModels->require(\Hyva\Theme\ViewModel\Modal::class);
?>

<?php if (!$subscriptions): ?>
    <div class="message info empty">
        <span>
            <?= $escaper->escapeHtml(__('You have no active subscriptions.')) ?>
        </span>
    </div>
<?php else: ?>
    <div class="card grid grid-cols-3 lg:grid-cols-6 mb-4">
        <div class="text-sm text-secondary p-2"><?= $block->escapeHtml(__('ID')) ?></div>
        <div class="text-sm text-secondary p-2"><?= $block->escapeHtml(__('Date')) ?></div>
        <div class="text-sm text-secondary p-2"><?= $block->escapeHtml(__('Status')) ?></div>
        <div class="text-sm text-secondary p-2"><?= $block->escapeHtml(__('Description')) ?></div>
        <div class="text-sm text-secondary p-2"><?= $block->escapeHtml(__('Amount')) ?></div>
        <div class="text-sm text-secondary p-2 text-center"><?= $block->escapeHtml(__('Action')) ?></div>

        <?php $i = 0; ?>
        <?php foreach ($subscriptions as $subscription): ?>
            <?php $i++; ?>
            <div class="p-2 flex items-center <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                <?= $block->escapeHtml($subscription->getId()) ?>
            </div>
            <div class="p-2 flex items-center <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                <?= /* @noEscape */ $block->formatDate($subscription->getCreatedAt()) ?>
            </div>
            <div class="p-2 flex items-center <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                <?= $block->escapeHtml($subscription->getStatus()) ?>
            </div>
            <div class="p-2 flex items-center <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                <?= $block->escapeHtml($subscription->getDescription()) ?>
            </div>
            <div class="p-2 flex items-center <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                <?= /* @noEscape */ $block->formatPrice($subscription->getAmount()) ?>
            </div>
            <div class="p-2 flex items-center justify-evenly<?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                <?php if (!$block->hasParent($subscription->getId())): ?>
                    <?php if ($subscription->getStatus() == 'active'): ?>
                        <?php $formData = json_decode(
                            $this->helper(\Magento\Framework\Data\Helper\PostHelper::class)->getPostData(
                                $block->getUrl(
                                    'mollie_subscriptions/index/cancel',
                                    ['subscription_id' => $subscription->getId()]
                                ),
                            ),
                            true
                        ); ?>

                        <form method="post"
                              action="<?= /* @noEscape */ $formData['action'] ?>"
                              class="inline-flex items-center"
                              x-data="hyva.modal()"
                              x-ref="subscription-<?= $escaper->escapeHtmlAttr($subscription->getId()); ?>"
                        >
                            <?= $block->getBlockHtml('formkey'); ?>

                            <input type="hidden" name="data"
                                   value="<?= $escaper->escapeHtmlAttr(json_encode($formData['data'])) ?>"
                            />

                            <?php $confirmation = $modalViewModel->confirm()
                                ->withDetails($escaper->escapeHtml(__('Are you sure you want to cancel this subscription?'))) ?>
                            <button type="submit"
                                @click.prevent="<?= $escaper->escapeHtmlAttr($confirmation->getShowJs()) ?>.then(result => result === true ? $refs['subscription-<?= $escaper->escapeHtmlAttr($subscription->getId()); ?>'].submit() : '')"
                                class="inline-block text-sm underline text-secondary-darker"
                               title="<?= $escaper->escapeHtmlAttr(__('Cancel')); ?>"
                            >
                                <span><?= /** @noEscape */ $heroicons->xHtml() ?></span>
                            </button>

                            <?= /** @noEscape */ $confirmation->render() ?>
                        </form>
                    <?php endif; ?>

                    <?php if ($subscription->getStatus() == 'canceled'): ?>
                        <?php $formData = json_decode(
                            $this->helper(\Magento\Framework\Data\Helper\PostHelper::class)->getPostData(
                                $block->getUrl(
                                    'mollie_subscriptions/index/restart',
                                    ['subscription_id' => $subscription->getId()]
                                )
                            ),
                            true
                        ); ?>
                        <form action="<?= /* @noEscape */ $formData['action'] ?>" method="post"
                              class="inline-flex items-center">
                            <?= $block->getBlockHtml('formkey'); ?>
                            <input type="hidden" name="data"
                                   value="<?= $escaper->escapeHtmlAttr(json_encode($formData['data'])) ?>"
                            />

                            <button type="submit"
                                    class="inline-block text-sm underline text-secondary-darker"
                                    title="<?= $escaper->escapeHtmlAttr(__('Restart')); ?>"
                            >
                                <span><?= $heroicons->refreshHtml() ?></span>
                            </a>
                        </form>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
