<?php

use app\helpers\Html;
use app\modules\subscription\bundles\PricingTableBundle;
use app\modules\subscription\models\Subscription;
use app\modules\subscription\models\SubscriptionPrice;
use Carbon\Carbon;
use kartik\datecontrol\Module as DateControlModule;
use yii\helpers\Url;

/** @var $subscription Subscription */

PricingTableBundle::register($this);

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

/** @var kartik\datecontrol\Module $dateControlModule */
$dateControlModule = Yii::$app->getModule('datecontrol');
$dateFormat = $dateControlModule->displaySettings[DateControlModule::FORMAT_DATE];

// Calculate the number of days until the subscription expires & expiration date
Carbon::setLocale(substr(Yii::$app->language, 0, 2)); // eg. en-US to en
$endsAt = null;
$diffInDays = null;
$expired = null;
$expirationDate = null;
if (!empty($subscription->ends_at)) {
    $endsAt = Carbon::createFromTimestamp($subscription->ends_at);
    $now = Carbon::now();
    $diffInDays = $now->diffInDays($endsAt, false);
    $expired = $diffInDays < 0;
    $diffInDays = $now->diffInDays($endsAt);
    $expirationDate = Yii::$app->formatter->asDate($subscription->ends_at, $dateFormat);
}

/** @var array $nextSubscription Is there a next subscription? **/
$nextSubscription = $subscription->getNextSubscription();
$nextSubscriptionTime = null;
if (isset($nextSubscription['info']['next_billing_time'])) {
    $nextSubscriptionTime = $subscription->getNextBillingTime();
}
$nextSubscriptionName = null;
if (!empty($nextSubscription['name'])) {
    $nextSubscriptionName = $nextSubscription['name'];
}

?>

<h3 style="margin: 5px 0 15px 0"><?= Html::encode($this->title) ?><?php if ($subscription->onGracePeriod() && $subscription->isRecurring()): ?> <small class="text-muted"><?= Yii::t('app', 'On Grace Period') ?></small><?php endif; ?></h3>

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title"><?= $subscription->name ?></h3>
    </div>
    <div class="panel-body">
        <?php if ($subscription->isRecurring()): ?>
            <?php if ($nextSubscription): ?>
                <div class="alert alert-info">
                    <span class="glyphicon glyphicon-info-sign"> </span>
                    <?= Yii::t('app', 'Your subscription will be updated to {next_subscription_name} at the end of your current billing cycle {next_subscription_time}', [
                        'next_subscription_name' => '<strong>'. $nextSubscriptionName . '</strong>',
                        'next_subscription_time' => '<strong>'. $nextSubscriptionTime . '</strong>',
                    ]) ?>
                </div>
            <?php endif; ?>
            <p style="font-weight: bold;"><?= $subscription->getCurrencySymbol() ?><?= $subscription->getAmount() ?> / <?= $subscription->displayInfo('interval') ?></p>
        <?php else: ?>
            <p style="font-weight: bold;"><?= $subscription->getCurrencySymbol() ?><?= $subscription->getAmount() ?> / <?= Yii::t('app', 'One-Time Payment') ?></p>
        <?php endif; ?>
        <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>
        <?php if ($subscription->isRecurring()): ?>
        <p><?= Html::a(Yii::t('app', 'Change Plan'), ['/subscription/user/change'], ['class' => 'btn btn-primary']) ?></p>
        <?php endif; ?>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title"><?= Yii::t('app', 'Payment Method') ?></h3>
    </div>
    <div class="panel-body">
        <p><?= Yii::t('app', 'Your current payment method is {payment_method}.', [
                'payment_method' => '<strong style="text-transform: capitalize">' . $subscription->gateway . '</strong>',
            ]) ?></p>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title"><?= Yii::t('app', 'Billing History') ?></h3>
    </div>
    <div class="panel-body">
        <p>
            <?= Yii::t('app', 'If you would like to update your billing details, please submit {your_billing_information}.', [
                'your_billing_information' => Html::a('your billing information', Url::to(['/subscription/user/billing-info']))
            ]) ?>
        </p>
        <p><?= Html::a(Yii::t('app', 'View Receipts'),
            ['/subscription/user/receipts'], [
                'class' => 'btn btn-primary',
            ]) ?></p>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title"><?= Yii::t('app', 'Cancel Subscription') ?></h3>
    </div>
    <div class="panel-body">
        <?php if ($subscription->type === SubscriptionPrice::TYPE_RECURRING): ?>
            <?php if ($subscription->onGracePeriod()): ?>
                <p><?= Yii::t('app', 'Your subscription has been cancelled, however your account will stay active until the end of the current billing cycle on {expirationDate}.', [
                        'expirationDate' => '<strong>' . $expirationDate . '<strong>',
                    ]) ?></p>
                <p><?= Html::a(Yii::t('app', 'Cancel Now'),
                        ['/subscription/user/cancel-now'], [
                            'class' => 'btn btn-danger',
                            'data-method' => 'POST',
                            'data-confirm' => Yii::t('app', 'Are you sure you want to cancel your subscription right now?'),
                            'data-params' => [
                                'id' => $subscription->id,
                            ],
                        ]) ?></p>
            <?php else: ?>
                <p><?= Yii::t('app', 'You may cancel your subscription at any time. Once your subscription has been cancelled, you will have the option to resume the subscription until the end of your current billing cycle.') ?></p>
                <p><?= Html::a(Yii::t('app', 'Cancel Subscription'),
                        ['/subscription/user/cancel'], [
                            'class' => 'btn btn-danger',
                            'data-method' => 'POST',
                            'data-confirm' => Yii::t('app', 'Are you sure you want to cancel your subscription?'),
                            'data-params' => [
                                'id' => $subscription->id,
                            ],
                        ]) ?></p>
            <?php endif; ?>
        <?php else: ?>
            <?php if ($expired): ?>
                <p><?= Yii::t('app', 'Your subscription expired {n,plural,=0{today} =1{# day} other{# days}} ago on {expirationDate}', [
                        'n' => $diffInDays,
                        'expirationDate' => $expirationDate,
                    ]) ?></p>
            <?php else: ?>
                <p><?= Yii::t('app', 'Your subscription will end in {diffInDays} days on {expirationDate}', [
                        'diffInDays' => $diffInDays,
                        'expirationDate' => $expirationDate,
                    ]) ?></p>
                <p><?= Html::a(Yii::t('app', 'Cancel Now'),
                        ['/subscription/user/cancel-now'], [
                            'class' => 'btn btn-danger',
                            'data-method' => 'POST',
                            'data-confirm' => Yii::t('app', 'Are you sure you want to cancel your subscription right now? You will lose all the features and benefits of your current plan. This action cannot be undone.'),
                            'data-params' => [
                                'id' => $subscription->id,
                            ],
                        ]) ?></p>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>