<?php

use app\helpers\Html;
use app\modules\subscription\bundles\PricingTableBundle;
use app\modules\subscription\models\Subscription;
use app\modules\subscription\models\SubscriptionPrice;
use app\modules\subscription\models\SubscriptionProduct;

/** @var $products SubscriptionProduct[] */
/* @var $switcher boolean */
/* @var $intervals array */
/** @var $subscription Subscription */
/** @var $monthlyAndYearlyPrices SubscriptionPrice[] */
/** @var $otherRecurringPrices SubscriptionPrice[] */
/** @var $oneTimePrices SubscriptionPrice[] */
/** @var $validSubscription bool */

PricingTableBundle::register($this);

$validSubscription = !empty($subscription) && $subscription->valid();

$this->title = $validSubscription ? Yii::t('app', 'Change Subscription Plan') : Yii::t('app', 'Subscribe');
$this->params['breadcrumbs'][] = $this->title;

?>

<div id="subscription-products">
    <h3 style="margin: 5px 0 15px 0"><?= Html::encode($this->title) ?></h3>

    <?php if (!$validSubscription): ?>
    <div class="alert alert-warning alert-dismissable fade in">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <?= Yii::t('app', 'It looks like you do not have an active subscription. You may choose one of the subscription plans below to get started. Subscription plans may be changed or cancelled at your convenience.') ?>
    </div>
    <?php endif; ?>

    <div class="row">
        <div class="col-xs-12" style="text-align: left">
            <div class="pricing-switcher">
                <div class="fieldset">
                    <?php $i = 1; foreach ($intervals as $interval => $name): ?>
                        <input type="radio" name="duration" value="<?= $interval ?>" id="<?= $interval ?>" <?= $i === 1 ? 'checked' : '' ?>>
                        <label for="<?= $interval ?>"><?= $name ?></label>
                        <?php $i++; endforeach; ?>
                    <span class="switch"></span>
                </div>
            </div>
        </div>
    </div>

    <?php foreach ($monthlyAndYearlyPrices as $price): ?>
        <?php if ($validSubscription && $subscription && $subscription->price_id === $price->id): ?>
            <div class="panel panel-default <?= $price->interval ?>">
                <div class="panel-heading">
                    <h3 class="panel-title"><?= $subscription->name ?></h3>
                </div>
                <div class="panel-body">
                    <p style="font-weight: bold;"><?= $subscription->getCurrencySymbol() ?><?= $subscription->getAmount() ?> / <?= $subscription->displayInfo('interval') ?></p>
                    <p><?= $subscription->displayInfo('description') ?></p>
                    <div class="plan-features">
                        <?php $details = explode("\n", str_replace("\r", "", $subscription->displayInfo('details'))); ?>
                        <?php foreach ($details as $detail): ?>
                            <p><span class="glyphicon glyphicon-ok text-success"> </span> <?= $detail ?></p>
                        <?php endforeach; ?>
                    </div>
                </div>
                <div class="panel-footer">
                    <footer class="text-right text-muted" style="margin: 3px 0">
                        <?= Yii::t('app', 'Currently Subscribed') ?>
                    </footer>
                </div>
            </div>
        <?php elseif ($price->product->status === SubscriptionProduct::ON): ?>
            <div class="panel panel-default <?= $price->interval ?>">
                <div class="panel-heading">
                    <h3 class="panel-title"><?= $price->product->name ?></h3>
                </div>
                <div class="panel-body">
                    <p style="font-weight: bold;"><?= $price->getCurrencySymbol() ?><?= $price->getFormattedAmount() ?> / <?= $price->interval ?></p>
                    <p><?= $price->description ?></p>
                    <div class="plan-features">
                        <?php $details = explode("\n", str_replace("\r", "", $price->product->details)); ?>
                        <?php foreach ($details as $detail): ?>
                            <p><span class="glyphicon glyphicon-ok text-success"> </span> <?= $detail ?></p>
                        <?php endforeach; ?>
                    </div>
                </div>
                <div class="panel-footer">
                    <footer class="text-right">
                        <?php if ($validSubscription): ?>
                            <?php if ($subscription->isPaypal()): ?>
                                <?= Html::a(Yii::t('app', 'Change Plan With Next Billing Cycle'),
                                    ['/subscription/user/confirm', 'id' => $price->id], [
                                        'class' => 'btn btn-default',
                                    ]) ?>
                            <?php endif; ?>
                            <?= Html::a(Yii::t('app', 'Change Plan Now'),
                                ['/subscription/user/confirm', 'id' => $price->id, 'now' => 1], [
                                    'class' => 'btn btn-primary',
                                ]) ?>
                        <?php else: ?>
                            <?= Html::a(Yii::t('app', 'Subscribe'),
                                ['/subscription/user/confirm', 'id' => $price->id], [
                                    'class' => 'btn btn-primary',
                                ]) ?>
                        <?php endif; ?>
                    </footer>
                </div>
            </div>
        <?php endif; ?>
    <?php endforeach; ?>

    <?php if (count($otherRecurringPrices) > 0): ?>
        <h3 style="margin: 5px 0 15px 0"><?= Yii::t('app', 'Other Recurring Plans') ?></h3>
        <?php foreach ($otherRecurringPrices as $price): ?>
            <?php if ($price->product->status === SubscriptionProduct::ON && $validSubscription && $subscription && $subscription->price_id === $price->id): ?>
                <div class="panel panel-default <?= $price->interval ?>">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?= $subscription->name ?></h3>
                    </div>
                    <div class="panel-body">
                        <p style="font-weight: bold;"><?= $subscription->getCurrencySymbol() ?><?= $subscription->getAmount() ?> / <?= $subscription->displayInfo('interval') ?></p>
                        <p><?= $subscription->displayInfo('description') ?></p>
                        <div class="plan-features">
                            <?php $details = explode("\n", str_replace("\r", "", $subscription->displayInfo('details'))); ?>
                            <?php foreach ($details as $detail): ?>
                                <p><span class="glyphicon glyphicon-ok text-success"> </span> <?= $detail ?></p>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <footer class="text-right text-muted" style="margin: 3px 0">
                            <?= Yii::t('app', 'Currently Subscribed') ?>
                        </footer>
                    </div>
                </div>
            <?php elseif ($price->product->status === SubscriptionProduct::ON): ?>
                <div class="panel panel-default <?= $price->interval ?>">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?= $price->product->name ?></h3>
                    </div>
                    <div class="panel-body">
                        <p style="font-weight: bold;"><?= $price->getCurrencySymbol() ?><?= $price->getFormattedAmount() ?> / <?= $price->interval ?></p>
                        <p><?= $price->description ?></p>
                        <div class="plan-features">
                            <?php $details = explode("\n", str_replace("\r", "", $price->product->details)); ?>
                            <?php foreach ($details as $detail): ?>
                                <p><span class="glyphicon glyphicon-ok text-success"> </span> <?= $detail ?></p>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <footer class="text-right">
                            <?= Html::a(Yii::t('app', 'Subscribe'),
                                ['/subscription/user/confirm', 'id' => $price->id], [
                                    'class' => 'btn btn-primary',
                                ]) ?>
                        </footer>
                    </div>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
    <?php endif; ?>

    <?php if (count($oneTimePrices) > 0): ?>
        <h3 style="margin: 5px 0 15px 0"><?= Yii::t('app', 'One-Time Payment Plans') ?></h3>
        <?php foreach ($oneTimePrices as $price): ?>
            <?php if ($price->product->status === SubscriptionProduct::ON && $validSubscription && $subscription && $subscription->price_id === $price->id): ?>
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?= $subscription->name ?></h3>
                    </div>
                    <div class="panel-body">
                        <p style="font-weight: bold;"><?= $subscription->getCurrencySymbol() ?><?= $subscription->getAmount() ?> / <?= Yii::t('app', 'One-Time Payment') ?></p>
                        <p><?= $subscription->displayInfo('description') ?></p>
                        <div class="plan-features">
                            <?php $details = explode("\n", str_replace("\r", "", $subscription->displayInfo('details'))); ?>
                            <?php foreach ($details as $detail): ?>
                                <p><span class="glyphicon glyphicon-ok text-success"> </span> <?= $detail ?></p>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <footer class="text-right text-muted" style="margin: 3px 0">
                            <?= Yii::t('app', 'Currently Subscribed') ?>
                        </footer>
                    </div>
                </div>
            <?php elseif ($price->product->status === SubscriptionProduct::ON): ?>
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?= $price->product->name ?></h3>
                    </div>
                    <div class="panel-body">
                        <p style="font-weight: bold;"><?= $price->getCurrencySymbol() ?><?= $price->getFormattedAmount() ?> / <?= Yii::t('app', 'One-Time Payment') ?></p>
                        <p><?= $price->description ?></p>
                        <div class="plan-features">
                            <?php $details = explode("\n", str_replace("\r", "", $price->product->details)); ?>
                            <?php foreach ($details as $detail): ?>
                                <p><span class="glyphicon glyphicon-ok text-success"> </span> <?= $detail ?></p>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <footer class="text-right">
                            <?= Html::a(Yii::t('app', 'Subscribe'),
                                ['/subscription/user/confirm', 'id' => $price->id], [
                                    'class' => 'btn btn-primary',
                                ]) ?>
                        </footer>
                    </div>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
    <?php endif; ?>
</div>
