{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}

{% import "macro_functions.phtml" as mf %}

{% block meta_title %}{{ 'Order confirm'|trans }}{% endblock %}

{% block content_before %}
    {% include "partial_steps.phtml" with { 'selected': 3 } %}
{% endblock %}

{% set cart = guest.cart_get %}

{% block content %}
{% if cart.items|length > 0 %}
<div id="shopping-cart">
    <div class="h-block">
        <div class="h-block-header">
            <div class="icon"><span class="number">3</span></div>
            <h2>{{ 'Order confirmation'|trans }}</h2>
            <p>{{ 'These are the products you have selected'|trans }}</p>
        </div>
        <div class="block">
            <table class="with-border">
                <thead>
                    <tr>
                        <th>{{ 'Product'|trans }}</th>
                        <th>{{ 'Period'|trans }}</th>
                        <th class="currency">{{ 'Price'|trans }}</th>
                    </tr>
                 </thead>

                 <tbody>
                    {% for i, item in cart.items %}
                    <tr class="{{ cycle(['odd', 'even'],i) }}">
                        <td>
                            {{ item.title }}
                            {% if item.quantity > 1 %}
                            x {{ item.quantity }}
                            {% endif %}
                        </td>
                        <td>{{ item.period | period_title }}</td>
                        <td class="currency">
                            {% if item.discount_price %}
                                <del>{{ (item.total)  | money_convert }}</del>
                                {{ (item.total - item.discount_price) | money_convert }}
                            {% else %}
                                {{ (item.total) | money_convert }}
                            {% endif %}
                        </td>
                    </tr>
                    {% if item.setup_price != 0 %}
                    <tr>
                        <td>{{ item.title }} {{ 'setup'|trans }}</td>
                        <td>-</td>
                        <td class="currency">
                            {% if item.discount_setup %}
                                <del>{{ item.setup_price | money_convert }}</del>
                                {{ (item.setup_price - item.discount_setup) | money_convert }}
                            {% else %}
                                {{ item.setup_price | money_convert }}</td>
                            {% endif %}
                    </tr>
                    {% endif %}
                    {% endfor %}
                 </tbody>
            </table>
            
            {% if client %}
            <div class="grid_5 suffix_2 alpha">
                <div class="box">
                    <div class="block">
                        <form action="{{ 'api/guest/cart/apply_promo'|link }}" method="post" class="api-form" data-api-reload="1">
                            <fieldset>
                                <input type="text" name="promocode" value="{{ request.promocode|default(cart.promocode) }}" required="required" placeholder="{{ 'Have a promo code?'|trans }}">
                                {% if cart.promocode %}
                                <a href="{{ 'api/guest/cart/remove_promo'|link }}" class="bb-button api-link" data-api-reload="1">{{ 'Reset'|trans }}</a>
                                {% else %}
                                <input class="bb-button bb-button-submit" type="submit" value="{{ 'Apply'|trans }}">
                                {% endif %}
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
            <div class="grid_5 omega">
                <div class="box">
                    <div class="block pricing">
                        {% set tax_amount = 0 %}
                        {% if client.client_is_taxable %}
                            {% set tax_rate = client.invoice_get_tax_rate %}
                            {% set tax_amount = cart.total * tax_rate / 100 %}

                            <p>{{ 'VAT'|trans }} ({{ tax_rate }}%) :</p>

                            <div class="currency">{{ tax_amount | money_convert }}</div>
                        {% endif %}
                        <p>{{ 'Total:'|trans }}</p>
                        <div class="currency">
							{{(cart.total + tax_amount) | money_convert }}
	                    </div>
                        <form method="post" action="{{ 'api/client/cart/checkout'|link }}" class="api-form" data-api-jsonp="onAfterCartCheckout">
                        {% set enough_in_balance = client.client_balance_get_total >= cart.total %}
                        {% if cart.total and not enough_in_balance  %}
                        <div style="text-align: left;">
                        {% for gtw in guest.invoice_gateways %}
                            {% if cart.currency.code in gtw.accepted_currencies %}
                            <label><input type="radio" name="gateway_id" value="{{gtw.id}}" {{loop.first ? 'checked' : ''}}/>
                                {{ 'Pay by'|trans }} {{ gtw.title }}
                            </label>
                            <div class="clear"></div>
                            {% endif %}
                        {% endfor %}
                        </div>
                        {% endif %}
                        <br/>
                        <input type="checkbox" id="tos" name="tos" required>
                        <label for="tos">I agree to the <a href="{{ '/tos'|link }}" target="_blank">{{ 'Terms and Conditions'|trans }}</a></label>
                        <button class="bb-button bb-button-submit bb-button-big" type="submit">{{ 'Checkout'|trans }}</button>
                        {% if enough_in_balance %}
                        <p>{{ 'Total amount will be deducted from account balance'|trans }}</p>
                        {% endif %}
                        </form>
                    </div>
                </div>
            </div>
            {% else %}

            <div class="block">
                <div class="splitter">
                <div class="grid_6 alpha">
                    <div class="box">
                        <div class="block">
                            <h2>{{ 'Sign in'|trans }}</h2>
                            <form action="{{ 'api/guest/client/login'|link }}" method="post" class="api-form" data-api-reload="1">
                                <fieldset>
                                    <legend>{{ 'Login to client area'|trans }}</legend>
                                    <p>
                                        <label>{{ 'Email Address'|trans }}: </label>
                                        <input type="email" name="email" value="{{ request.email }}" required="required">
                                    </p>
                                    <p>
                                        <label>{{ 'Password'|trans }}: </label>
                                        <input type="password" name="password" value="" required="required">
                                    </p>
                                    <input class="bb-button bb-button-submit" type="submit" value="{{ 'Login'|trans }}">
                                    <a class="bb-button" href="{{ 'reset-password'|link }}">{{ 'Reset password'|trans }}</a>
                                </fieldset>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="grid_6 omega">

                    {% if not request.register %}
                    <div class="box">
                        <h2>{{ 'Do not have an account yet?'|trans }}</h2>
                        <div class="block">
                            <a class="bb-button bb-button-red bb-button-big" href="#" onclick="$(this).parents('.box').hide(); $('#register-new-account').fadeIn(); return false;" style="margin: 30px 0 0 50px;">{{ 'Register'|trans }}</a>
                        </div>
                        {% if guest.extension_is_on({"mod":"oauth"}) %}
                            {% include "mod_oauth_buttons.phtml" with {"redirect_to":"cart"}%}
                        {% endif %}
                    </div>
                    {% endif %}

                    <div class="box" id="register-new-account" {% if not request.register %}style="display: none;"{% endif %} >
                            <h2>{{ 'Enter your details below'|trans }}</h2>
                            <div class="block">
                                {% set r = guest.client_required %}
                                <form action="{{ 'api/guest/client/create'|link }}" method="post" class="api-form" data-api-reload="1">
                                <fieldset>
                                        <legend>{{ 'Create new profile'|trans }}</legend>
                                        <p>
                                            <label>{{ 'Email Address'|trans }}: </label>
                                            <input type="email" name="email" value="{{ request.email }}" required="required" id="reg-email">
                                        </p>
                                        <p>
                                            <label>{{ 'First Name'|trans }}: </label>
                                            <input type="text" name="first_name" value="{{ request.first_name }}" required="required">
                                        </p>

                                        {% if 'last_name' in r %}
                                        <p>
                                            <label>{{ 'Last Name'|trans }}: </label>
                                            <input type="text" name="last_name" value="{{ request.last_name }}" required="required">
                                        </p>
                                        {% endif %}

                                        {% if 'company' in r %}
                                        <p>
                                            <label>{{ 'Company'|trans }}: </label>
                                            <input type="text" name="company" value="{{ request.company }}" required="required">
                                        </p>
                                        {% endif %}

                                        {% if 'birthday' in r %}
                                        <p>
                                            <label>{{ 'Birthday'|trans }}: </label>
                                            <input type="text" name="birthday" value="" required="required">
                                        </p>
                                        {% endif %}

                                        {% if 'gender' in r %}
                                        <p>
                                            <label>{{ 'You are'|trans }}: </label>
                                            <select name="gender" required="required">
                                                <option value="male">Male</option>
                                                <option value="female">Female</option>
                                            </select>
                                        </p>
                                        {% endif %}

                                        {% if 'address_1' in r %}
                                        <p>
                                            <label>{{ 'Address'|trans }}: </label>
                                            <input type="text" name="address_1" value="{{ request.address_1 }}" required="required">
                                        </p>
                                        {% endif %}

                                        {% if 'address_2' in r %}
                                        <p>
                                            <label>{{ 'Address 2'|trans }}: </label>
                                            <input type="text" name="address_2" value="{{ request.address_2 }}" required="required">
                                        </p>
                                        {% endif %}

                                        {% if 'city' in r %}
                                        <p>
                                            <label>{{ 'City'|trans }}: </label>
                                            <input type="text" name="city" value="{{ request.city }}" required="required">
                                        </p>
                                        {% endif %}

                                        {% if 'country' in r %}
                                        <p>
                                            <label>{{ 'Country'|trans }}: </label>
                                            {{ mf.selectbox('country', guest.system_countries, request.country, 0, 'Select country') }}
                                        </p>
                                        {% endif %}

                                        {% if 'state' in r %}
                                        <p>
                                            <label>{{ 'State'|trans }}: </label>
                                            {# mf.selectbox('state', guest.system_states, request.state, 0, 'Select state') #}
                                            <input type="text" name="state" value="{{ request.state }}" />
                                        </p>
                                        {% endif %}

                                        {% if 'postcode' in r %}
                                        <p>
                                            <label>{{ 'Zip/Postal Code'|trans }}: </label>
                                            <input type="text" name="postcode" value="{{ request.postcode }}" required="required">
                                        </p>
                                        {% endif %}

                                        {% if 'phone' in r %}
                                        <p>
                                            <label>{{ 'Phone'|trans }}: </label>
                                            <br/>
                                            <input type="text" name="phone_cc" value="{{ request.phone_cc }}" required="required" style="width: 30px; ">
                                            <input type="text" name="phone" value="{{ request.phone }}" required="required"style="width: 230px; ">
                                        </p>
                                        {% endif %}

                                        {% set custom_fields = guest.client_custom_fields %}
                                        {% for field_name, field in custom_fields %}
                                        {% if field.active %}
                                        <div class="control-group">
                                            <label class="control-label" for="{{ field_name }}">{% if field.title is not empty %}{{ field.title }}{% else %} {{ field_name | capitalize }} {% endif %}</label>
                                            <div class="controls">
                                                <input type="text" name="{{ field_name }}" id="{{ field_name }}" value="{{ attribute(request, field_name) }}" {% if field.required %}required="required"{% endif %}>
                                                <p class="help-block"></p>
                                            </div>
                                        </div>
                                        {% endif %}
                                        {% endfor %}

                                        <p>
                                            <label>{{ 'Password'|trans }}: </label>
                                            <input type="password" name="password" value="" required="required" id="reg-password">
                                        </p>

                                        <p>
                                            <label>{{ 'Password confirm'|trans }}: </label>
                                            <input type="password" name="password_confirm" value="" required="required">
                                        </p>

                                        {{ mf.recaptcha }}

                                        <input class="bb-button bb-button-red" type="submit" value="{{ 'Register'|trans }}">
                                        <input type="hidden" name="auto_login" value="1">
                                    </fieldset>
                                </form>
                                </div>
                            </div>
                </div>
                <div class="clear"></div>
                </div>
            </div>
            
            {% endif %}
            <div class="clear"></div>
        </div>
    </div>
</div>

{% else %}

<div class="h-block">
    <div class="h-block-header">
        <div class="icon"><span class="number">3</span></div>
        <h2>{{ 'You have not selected any product'|trans }}</h2>
    </div>
    <div class="block">

        <div class="box">
            <a class="bb-button bb-button-red" href="{{ 'order'|link }}">{{ 'Order new service'|trans }}</a>
        </div>
        
    </div>
</div>

{% endif %}
{% endblock %}

{% block sidebar2 %}
    {% include 'partial_currency.phtml' %}
{% endblock %}

{% block js %}
<script type="text/javascript">
    function onAfterCartCheckout(result) {
        if (result.invoice_hash) {
            bb.redirect("{{ 'invoice'|link}}" + '/' + result.invoice_hash + '&auto_redirect=1');
        } else {
            bb.redirect("{{ 'order/service/manage'|link }}" + '/' + result.order_id );
        }
    }
</script>
{% endblock %}
