<style>
    @media (min-width: 650px) {
        html {
            height: 100%;
        }
        body {
            height: 100%;
        }
        .body_wrapper_wrapper {
            height: 100%;
        }
    }
    .root_table__bottom > table > tbody > tr:first-child {display: none;}
    .footer_table_main {border-top: 1px solid rgba(150, 150, 150, 0.5);}
</style>

<div class="max_width">
    <i class="br25 mobile_hide"></i>
<?php
slot('title', __('Корзина'));
include_partial('main/breadcrumbs');
echo '<h1 class="content_title">' . __('Корзина') . '</h1>';
?>
</div>
<form method="post" class="basket_wrap">
    <div class="basket_refresh">
        <?php
        include_component('basket', 'basket');
        ?>
    </div>

    <div class="max_width">
        <?php
        $info = $sf_user->getBasketInfo();
            echo $order_form->renderGlobalErrors();
            echo $order_form->renderHiddenFields();

            ?>

        <div class="formsuccess" <?php echo ($info['offerCount'] > 0 ) ? '' : 'style="display: none"' ?> >
            <div class="basket_buyer">
                <?php
                echo '<h2>' . __('Контактные данные') . ':</h2>';
                echo '<div class="basket_buyer__item"><input size="27" maxlength="255" required="1" placeholder="Как к вам обращаться" onchange="form_validator.check($(this))" onkeyup="form_validator.check($(this))" data-check-rule="noempty" class="basket_buyer__inp_name validate_it" name="basket_order[username]" id="basket_order_username" type="text"></div>';
                echo '<div class="basket_buyer__item"><div class="phone_info"></div><input size="27" maxlength="24" required="1" placeholder="Номер телефона" onchange="form_validator.check($(this))" onkeyup="form_validator.check($(this))" data-check-rule="phone" class="basket_buyer__inp_name phone_mask validate_it" name="basket_order[phone]" id="basket_order_phone" type="text"></div>';
                echo '<div class="basket_buyer__item"><input size="27" maxlength="24" required="1" placeholder="Email" onchange="form_validator.check($(this))" onkeyup="form_validator.check($(this))" data-check-rule="email" class="basket_buyer__inp_name validate_it" name="basket_order[email]" id="basket_order_email" type="text"></div>';
                ?>
            </div>
            <div class="basket_get">
                <?php echo '<h2>' . __('Как получить товар') . '</h2>';?>
                <div class="basket_get__pickup">
                    <?php
                    echo '<label class="radio"><input type="radio" class="not_custom" name="basket_order[delivery_type]" value="Самовывоз" required="required" autocomplete="off">';
                    echo '<span class="radio_span"></span>';
                    echo '<span class="radio_text">' . __('Самовывоз') . '</span>';
                    echo $order_form['delivery_type']->renderError();
                    echo '</label>';
                    ?>
                    <div class="basket_get__desc">
                        <?php echo __('Вы можете забрать товар по адресу') . ':<br>' . csSettings::get('pickup_address');?>
                    </div>
                </div>
                <div class="basket_get__delivery">
                    <label class="radio">
                        <input class="not_custom" id="basket_order_delivery_type_delivery" type="radio" name="basket_order[delivery_type]" value="Доставка по адресу" required="required" autocomplete="off">
                        <span class="radio_span"></span>
                        <span class="radio_text"><?php echo __('Доставка');?></span>
                    </label>
                        <div class="basket_get__desc" style="margin-top: 10px">
                    <span class="basket_delivery_info">

                    </span>
                        <?php
                        echo $order_form['delivery_address']->render() . $order_form['delivery_address']->renderError();
                        ?>
                    </div>
                </div>
            </div>
            <div class="basket_advanced">
                <?php
                echo '<h2>' . __('Пожелание к заказу') . '</h2>';
                echo $order_form['comment']->render() . $order_form['comment']->renderError();
                ?>
            </div>
            <div class="basket_order">
                <?php
                echo '<h2>' . __('Общая сумма заказа') . '</h2>';
                echo '<div class="basket_order__price">' . $info['priceText'] . '&nbsp;<span class="rub">i</span></div>';
                echo '<input type="submit" class="btn green_b_btn order_btn btn_disabled" value="' . __('Оформить заказ') . '" disabled>';
                ?>
            </div>
            <script type="text/javascript">

                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;
                    }
                });

                $('.phone_mask').on('blur', function (e) {
                    if ($('.phone_mask').val().length == 18) {
                        $.ajax({
                            url: '/ru/basket/check_discount/',
                            dataType: 'json',
                            type: 'post',
                            data: {phone: $('.phone_mask').val()},
                            success: function (data) {
                                if (data.status == 'success' && data.active_discount > 0) {
                                    $('.basket_refresh').html(data.body);
                                    $($('.top_right')[1]).addClass('discount_basket');
                                    $('.phone_info').html('На этом номере обнаружена скидка ' + data.active_discount + '%')
                                } else if (data.status == 'success' && data.active_discount == 0) {
                                    $('.basket_refresh').html(data.body);
                                    $($('.top_right')[1]).removeClass('discount_basket');
                                    $('.phone_info').html('На этом номере скидка не обнаружена')
                                }
                            },
                            error: function (error) {
                               console.log(error);
                            }
                        })
                    }
                })

                $(document).ready(function () {
                    var delivery_info = $('.basket_total').data('delivery-info');
                    console.log(delivery_info);
                    if (delivery_info == '0') {
                        $('.basket_delivery_info').html('Свяжитесь с нами, для уточнения стоимости доставки +7 (635) 644-75-75.<br/>Или уточните у оператора, после оформления заказа');
                    }else {
                        $('.basket_delivery_info').html('Доставка бесплатная');
                    }
                });
                var delivery_type = undefined;

                $('.basket_order input[type="submit"]').on('click', function () {

                    var cl = $('.basket_order input[type="submit"]').attr('class');
                    console.log(cl);
                    console.log(cl.indexOf('btn_disabled'));
                    if (cl.indexOf('btn_disabled') > 0)
                    {
                        return false
                    }

                    delivery_adr = $('#basket_order_delivery_address').val();

                    if(typeof delivery_type == 'undefined') {
                        invalid($('.basket_get'));
                        return false;
                    }

                    if(delivery_type == 2 && delivery_adr.length < 3) {
                        invalid($('#basket_order_delivery_address'));
                        return false;
                    }
                    return true;
                });

                $('.basket_get input[type="radio"]').change(function (e) {
                    var deliveryAddress = $('#basket_order_delivery_address');
                    if(e.target.value == 'Самовывоз'){
                        delivery_type = 1;
                        deliveryAddress.removeAttr('required');
                    }else{
                        delivery_type = 2;
                        deliveryAddress.attr('required', 'required');
                    }
                });

                $('#basket_order_delivery_address').focus(function () {
                    $('#basket_order_delivery_type_delivery').click();
                });
                function invalid(el) {
                    $('html, body').animate({
                        scrollTop: el.offset().top - 160
                    }, 300);

                    setTimeout(function() {
                        el.addClass('jump');
                    }, 300);

                    setTimeout(function() {
                        el.removeClass('jump')
                    }, 800);
                }
                var count = <?php echo $info['offerCount'] ?>;
                $(document).ready(function () {
                    form_validator.check($('#basket_order_username'));
                    form_validator.check($('#basket_order_email'));
                    form_validator.check($('#basket_order_phone'));

                })
            </script>
        </div>
        <i class="br40"></i>
    </div>
</form>