<?php

use app\helpers\Html;
use Carbon\Carbon;
use kartik\datecontrol\Module as DateControlModule;

/* @var $price app\modules\subscription\models\SubscriptionPrice */
/* @var $subscription app\modules\subscription\models\Subscription */
/* @var $now int|null */
/* @var $stripeInvoice \Stripe\Invoice|null */

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

$changeSubscription = $subscription && !$subscription->cancelled() ? 1 : 0;

/** @var kartik\datecontrol\Module $dateControlModule */
$dateControlModule = Yii::$app->getModule('datecontrol');
$dateFormat = $dateControlModule->displaySettings[DateControlModule::FORMAT_DATE];
$nextBillingTime = null;
if ($changeSubscription) {
    $nextBillingTime = $subscription->getNextBillingTime();
}
?>

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

    <?php if (!$changeSubscription): ?>
        <div class="alert alert-warning alert-dismissable fade in">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <?= Yii::t('app','Select your payment method, confirm your subscription and enjoy all the features and benefits. Subscription plans may be changed or cancelled at your convenience.') ?>
        </div>
    <?php endif; ?>

    <div class="row">
        <div class="col-sm-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <?php if ($price->isRecurring()): ?>
                        <?= Yii::t('app', 'Confirm subscription') ?>
                    <?php else: ?>
                        <?= Yii::t('app', 'Confirm order') ?>
                    <?php endif; ?>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-sm-4">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <?= Yii::t('app', 'Plan') ?>: <?= $price->product->name ?>
                                </div>
                                <div class="panel-body">
                                    <p style="font-weight: bold;"><?= $price->currency_code ?><?= $price->getFormattedAmount() ?> / <?= $price->interval ?></p>
                                    <p><?= $price->product->description ?></p>
                                    <div class="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>
                        </div>
                        <div class="col-sm-8">
                            <?php if ($changeSubscription): ?>
                                <?php if ($subscription->isPaypal()): ?>
                                    <?php if ($now): ?>
                                        <?= $this->render('partials/_paypal-change-now'); ?>
                                        <?= $this->render('partials/_paypal-button', ['price'=> $price, 'subscription' => $subscription, 'now' => 1] ); ?>
                                    <?php else: ?>
                                        <?= $this->render('partials/_paypal-change-with-billing-cycle', ['nextBillingTime'=> $nextBillingTime] ); ?>
                                        <?= $this->render('partials/_paypal-button', ['price'=> $price, 'subscription' => $subscription, 'now' => 0] ); ?>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <?= $this->render('partials/_stripe-change-now', ['price'=> $price, 'subscription' => $subscription, 'now' => 1, 'stripeInvoice' => $stripeInvoice]); ?>
                                <?php endif; ?>
                            <?php else: ?>
                                <div class="form-group">
                                    <div class="inline-control inline-control-radio">
                                        <label class="control-label"><?= Yii::t('app', 'Payment Method') ?></label>
                                        <?php if ($price->hasPaypalId()): ?>
                                        <input type="radio" id="payment-method-paypal" name="payment-method" value="paypal" checked>
                                        <label for="payment-method-paypal" class="radio-inline"><?= Yii::t('app', 'PayPal') ?></label>
                                        <?php endif; ?>
                                        <?php if ($price->hasStripeId()): ?>
                                        <input type="radio" id="payment-method-stripe" name="payment-method" value="stripe"<?php if (!$price->hasPaypalId()): ?> checked<?php endif; ?>>
                                        <label for="payment-method-stripe" class="radio-inline"><?= Yii::t('app', 'Stripe') ?></label>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <?php if ($price->hasPaypalId()): ?>
                                <div id="paypal-button">
                                    <?= $this->render('partials/_paypal-button', ['price'=> $price, 'subscription' => null, 'now' => 1] ); ?>
                                </div>
                                <?php endif; ?>
                                <?php if ($price->hasStripeId()): ?>
                                <div id="stripe-button">
                                    <?= $this->render('partials/_stripe-button', ['price'=> $price, 'subscription' => null, 'now' => 1] ); ?>
                                </div>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="subscription-pending" style="display: none">
    <h3 style="margin: 5px 0 15px 0"><?= Yii::t('app', 'Subscription Pending') ?></h3>
    <div class="alert alert-warning">
        <?= Yii::t('app', 'We are processing your subscription. Once the subscription has successfully processed, this page will update automatically. Typically, this process should only take a few seconds.') ?>
    </div>
</div>

<?php

$script = <<<JS

    $(document).ready(function(){
        // Handlers
        var toggleSettings = function () {
            if ($("#payment-method-paypal").is(":checked") === true) {
                $("#paypal-button").show();
                $("#stripe-button").hide();
            } else {
                $("#paypal-button").hide();
                $("#stripe-button").show();
            }
        };
        $('input[type="radio"]').click(function(e){
            toggleSettings()
        });
        toggleSettings();
    });

JS;

$this->registerJs($script, $this::POS_END);
