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

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

{% block meta_title %}{{ order.title }}{% endblock %}

{% block breadcrumbs %}
<ul>
    <li class="firstB"><a href="{{ '/'|alink }}">{{ 'Home'|trans }}</a></li>
    <li><a href="{{ 'order'|alink }}">{{ 'Orders'|trans }}</a></li>
    <li class="lastB">{{ order.title }}</li>
</ul>
{% 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">
    <div class="head">
        <h5 class="iFrames">{{ 'Order management'|trans }}</h5>
    </div>

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

                    <tr>
                        <td><label>{{ 'Client'|trans }}</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>{{ 'Title'|trans }}</label></td>
                        <td><a href="{{ 'product/manage'|alink }}/{{ order.product_id }}"><strong>{{ order.title }}</strong></a></td>
                    </tr>

                    <tr>
                        <td><label>{{ 'Payment amount'|trans }}</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>

                    {% if order.discount and order.discount > 0%}
                    <tr>
                        <td><label>{{ 'Order discount'|trans }}</label></td>
                        <td>-{{ mf.currency_format( order.discount, order.currency) }} </td>
                    </tr>

                    <tr>
                        <td><label>{{ 'Payment amount after discount'|trans }}</label></td>
                        <td>{{ mf.currency_format( order.total - order.discount, order.currency) }} </td>
                    </tr>
                    {% endif %}

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

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

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

                    <tr>
                        <td>
                            <label>{{ 'Renewal date'|trans }} {% 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>{{ 'Order notes'|trans }}</label></td>
                        <td>{{ order.notes|bbmd }}</td>
                    </tr>

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

                    {% if order.promo_id %}
                    <tr>
                        <td><label>{{ 'Order promo code'|trans }}</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>{{ 'Active support tickets'|trans }}</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="{{ 'api/admin/order/activate'|link({ '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' %}
                                    {% set params = admin.extension_config_get({"ext":"mod_order"}) %}
                                    <a href="{{ 'api/admin/order/renew'|link({ '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>

                                    {% if params.suspend_reason_list|trim == null %}
                                    <a href="{{ 'api/admin/order/suspend'|link({ 'id': order.id }) }}" title="" class="btn55 mr10 api-link" data-api-prompt-key="reason" data-api-prompt="1" data-api-prompt-text="{{ 'Reason of suspension'|trans }}" data-api-prompt-title="{{ 'Suspension reason'|trans }}" data-api-reload="Order suspended">
                                        <img src="images/icons/middlenav/stop.png" alt="">
                                        <span>Suspend</span>
                                    </a>

                                    {% else %}
                                    <div id="suspend_popup" style="position: fixed; z-index: 99999; padding: 5px; margin: 0px; min-width: 310px; max-width: 310px; top: 30%; left: 45%; display: none;">
                                        <h5 id="suspend_popup_title">{{ 'Suspension reason'|trans }}</h5>
                                        <div id="suspend_popup_content" class="confirm">
                                            <div id="suspend_popup_message">
                                                <div>{{ 'Reason of suspension'|trans }}
                                                    {% for reason in params.suspend_reason_list|trim|split("\r\n") %}
                                                    <div class="item">
                                                        <input type="radio" value="{{reason}}" name="reason"/> <label>{{reason}}</label>
                                                    </div>
                                                    {% endfor %}
                                                </div>
                                            </div>
                                            <div id="suspend_popup_panel">
                                                <input type="button" class="blueBtn" value="&nbsp;{{ 'Suspend'|trans }}&nbsp;" id="popup_ok" onclick="return susp.suspendOrder('/api/admin/order/suspend?id={{order.id}}', 'reason');"/>
                                                <input type="button" class="basicBtn" value="&nbsp;Cancel&nbsp;" id="popup_cancel" onclick="return susp.suspenderHide();"/>
                                            </div>
                                        </div>
                                    </div>
                                    <a href="#" title="" id="suspend_button" onclick="return susp.showSuspendPopup()" data-api-reload="Order suspended" class="btn55 mr10">
                                        <img src="images/icons/middlenav/stop.png" alt=""><span>Suspend</span>
                                    </a>
                                    {% endif %}
                                    <a href="{{ 'api/admin/order/cancel'|link({'id' : order.id}) }}" title="" class="btn55 mr10 api-link" data-api-prompt-key="reason" data-api-prompt="1" data-api-prompt-text="{{ 'Reason of cancelation'|trans }}" data-api-prompt-title="{{ 'Cancelation reason'|trans }}" data-api-reload="Order canceled">
                                        <img src="images/icons/middlenav/close.png" alt=""><span>Cancel</span>
                                    </a>
                                    {% endif %}
                                    
                                    {% if order.status == 'suspended' %}
                                    <a href="{{ 'api/admin/order/unsuspend'|link({'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="{{ 'api/admin/order/uncancel'|link({'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="{{ 'api/admin/order/delete'|link({'id' : order.id}) }}" title="" data-api-confirm="Are you sure?" data-api-redirect="{{ 'order'|alink }}" 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="{{ 'api/admin/invoice/renewal_invoice'|link({'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="{{ 'api/admin/order/update'|link }}" class="mainForm api-form" data-api-reload="1">
                <fieldset>

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

                    <div class="rowElem">
                        <label>{{ 'Changes status without performing action'|trans }}</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>{{ 'Invoice option'|trans }}</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>{{ 'Price'|trans }}</label>
                        <div class="formRight">
                            <input type="text" name="price" value="{{order.price}}" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{{ 'Reason'|trans }}</label>
                        <div class="formRight">
                            <input type="text" name="reason" value="{{order.reason}}"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{{ 'Period'|trans }}</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>{{ 'Expires at'|trans }}</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>{{ 'Created at'|trans }}</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>{{ 'Activated at'|trans }}</label>
                        <div class="formRight">
                            <input type="text" name="activated_at" value="{{order.activated_at|date('Y-m-d')}}" required="required" class="datepicker"/>
                        </div>
                        <div class="fix"></div>
                    </div>

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

                     <input type="submit" value="{{ 'Update'|trans }}" class="greyishBtn submitForm" />
                </fieldset>
                <input type="hidden" name="id" value="{{ order.id }}">
            </form>
            
            {#
            <form method="post" action="{{ 'api/admin/order/update'|link }}" class="mainForm api-form" data-api-reload="1">
                <fieldset>
                    <legend>Order promotion code</legend>
                    
                    <div class="rowElem noborder">
                        <label>{{ 'Promo code'|trans }}</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="{{ 'Update promo'|trans }}" class="greyishBtn submitForm" />
                </fieldset>
                <input type="hidden" name="id" value="{{ order.id }}">
            </form>
            #}
            {# order_actions #}
            
            {# if order.status == 'pending_setup' or order.status == 'failed_setup' %}
            <div class="help">
                <h2>Order parameters</h2>
            </div>

            <form method="post" action="{{ 'api/admin/order/update_config'|link }}" 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="{{ 'Update'|trans }}" class="greyishBtn submitForm" />
                    <input type="hidden" name="id" value="{{ order.id }}"/>
                </fieldset>
            </form>
            {% endif #}
        </div>

        <div class="tab_content nopadding" id="tab-config">
            <div class="help">
                <h2>{{ 'Order config management'|trans }}</h2>
                <h6>{{ 'Please be cautious and make sure you know what you are doing when editing order config! BoxBilling relies on these parameters and you might get unexpected results if you change some of them'|trans }}</h6>
            </div>
            <form method="post" action="{{ 'api/admin/order/update_config'|link }}" class="mainForm api-form" data-api-reload="1">
                <fieldset>
                    {% for key, config in order.config %}
                    <div class="rowElem noborder">
                        <label>{{ key }}</label>
                        <div class="formRight noborder">
                            {% if config is iterable %}
                                {% for key2, config2 in config %}
                                    <input type="text" name="config[{{key}}][{{key2}}]" value="{{ config2 }}" {% if config2 is null %} placeholder="null" {% endif %}/>
                                {% endfor %}
                            {% else %}
                                <input type="text" name="config[{{key}}]" value="{{ config }}" {% if config is null %} placeholder="null" {% endif %}/>
                            {% endif %}
                        </div>
                        <div class="fix"></div>
                    </div>
                    {% endfor %}

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

        <div class="tab_content nopadding" id="tab-addons">
            <div class="help">
                <h2>{{ 'Addons you have ordered with this service'|trans }}</h2>
            </div>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>{{ 'Product/Service'|trans }}</td>
                        <td>{{ 'Price'|trans }}</td>
                        <td>{{ 'Billing Cycle'|trans }}</td>
                        <td>{{ 'Next due date'|trans }}</td>
                        <td>{{ 'Status'|trans }}</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 guest.system_template_exists({"file":service_partial}) %}
                {% set service = admin.order_service({"id":order.id}) %}
                {% include service_partial with {'order' : order, 'service':service} %}
            {% elseif order.form_id and guest.extension_is_on({"mod":"formbuilder"}) %}
                {% include 'mod_formbuilder_manage.phtml' with order %}
            {% else %}
                {# trans 'Order form was not found' #}
            {% endif %}
        </div>

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

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td width="15%">{{ 'Amount'|trans }}</td>
                        <td width="30%">{{ 'Issued at'|trans }}</td>
                        <td width="30%">{{ 'Paid at'|trans }}</td>
                        <td width="15%">{{ 'Status'|trans }}</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">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-status">
            <div class="help">
                <h2>{{ 'Order status change history'|trans }}</h2>
            </div>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>{{ 'Status'|trans }}</td>
                        <td>{{ 'Note'|trans }}</td>
                        <td>{{ 'Date'|trans }}</td>
                        <td>{{ 'Actions'|trans }}</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'|alink }}" href="{{ 'api/admin/order/status_history_delete'|link({'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>{{ 'Support tickets regarding this order'|trans }}</h2>
            </div>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td width="60%">{{ 'Subject'|trans }}</td>
                        <td width="15%">{{ 'Help desk'|trans }}</td>
                        <td width="15%">{{ 'Status'|trans }}</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">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}

{% block js %}
<script type="text/javascript">
    var susp = {
        showSuspendPopup: function() {
            $('#suspend_popup').show();

            return false;
        },
        suspendOrder: function(url, name) {
            var p = {};
            var inps = document.getElementsByName(name);
            var value = '';

            $.each(inps, function(index, input) {
                if (input.checked) {
                    value = input.value;

                }
            });

            p[name] = value;
            bb.get(url, p, function(result) {
                return bb._afterComplete($('#suspend_button'), result);
            });

            $('#suspend_popup').hide();
            
            return false;
        },
        suspenderHide: function() {
            $('#suspend_popup').hide();
        }
    };
</script>
{% endblock %}
