{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% import "macro_functions.phtml" as mf %}
{% block meta_title %}{{ order.title }}{% endblock %}
{% set active_menu = 'order' %}

{% set service_partial = "mod_service" ~ order.service_type ~ "_manage.phtml" %}
{% if order.group_master == 1 %}
    {% set addons = admin.order_addons({"id":order.id}) %}
{% endif %}

{% block content %}

<div class="widget simpleTabs tabsRight nomargin">

    <div class="head">
        <h5 class="iFrames">{% trans 'Order management' %}</h5>
    </div>

    <ul class="tabs">
        <li><a href="#tab-info">{% trans 'Details' %}</a></li>
        <li><a href="#tab-manage">{% trans 'Edit order' %}</a></li>
        {% if admin.system_template_exists({"file":service_partial}) %}<li><a href="#tab-service">{% trans 'Service management' %}</a></li>{% endif %}
        <li><a href="#tab-invoices">{% trans 'Invoices' %}</a></li>
        {% if addons|length > 0 %}<li><a href="#tab-addons">{% trans 'Addons' %}</a></li>{% endif %}
        <li><a href="#tab-status">{% trans 'History' %}</a></li>
        <li><a href="#tab-support">{% trans 'Support' %}</a></li>
    </ul>

    <div class="tabs_container">
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-info">
            <div class="help">
                <h2>{{ order.title }}</h2>
            </div>

            <div class="block">
                <table class="tableStatic wide">
                    <tbody>
                    <tr class="noborder">
                        <td><label>{% trans 'Order' %}</label></td>
                        <td>#{{ order.id }} ({{ order.service_type }})</td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Client' %}</label></td>
                        <td><a href="{{ 'client/manage'|alink }}/{{order.client.id}}">{{ order.client.first_name }} {{ order.client.last_name }}</a></td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Title' %}</label></td>
                        <td><a href="{{ 'product/manage'|alink }}/{{ order.product_id }}"><strong>{{ order.title }}</strong></a></td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Payment amount' %}</label></td>
                        <td>{{ mf.currency_format( order.total, order.currency) }} {% if order.period %}{{mf.period_name(order.period)}}{% endif %} {% if order.quantity > 1 %}({{ order.quantity }} x {{ mf.currency_format( order.price, order.currency) }}){% endif %}</td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Order status' %}</label></td>
                        <td>{{mf.status_name(order.status)}}</td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Order created' %}</label></td>
                        <td>{{ order.created_at|date('l, d F Y') }}</td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Activated at' %}</label></td>
                        <td>{% if order.activated_at %}{{ order.activated_at|date('l, d F Y')}}{% else %}-{% endif %}</td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Renewal date' %} {% if order.expires_at %} in {{ order.expires_at|daysleft }} day(s) {% endif %}</label></td>
                        <td>{% if order.expires_at %}{{ order.expires_at|date('l, d F Y')}}{% else %}-{% endif %}</td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Order notes' %}</label></td>
                        <td>{{ order.notes|bbmd }}</td>
                    </tr>

                    <tr>
                        <td><label>{% trans 'Order group ID' %}</label></td>
                        <td>{{ order.group_id|default('-') }}</td>
                    </tr>

                    {% if order.promo_id %}
                    <tr>
                        <td><label>{% trans 'Order promo code' %}</label></td>
                        <td>
                            {% set promo = admin.product_promo_get({"id":order.promo_id}) %}
                            {{ promo.code }}
                        </td>
                    </tr>
                    {% endif %}
                    
                    {% if order.active_tickets > 0 %}
                    <tr>
                        <td><label>{% trans 'Active support tickets' %}</label></td>
                        <td>
                            <div class="num"><a href="{{ 'support'|alink }}?status=open&order_id={{ order.id }}" class="redNum">{{ order.active_tickets }}</a></div>
                        </td>
                    </tr>
                    {% endif %}
                    </tbody>
                    
                    <tfoot>
                        <tr>
                            <td colspan="2">
                                
                                <div class="aligncenter">
                                    {% set order_actions %}
                                    
                                    {% if order.status == 'pending_setup' or order.status == 'failed_setup' %}
                                    <a href="admin/order/activate?id={{order.id}}" title="" data-api-confirm="Are you sure?" class="btn55 mr10 api-link" data-api-reload="Order activated"><img src="images/icons/middlenav/play2.png" alt=""><span>Activate</span></a>
                                    {% endif %}
                                    
                                    {% if order.status == 'active' %}
                                    <a href="admin/order/renew?id={{order.id}}" title="" data-api-confirm="Are you sure?" class="btn55 mr10 api-link" data-api-reload="Order renewed"><img src="images/icons/middlenav/play2.png" alt=""><span>Renew</span></a>
                                    <a href="admin/order/suspend?id={{order.id}}" title="" class="btn55 mr10 api-link" data-api-prompt-key="reason" data-api-prompt="1" data-api-prompt-text="{% trans 'Reason of suspendation' %}" data-api-prompt-title="{% trans 'Suspendation reason' %}" data-api-reload="Order suspended"><img src="images/icons/middlenav/stop.png" alt=""><span>Suspend</span></a>
                                    <a href="admin/order/cancel?id={{order.id}}" title="" class="btn55 mr10 api-link" data-api-prompt-key="reason" data-api-prompt="1" data-api-prompt-text="{% trans 'Reason of cancelation' %}" data-api-prompt-title="{% trans 'Cancelation reason' %}" data-api-reload="Order canceled"><img src="images/icons/middlenav/close.png" alt=""><span>Cancel</span></a>
                                    {% endif %}
                                    
                                    {% if order.status == 'suspended' %}
                                    <a href="admin/order/unsuspend?id={{order.id}}" title="" data-api-confirm="Are you sure?" class="btn55 mr10 api-link" data-api-reload="Order activated"><img src="images/icons/middlenav/play2.png" alt=""><span>Unsuspend</span></a>
                                    {% endif %}
                                    
                                    {% if order.status == 'canceled' %}
                                    <a href="admin/order/uncancel?id={{order.id}}" title="" data-api-confirm="Are you sure?" class="btn55 mr10 api-link" data-api-reload="Order activated"><img src="images/icons/middlenav/play2.png" alt=""><span>Activate</span></a>
                                    {% endif %}
                                    
                                    <a href="admin/order/delete?id={{order.id}}" title="" data-api-confirm="Are you sure?" data-api-redirect="order" class="btn55 mr10 api-link"><img src="images/icons/middlenav/trash.png" alt=""><span>Delete</span></a>

                                    {% if not order.unpaid_invoice_id %}
                                    <a href="admin/invoice/renewal_invoice?id={{order.id}}" title="" data-api-confirm="Are you sure?" class="btn55 mr10 api-link" data-api-reload="1"><img src="images/icons/middlenav/create.png" alt=""><span>Issue invoice</span></a>
                                    {% endif %}
                                    {% endset %}
                                    
                                    {{ order_actions }}
                                </div>
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>

        </div>

        <div class="tab_content nopadding" id="tab-manage">
            <div class="help">
                <h2>Order management</h2>
            </div>
            <form method="post" action="admin/order/update" class="mainForm api-form" data-api-reload="1">
                <fieldset>

                    <div class="rowElem">
                        <label>{% trans 'Title' %}</label>
                        <div class="formRight noborder">
                            <input type="text" name="title" value="{{order.title}}"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Changes status without performing action' %}</label>
                        <div class="formRight noborder">
                            {{ mf.selectbox('status', admin.order_get_status_pairs, order.status, 0, 'Select status') }}
                        </div>
                        <div class="fix"></div>
                    </div>
                    
                    <div class="rowElem">
                        <label>{% trans 'Invoice option' %}</label>
                        <div class="formRight noborder">
                            {{ mf.selectbox('invoice_option', admin.order_get_invoice_options, order.invoice_option) }}
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Price' %}</label>
                        <div class="formRight">
                            <input type="text" name="price" value="{{order.price}}" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Period' %}</label>
                        <div class="formRight">
                            {{ mf.selectbox('period', guest.system_periods, order.period, 0, 'Not recurrent') }}
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Expires at' %}</label>
                        <div class="formRight">
                            <input type="text" name="expires_at" value="{% if order.expires_at %}{{order.expires_at|date('Y-m-d')}}{% endif %}" class="datepicker"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    
                    <div class="rowElem">
                        <label>{% trans 'Created at' %}</label>
                        <div class="formRight">
                            <input type="text" name="created_at" value="{{order.created_at|date('Y-m-d')}}" required="required" class="datepicker"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Notes' %}</label>
                        <div class="formRight">
                            <textarea name="notes" cols="5" rows="5">{{ order.notes }}</textarea>
                        </div>
                        <div class="fix"></div>
                    </div>

                     <input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
                </fieldset>
                <input type="hidden" name="id" value="{{ order.id }}">
            </form>
            
            {#
            <form method="post" action="admin/order/update" class="mainForm api-form" data-api-reload="1">
                <fieldset>
                    <legend>Order promotion code</legend>
                    
                    <div class="rowElem noborder">
                        <label>{% trans 'Promo code' %}</label>
                        <div class="formRight">
                            <input type="text" name="promo_id" value="{{order.promo_id}}" required="required" />
                        </div>
                        <div class="fix"></div>
                    </div>

                    <input type="submit" value="{% trans 'Update promo' %}" class="greyishBtn submitForm" />
                </fieldset>
                <input type="hidden" name="id" value="{{ order.id }}">
            </form>
            #}
            {# order_actions #}
            
            {# if order.status == 'pending_setup' %}
            <div class="help">
                <h2>Order parameters</h2>
            </div>

            <form method="post" action="admin/order/update" class="mainForm save api-form" data-api-msg="Order config updated">
                <fieldset>

                    {% for name, value in order.config %}
                    <div class="rowElem">
                        <label class="topLabel">{{ name }}:</label>
                            <div class="formBottom">
                                <textarea rows="2" cols="" name="config[{{ name }}]">{{ value }}</textarea>
                            </div>
                        <div class="fix"></div>
                    </div>
                    {% endfor %}

                    <input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
                    <input type="hidden" name="id" value="{{ order.id }}"/>
                </fieldset>
            </form>
            {% endif #}
        </div>

        <div class="tab_content nopadding" id="tab-addons">
            <div class="help">
                <h2>{% trans 'Addons you have ordered with this service' %}</h2>
            </div>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>{% trans 'Product/Service' %}</td>
                        <td>{% trans 'Price' %}</td>
                        <td>{% trans 'Billing Cycle' %}</td>
                        <td>{% trans 'Next due date' %}</td>
                        <td>{% trans 'Status' %}</td>
                        <td>&nbsp</td>
                    </tr>
                </thead>

                <tbody>
                {% for i, addon in addons %}
                <tr>
                    <td>{{addon.title}}</td>
                    <td>{{ mf.currency_format( addon.total, addon.currency) }}</td>
                    <td>{{ mf.period_name(addon.period) }}</td>
                    <td>{% if addon.expires_at %}{{addon.expires_at|date('l, d F Y')}}{% else %}-{% endif %}</td>
                    <td>{{ mf.status_name(addon.status) }}</td>
                    <td class="actions"><a class="bb-button btn14" href="{{ '/order/manage'|alink }}/{{addon.id}}"><img src="images/icons/dark/pencil.png" alt=""></a></td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-service">
            {% if admin.system_template_exists({"file":service_partial}) %}
                {% set service = admin.order_service({"id":order.id}) %}
                {% include service_partial with {'order': order, 'service': service} %}
            {% endif %}
        </div>

        <div class="tab_content nopadding" id="tab-invoices">
            <div class="help">
                <h2>{% trans 'Order invoices' %}</h2>
            </div>

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td width="15%">{% trans 'Amount' %}</td>
                        <td width="30%">{% trans 'Issued at' %}</td>
                        <td width="30%">{% trans 'Paid at' %}</td>
                        <td width="15%">{% trans 'Status' %}</td>
                        <td>&nbsp;</td>
                    </tr>
                </thead>
                
                <tbody>
                    {% set invoices = admin.invoice_get_list({"per_page":50, "order_id":order.id}) %}
                    {% for invoice in invoices.list %}
                    <tr>
                        <td>{{invoice.id}}</td>
                        <td>{{ mf.currency_format( invoice.total, invoice.currency, 1) }}</td>
                        <td>{{ invoice.created_at|date('Y-m-d') }}</td>
                        <td>{% if invoice.paid_at %}{{ invoice.paid_at|date('Y-m-d') }}{% else %}-{% endif %}</td>
                        <td>{{ mf.status_name(invoice.status) }}</td>
                        <td class="actions"><a class="bb-button btn14" href="{{ '/invoice/manage'|alink }}/{{invoice.id}}"><img src="images/icons/dark/pencil.png" alt=""></a></td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="5">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-status">
            <div class="help">
                <h2>{% trans 'Order status change history' %}</h2>
            </div>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>{% trans 'Status' %}</td>
                        <td>{% trans 'Note' %}</td>
                        <td>{% trans 'Date' %}</td>
                        <td>{% trans 'Actions' %}</td>
                    </tr>
                </thead>
                <tbody>
                    {% set statuses = admin.order_status_history_get_list({"per_page":50, "id":order.id}) %}
                    {% for i,sh in statuses.list %}
                    <tr>
                        <td>{{ mf.status_name(sh.status) }}</td>
                        <td><div style="overflow: auto; width: 470px; max-height: 50px;">{{ sh.notes }}</div></td>
                        <td>{{ sh.created_at|date('Y-m-d H:i') }}</td>
                        <td>
                            <a class="bb-button btn14 bb-rm-tr api-link" data-api-confirm="Are you sure?" data-api-redirect="support" href="admin/order/status_history_delete?id={{sh.id}}"><img src="images/icons/dark/trash.png" alt=""></a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-support">
            <div class="help">
                <h2>{% trans 'Support tickets regarding this order' %}</h2>
            </div>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td width="60%">{% trans 'Subject' %}</td>
                        <td width="15%">{% trans 'Help desk' %}</td>
                        <td width="15%">{% trans 'Status' %}</td>
                        <td>&nbsp;</td>
                    </tr>
                </thead>

                <tbody>
                    {% set tickets = admin.support_ticket_get_list({"per_page":"20", "order_id":order.id}) %}
                    {% for ticket in tickets.list %}
                    <tr>
                        <td>{{ ticket.id }}</td>
                        <td>{{ ticket.subject|truncate(30) }}</td>
                        <td>{{ ticket.helpdesk.name }}</td>
                        <td>{{ mf.status_name(ticket.status) }}</td>
                        <td class="actions"><a class="bb-button btn14" href="{{ '/support/ticket'|alink }}/{{ticket.id}}"><img src="images/icons/dark/pencil.png" alt=""></a></td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="5">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

</div>
{% endblock %}