<?php

use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;

use common\modules\content\models\CoContent;
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;

$more = CoContent::find()
    ->where([
        'category_id' => 4,
        'active' => true
    ])
    ->andWhere(['!=', 'id', $model->id])
    ->orderBy('RAND()')
    ->one();
?>

<!-- <section class="short_keys_sect">
    <div class="container"> -->
        <div class="row">
            <div class="col-md-12 col-xs-12 col-sm-12">
                <h2 class="short_keys_title"><?=Yii::t('case', 'Explore another case')?></h2>
            </div>
        </div>
        <div class="row">
            <?php if($more) : ?>
                <div class="col-md-6 col-xs-6 col-sm-12">
                    <div class="short_keys_block">
                        <img src="<?=$more->preview?>" height="338" width="455">
                        <a href="<?=Url::to(['/'.$more->url]);?>" style="color:<?if($more->custom==CoContent::CUSTOM_WHITE){?>#fff<?}else{?>#2d3642<?}?>;" class="short_keys_title_link"><?=$more->lang->title?></a>
                        <div class="short_keys_foot">
                            <a href="<?=Url::to(['/'.$more->url]);?>" class="short_keys_btn_more"><span><?=Yii::t('case', 'Read more')?></span></a>
                            <!-- <a href="#" class="short_keys_tags"># Big data</a> -->
                        </div>
                    </div>
                </div>
            <?php endif; ?>

            <?php
            //$show = true;
            //if (!Yii::$app->user->isGuest && Bid::find()->where(['email' => Yii::$app->user->identity->email])->exists())
            //    $show = false;

            //if ($show===true):
            if ($userIsSubscribed===false): ?>

                <div class="col-md-6 col-xs-6 col-sm-12">
                    <div class="subsc_block">
                        <h2 class="subsc_block_title"><?=\Yii::t('form', 'Subscribe for cases updates')?></h2>
                        <div class="subsc_block_txt"><?=\Yii::t('form', 'When we will prepare new interesting case – we will send you a detailed letter. Subscribe here!')?></div>
                        <?php
                        $model = new CasesBids();

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

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

                        <div class="message-box send_err" style="color:red; left: 38%; top: 67%;"></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>

                <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; ?>
        </div>
    <!-- </div>
</section> -->
<style type="text/css">
    .field-bid-email,
    .field-bid-email input {
        margin-bottom: 0;
    }
</style>