<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\modules\cases\models\CasesBids;

$userIsSubscribed = false;
$cacheEmail = Yii::$app->session->get('user_email');
if ($cacheEmail!==false && CasesBids::find()->where(['email' => $cacheEmail])->exists()===true)
    $userIsSubscribed = true;
?>

<?php if ($userIsSubscribed===false): ?>
<div class="row">
    <div class="col-md-12 col-xs-12 col-sm-12">
        <div class="keys_send_mail">
            <p><?=\Yii::t('form', 'The cases description process require a lot of time. Thus, we write them at free time. Subscribe to follow the updates.')?></p>
            <?php 
            $model = new CasesBids();

            $form = ActiveForm::begin([
                'action' => '/cases/default/add',
                'enableClientValidation' => false,
                'options' => [
                    'class' => 'keys_mail_form bids-form',
                    'data-title' => 'Страница Кейсы',
                    'data-form' => 'Подпись на обновления в кейсах',
                ],
            ]);
            ?>

                <div class="message-box send_secce">Теперь вы подписаны на обновления кейсов.</div>

                <div class="message-box send_err" style="color:red; left: 21%; top: 56%;"></div>

                <div class="content">

                    <?php echo Html::hiddenInput('scenario', $model->scenario, ['class' => 'not_clear']); ?>
                    
                    <?php //echo $form->field($model, 'form', ['template' => '{input}'])->hiddenInput(['class' => 'not_clear']); ?>
                    
                    <?php echo $form->field($model, 'email', [
                        'template' => '<div class="row"><div class="col-sm-4">{input}</div></div>',
                        'errorOptions' => []
                    ])->textInput([
                        'placeholder' => 'E-mail*',
                    ]); ?>
                    
                    <?php echo Html::button(\Yii::t('form', 'Subscribe'), ['class' => 'save-button']); ?>

                </div>

            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div>

<script type='text/javascript'>
    $('.save-button').on('click', function() {
        var form = $(this).closest('form');
        var email = form.find('input[name="CasesBids[email]"]').val();
        $.ajax({
            url: form.attr('action'),
            data: {
                email: email
            },
            method: 'GET',
            success: function(response) {
                var result = JSON.parse(response);
                if (result.result==true) {
                    form.find('.message-box.send_secce').fadeIn('fast');
                    form.find('.content').css('visibility','hidden');

                    setTimeout(function(){
                        form.find('.message-box.send_secce').fadeOut('fast');
                        form.find('.content').css('visibility','visible');
                    }, 2000);

                    setInterval(function() {
                        form.closest('.row').fadeOut(500);
                    }, 1600);
                } else {
                    form.find('.message-box.send_err').html(result.error);
                    form.find('.message-box.send_err').fadeIn('fast');

                    setTimeout(function(){
                        form.find('.message-box.send_err').fadeOut('fast');
                    }, 2000);
                }
            }
        });
    });
</script>
<?php endif; ?>