<?php

if ($status == 'success') {
    echo '<div class="image_discount">';
    echo '</div>';

    echo '<div class="form_discount">';
    echo '<div id="discount" class="discount_form__title">' . __('Вы получили скидку 3%') . '</div>';
    echo '<div class="discount_form_text">Введите номер телефона, чтобы сохранить скидку</div>';
    echo '<form class="discount_form" method="post" action="' . url_for('@discount') . '" onsubmit="discount.submit(this);return false;">';
    echo '<div class="discount_form__process">';
    echo '<div class="discount_form__inp_wrap"><input name="discount[phone]" type="text" id="phone_mask" class="form_type_1__input validate_it" placeholder="Номер телефона" onkeyup="form_validator.check($(this))" data-check-rule="phone" autocomplete="off" required></div>';
    echo '<input name="discount[url]" type="hidden" value="' . $discount . '">';
    $culture = $sf_user->getCulture();
    ?>
    <div class="discount_form__checkbox">
        <label class="checkbox ver2">
            <input type="checkbox" class="validater validate_it not_custom" onchange="form_validator.check($(this))"
                   data-check-rule="checked" autocomplete="off" required>
            <span class="checkbox_span"></span>
            <span class="checkbox_text"><?php echo __('Я принимаю условия') ?> <a
                        href="<?php echo '/' . $culture . '/terms-of-use/' ?>"
                        target="_blank"><?php echo __('пользовательского соглашения') ?></a><?php echo __(' и ') ?>
                <a href="<?php echo '/' . $culture . '/privacy-policy/' ?>"
                   target="_blank"><?php echo __('политики конфиденциальности') ?></a></span>
        </label>
    </div>
    <i class="br15"></i>
    <?php
    echo '<input type="submit" class="discount_form__submit green_b_btn send btn_disabled" value="' . __('Cохранить') . '" disabled />';
    echo '</div>';
    echo '</form>';
    echo '<div class="discount_form_head_text">Хотите скидку больше?<br>Тогда посмотрите семь товаров.</div>';
    echo '<div class="discount_form_text">В течение 30 дней вы можете сделать заказ со скидкой.</div>';
    echo '</div>';
} elseif ($status == 'complete') {
    echo '<div class="form_discount">';
    echo '<div id="discount" class="discount_form__title">' . __('Скидка успешно активирована!') . '</div>';
    echo '<div class="discount_form__submit green_b_btn" onclick="discount.close();">Понятно</div>';
    echo '</div>';
} elseif ($status == 'error') {
    echo '<div class="form_discount">';
    echo '<div id="discount" class="discount_form__title">' . __($error) . '</div>';
    echo '<div class="discount_form__submit green_b_btn" onclick="discount.close();">Понятно</div>';
    echo '</div>';
}

?>
<script>
    var lastvalue = '';
    var lastvalueunmask = '';


    function mask(str) {
        var arr = str.split('');
        if (str.length >= 2)
        {
            arr.splice(2,0," (");
            if (str.length >= 5)
            {
                arr.splice(6,0,") ");
            }
            if (str.length >=8)
            {
                arr.splice(10,0,"-");
            }
            if (str.length >= 10)
            {
                arr.splice(13,0,"-");
            }
        }
        return arr.join('');
    }

    function unmask(str) {

        var rep = /[()\-\s]/g;
        var value = str.replace(rep, '');

        return value;
    }

    $('#phone_mask').on('input', function () {
        var value = unmask($(this).val());
        var last = value.slice(-1);
        var first = value.charAt(0);
        var rep = /(^\+7)(9)([0-9]{0,9})/g;

        if ((value.length == 2) && (first == '+'))
        {
            if (value == '+9')
            {
                value = '+79';
                lastvalue = '+79';
            }else
            {
                value = '+7';
                lastvalue = '+7';
            }
        }else
        if ((value == '7') || (value == '8'))
        {
            value = '+7';
        } else
        if (value == '9')
        {
            value = '+79';
        } else
        {
            var execval = rep.exec(value);
            if (execval != null)
            {
                value = execval[0];
            } else
            {
                if ((last != '9'))
                {
                    value = value.substring(0, value.length - 1)
                }
            }
        }

        $(this).val(mask(value));
        lastvalue = $(this).val();
        lastvalueunmask = value;
    });

    $('#phone_mask').on('keydown', function (event) {
        var key = event.keyCode || event.charCode;

        if( key == 8 || key == 46 ){
            var value = unmask($('#phone_mask').val());
            value = value.substring(0, value.length - 1);
            $('#phone_mask').val(mask(value));
            lastvalue = $('#phone_mask').val();
            return false;
        }
    });
</script>

