{extends file="admin-layout.tpl"}

{block name="no-return-functions"}
    {$admin_current_location = 'order'}
{/block}

{block name="page-title"}{intl l='Edit an order'}{/block}

{block name="check-resource"}admin.order{/block}
{block name="check-access"}update{/block}

{block name="main-content"}

    {assign order_tab {$tab|default:$smarty.get.tab|default:"cart"}}

    <div class="orders edit-order">

        <div id="wrapper" class="container">

            <ul class="breadcrumb">
	            <li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
                <li><a href="{url path='/admin/orders'}">{intl l="Orders"}</a></li>
                <li>{intl l="Order details"}</a></li>
	        </ul>

            {hook name="order-edit.top" order_id=$order_id}

	        <div class="row">
            	<div class="col-md-12">

                    <div class="general-block-decorator">

                        {ifloop rel='the-order'}

                            {loop type="order" name="the-order" id=$order_id customer="*" with_prev_next_info="true" backend_context="1"}

                                {loop type="currency" name="order-currency" id=$CURRENCY}
                                    {$orderCurrency=$SYMBOL}
                                {/loop}

                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="title">{intl l='Order %ref - ID %id' id={$ID} ref={$REF}}</div>
                                        <p>{intl l='Order placed on %order_date at %order_time' order_date={format_date date=$CREATE_DATE output='date'} order_time={format_date date=$CREATE_DATE output='time'}}</p>
                                    </div>

                                    <div class="col-md-6 actions">
                                        <form class="form-horizontal" role="form" id="order-update-status-form" action="{url path="/admin/order/update/$ID/status"}" method="post">
                                            <input class="js-current-tab" type="hidden" name="tab" value="{$order_tab}">
                                            <div class="form-group">
                                                <label class="col-sm-6 control-label">{intl l="Order status:"}</label>
                                                <div class="col-sm-6">
                                                    <select class="js-update-order-status" name="status_id" data-toggle="selectpicker">
                                                    {assign "orderStatusId" $STATUS}
                                                    {loop type="order-status" name="all-status"}
                                                        <option {if $ID == $orderStatusId}selected="selected" {/if} value="{$ID}" data-content="<span class='label' style='background-color: {$COLOR};'>{$TITLE}</span>">{$TITLE}</option>
                                                    {/loop}
                                                    </select>
                                                </div>
                                            </div>
                                        </form>
                                    </div>

                                    <div class="col-md-2 actions">
                                        {if $HAS_PREVIOUS != 0}
                                        <a href="{url path="/admin/order/update/%previous" previous=$PREVIOUS}" class="btn btn-default" title="{intl l='Edit previous order'}"><span class="glyphicon glyphicon-arrow-left"></span></a>
                                        {else}
                                        <a href="#" disabled="disabled" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span></a>
                                        {/if}

                                        {if $HAS_NEXT != 0}
                                        <a href="{url path="/admin/order/update/%next" next=$NEXT}" class="btn btn-default" title="{intl l='Edit next order'}"><span class="glyphicon glyphicon-arrow-right"></span></a>
                                        {else}
                                        <a href="#" disabled="disabled" class="btn btn-default"><span class="glyphicon glyphicon-arrow-right"></span></a>
                                        {/if}
                                    </div>
                                </div>

                                {hookblock name="order.tab" id={$order_id} fields="id,title,href,content"}

                                    {capture "order_tab_tab"}
                                        {forhook rel="order.tab"}
                                        <li {if $order_tab == $id}class="active"{/if}>
                                            <a href="#{$id}"
                                               {if $href}data-href="{$href}"{/if}
                                               data-tab-name="{$id}"
                                               data-toggle="tab">{$title}
                                            </a>
                                        </li>
                                        {/forhook}
                                    {/capture}

                                    {capture "order_tab_content"}
                                        {forhook rel="order.tab"}
                                        <div class="tab-pane fade {if $order_tab == $id}active in{/if}" id="{$id}">
                                            {if $href}
                                            {* ajax *}
                                            <div class="text-center"><span class="loading">{intl l="Please wait, loading"}</span></div>
                                            {else}
                                            {$content nofilter}
                                            {/if}
                                        </div>
                                        {/forhook}
                                    {/capture}

                                {/hookblock}


                                <ul class="nav nav-tabs clearfix">
                                    <li {if $order_tab == 'cart'}class="active"{/if}><a href="#cart" data-tab-name="cart" data-toggle="tab"><span class="glyphicon glyphicon-shopping-cart"></span> {intl l="Ordered products"}</a></li>
                                    <li {if $order_tab == 'bill'}class="active"{/if}><a href="#bill" data-tab-name="bill" data-toggle="tab"><span class="glyphicon glyphicon-list-alt"></span> {intl l="Invoice and Delivery"}</a></li>
                                    {$smarty.capture.order_tab_tab nofilter}
                                    <li {if $order_tab == 'modules'}class="active"{/if}><a href="#modules" data-tab-name="modules" data-toggle="tab"><span class="glyphicon glyphicon-paperclip"></span> {intl l="Modules"}</a></li>
                                </ul>

                                <div class="tab-content">
                                    <div class="tab-pane fade {if $order_tab == 'cart'}active in{/if}" id="cart">

                                        {hook name="order-edit.cart-top" location="order-edit-cart-top" order_id=$order_id}

                                        <div class="table-responsive">
                                            {if $smarty.get.update_status_error_message}
                                                <div class='alert alert-danger'>
                                                    {$smarty.get.update_status_error_message}
                                                </div>
                                            {/if}

                                            {hook name="order-edit.before-order-product-list" location="before-order-product-list" order_id=$order_id}

                                            <table class="table table-striped table-condensed table-left-aligned table-order-items">
                                                <caption class="clearfix">
                                                   {intl l='Cart - Prices in %currency' currency={$orderCurrency}}
                                                </caption>
                                                <thead>
                                                    <tr>
                                                       <th>{intl l="Product"}</th>
                                                       {hook name="order-edit.order-product-table-header" location="order_edit_table_header" }
                                                       <th class="text-right">{intl l="Unit. price"}</th>
                                                       <th class="text-right">{intl l="Tax"}</th>
                                                       <th class="text-right">{intl l="Unit taxed price"}</th>
                                                       <th class="text-right">{intl l="Quantity"}</th>
                                                        <th class="text-right">{intl l="Untaxed total"}</th>
                                                        <th class="text-right">{intl l="Tax total"}</th>
                                                        <th class="text-right">{intl l="Taxed total"}</th>
                                                    </tr>
                                                </thead>

                                                <tbody>
                                                {$taxes = []}

                                                {loop type="order_product" name="order-products" order=$ID}
                                                    {if ! isset($taxes[{$TAX_RULE_TITLE}])}
                                                        {$taxes[{$TAX_RULE_TITLE}] = 0}
                                                    {/if}

                                                    {$taxes[{$TAX_RULE_TITLE}] = $taxes[{$TAX_RULE_TITLE}] + $REAL_PRICE_TAX * $QUANTITY}

                                                    {hook name="order-edit.before-order-product-row" location="before-order-product-row" order_id=$order_id order_product_id=$ID}
                                                    <tr>
                                                        <td>
                                                            {ifloop rel="my_product_loop"}
                                                                {loop type="product" name="my_product_loop" visible="*" ref=$REF return_url=false}
                                                                    <a href="{url path='/admin/products/update' product_id=$ID}">{$REF} - {$TITLE}</a>
                                                                    {if $VIRTUAL}<span class="glyphicon glyphicon-download text-muted" title="{intl l='Virtual product'}"></span>{/if}
                                                                {/loop}
                                                                <dl class="dl-horizontal">
                                                                    <dt>{intl l='Combination reference'}</dt>
                                                                    <dd>{$PRODUCT_SALE_ELEMENTS_REF}</dd>
                                                                </dl>
                                                            {/ifloop}
                                                            {elseloop rel="my_product_loop"}
                                                                {* The product doesn't exist anymore *}
                                                                {$TITLE}
                                                                {if $VIRTUAL}<span class="glyphicon glyphicon-download text-muted"></span>{/if}
                                                            {/elseloop}
                                                            {ifloop rel="combinations"}
                                                            <dl class="dl-horizontal">
                                                                {loop type="order_product_attribute_combination" name="combinations" order_product=$ID}
                                                                <dt>{$ATTRIBUTE_TITLE}</dt>
                                                                <dd>{$ATTRIBUTE_AVAILABILITY_TITLE}</dd>
                                                                {/loop}
                                                            </dl>
                                                            {/ifloop}
                                                            {hook name="order-edit.product-list" location="order-product-list" order_id=$order_id order_product_id=$ID}
                                                        </td>
                                                        {hook name="order-edit.order-product-table-row" location="order_edit_table_row" order_product_id={$ID} }
                                                        <td class="text-right">{format_money number=$REAL_PRICE currency_id=$CURRENCY}</td>
                                                        <td class="text-right">{format_money number=$REAL_PRICE_TAX currency_id=$CURRENCY}</td>
                                                        <td class="text-right">{format_money number=$REAL_TAXED_PRICE currency_id=$CURRENCY}</td>
                                                        <td class="text-right">{$QUANTITY}</td>
                                                        <td class="text-right">{format_money number=$REAL_TOTAL_PRICE currency_id=$CURRENCY}</td>
                                                        <td class="text-right">{format_money number=$REAL_TOTAL_PRICE_TAX currency_id=$CURRENCY}</td>
                                                        <td class="text-right">{format_money number=$REAL_TOTAL_TAXED_PRICE currency_id=$CURRENCY}</td>
                                                    </tr>
                                                    {hook name="order-edit.after-order-product-row" location="after-order-product-row" order_id=$order_id order_product_id=$ID}
                                                {/loop}
                                                </tbody>
                                            </table>
                                        </div>

                                        <div class="row">
                                            <div class="col-lg-5 col-lg-push-7 col-md-6 col-md-push-6">
                                                <div class="panel panel-default">
                                                    <table class="table table-condensed table-left-aligned">
                                                        <tbody class="table-condensed">
                                                            <tr>
                                                                <th>{intl l="Total without tax"}</th>
                                                                <td class="text-right">{format_money number=$TOTAL_ITEMS_AMOUNT currency_id=$CURRENCY}</td>
                                                            </tr>
                                                            <tr>
                                                                <th>{intl l="Total with taxes before discount"}</th>
                                                                <td class="text-right">{format_money number=$TOTAL_TAXED_AMOUNT-$POSTAGE+$DISCOUNT currency_id=$CURRENCY}</td>
                                                            </tr>
                                                            <tr>
                                                                <th>{intl l="Discount with taxes"}</th>
                                                                <td class="text-right">{if $DISCOUNT > 0}-{/if}{format_money number=$DISCOUNT currency_id=$CURRENCY}</td>
                                                            </tr>
                                                            <tr>
                                                                <th>{intl l="Total with taxes after discount"}</th>
                                                                <td class="text-right">{format_money number=$TOTAL_TAXED_AMOUNT-$POSTAGE currency_id=$CURRENCY}</td>
                                                            </tr>
                                                            <tr>
                                                                <th>{intl l="Shipping with taxes"}</th>
                                                                <td class="text-right">{format_money number=$POSTAGE currency_id=$CURRENCY}</td>
                                                            </tr>
                                                            <tr class="info">
                                                                <th>{intl l="ORDER TOTAL WITH TAXES"}</th>
                                                                <td class="text-right"><strong>{format_money number=$TOTAL_TAXED_AMOUNT currency_id=$CURRENCY}</strong></td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>

                                            <div class="col-lg-5 col-lg-pull-5 col-md-6 col-md-pull-6">
                                                <div class="panel panel-default">
                                                    <div class="panel-heading">
                                                        <h3 class="panel-title">{intl l='Taxes'}</h3>
                                                    </div>

                                                    <table class="table table-condensed table-left-aligned">
                                                        <tbody class="table-condensed">
                                                            <tr>
                                                                <th colspan="2">{intl l='Items taxes'}</th>
                                                            </tr>

                                                            {$tmp = 0}
                                                            {foreach $taxes as $name => $amount}
                                                                {if $amount != 0}
                                                                    <tr>
                                                                        <td>{$name}</td>
                                                                        <td class="text-right">
                                                                            {format_money number=$amount currency_id=$CURRENCY}
                                                                        </td>
                                                                    </tr>
                                                                    {$tmp = $tmp + $amount}
                                                                {/if}
                                                            {/foreach}
                                                            {if $tmp == 0}
                                                                <tr>
                                                                    <td>{intl l="None"}</td>
                                                                </tr>
                                                            {/if}

                                                            <tr>
                                                                <th colspan="2">{intl l='Shipping taxes'}</th>
                                                            </tr>
                                                            <tr>
                                                                {if $POSTAGE_TAX != 0}
                                                                    <td>{$POSTAGE_TAX_RULE_TITLE}</td>
                                                                    <td class="text-right">
                                                                        {format_money number=$POSTAGE_TAX currency_id=$CURRENCY}
                                                                    </td>
                                                                {else}
                                                                    <td>{intl l="None"}</td>
                                                                {/if}
                                                            </tr>


                                                            <tr>
                                                                <th colspan="2">{intl l='Discount tax'}</th>
                                                            </tr>
                                                            <tr>
                                                                {if $DISCOUNT_TAX != 0}
                                                                    <td>{intl l='Average rate'}</td>
                                                                    <td class="text-right">
                                                                        -{format_money number=$DISCOUNT_TAX currency_id=$CURRENCY}
                                                                    </td>
                                                                {else}
                                                                    <td>{intl l="None"}</td>
                                                                {/if}
                                                            </tr>

                                                            <tr class="info">
                                                                <th>{intl l="Taxes total"}</th>
                                                                <td class="text-right"><strong>{format_money number=$TOTAL_TAX currency_id=$CURRENCY}</strong></td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </div>

                                                <div class="panel panel-default">
                                                    <table class="table table-condensed table-left-aligned">
                                                        <tbody class="table-condensed">
                                                        <tr>
                                                            <th>{intl l="Coupon code"}</th>
                                                            <td class="text-right">
                                                                {loop type="order_coupon" name="couponcode" order=$ID}
                                                                    {$CODE}{if $LOOP_COUNT != $LOOP_TOTAL}, {/if}
                                                                {/loop}
                                                                {elseloop rel="couponcode"}
                                                                    {intl l='No coupon for this order'}
                                                                {/elseloop}
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <th>{intl l="Weight"}</th>
                                                            <td class="text-right">
                                                                {format_number number=$WEIGHT} {intl l="Kg"}
                                                            </td>
                                                        </tr>
                                                        </tbody>
                                                    </table>
                                                </div>

                                            </div>
                                        </div>

                                        {hook name="order-edit.after-order-product-list" location="after-order-product-list" order_id=$order_id}

                                        {loop type="order_address" name="order-invoice-address" id=$INVOICE_ADDRESS}
                                            {assign "orderInvoiceFirstName" $FIRSTNAME}
                                            {assign "orderInvoiceLastName" $LASTNAME}
                                            {assign "orderInvoiceCompany" $COMPANY}
                                        {/loop}

                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <h3 class="panel-title">
                                                    {intl l='Customer information'}
                                                </h3>
                                            </div>
                                            <table class="table table-condensed table-left-aligned">
                                                <tbody>
                                                <tr>
                                                    <th>{intl l="Customer"}</th>
                                                    <td><a href="{url path='/admin/customer/update' customer_id=$CUSTOMER}">{$orderInvoiceFirstName|ucwords} {$orderInvoiceLastName|upper}</a></td>
                                                </tr>
                                                <tr>
                                                    <th>{intl l="Email"}</th>
                                                    <td>{loop type="customer" name="customer" id=$CUSTOMER backend_context="on" current="false"}<a href="mailto:{$EMAIL}?Subject={intl l='Order %ref' ref={$REF}}">{$EMAIL}</a>{/loop}</td>
                                                </tr>
                                                {if $orderInvoiceCompany}
                                                <tr>
                                                    <th>{intl l="Company"}</th>
                                                    <td>{$orderInvoiceCompany}</td>
                                                </tr>
                                                {/if}
                                                </tbody>
                                            </table>

                                            {hook name="order-edit.customer-information-bottom" order_id=$order_id customer_id=$CUSTOMER}
                                        </div>

                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <h3 class="panel-title">
                                                    {intl l='Payment information'}
                                                </h3>
                                            </div>
                                            <table class="table table-condensed table-left-aligned">
                                                <tbody>
                                                <tr>
                                                    <th>{intl l="Payment module"}</th>
                                                    <td>{loop name="payment-module" type="module" id=$PAYMENT_MODULE order="code"}{$CODE} - {$TITLE}{/loop}</td>
                                                </tr>
                                                <tr>
                                                    <th>{intl l="Transaction reference"}</th>
                                                    <td>
                                                        {if $TRANSACTION_REF}
                                                            {$TRANSACTION_REF}
                                                        {else}
                                                            {intl l='No transaction for this order'}
                                                        {/if}
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>

                                            {hook name="order-edit.payment-module-bottom" order_id=$order_id module_id=$PAYMENT_MODULE}
                                        </div>

                                        {hook name="order-edit.cart-bottom" location="order-edit-cart-bottom" order_id=$order_id}
                                    </div> <!-- #cart -->

                                    <div class="tab-pane fade {if $order_tab == 'bill'}active in{/if} clearfix" id="bill">

                                        {hook name="order-edit.bill-top" location="order-edit-bill-top" order_id=$order_id}

                                        <div class="title title-without-tabs clearfix">
                                            {intl l='Delivery information'}
                                        </div>

                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="panel panel-default">
                                                    <div class="panel-heading">
                                                        <h3 class="panel-title">{intl l='Delivery module'}</h3>
                                                    </div>
                                                    <div class="panel-body">
                                                        {loop name="delivery-module" type="module" id=$DELIVERY_MODULE order="code"}
                                                            <p class="payment-module-title"><strong>{$CODE} - {$TITLE}</strong></p>

                                                        {if $DESCRIPTION}
                                                            <div class="payment-module-description">{$DESCRIPTION nofilter}</div>
                                                        {/if}
                                                        {/loop}
                                                    </div>

                                                    {hook name="order-edit.delivery-module-bottom" order_id=$order_id module_id=$DELIVERY_MODULE}
                                                </div>
                                            </div>

                                            <div class="col-md-6">
                                                <div class="panel panel-default">
                                                    <div class="panel-heading">
                                                        <h3 class="panel-title">{intl l='tracking reference'}</h3>
                                                    </div>
                                                    <div class="panel-body">
                                                        <form action="{url path="/admin/order/update/%id/delivery-ref" id=$ID}" method="post">
                                                            <input class="js-current-tab" type="hidden" name="tab" value="{$order_tab}">
                                                            <div class="input-group">
                                                                <input class="form-control" type="text" name="delivery_ref" value="{$DELIVERY_REF}" placeholder="{intl l="tracking reference"}"/>
                                                                <span class="input-group-btn">
                                                                    <button class="btn btn-success">{intl l="Save"} <span class="glyphicon glyphicon-ok"></span></button>
                                                                </span>
                                                            </div>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        {if $smarty.get.update_status_error_message}
                                            <div class='alert alert-danger'>
                                                {$smarty.get.update_status_error_message}
                                            </div>
                                        {/if}

                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="panel panel-default">
                                                    <div class="panel-heading">
                                                        <h3 class="panel-title">{intl l='Invoice informations'}</h3>
                                                    </div>
                                                    <div class="panel-body">
                                                        {include file='renderer/buttons.html' with_text=false buttons=[
                                                            [
                                                                'type' => 'view',
                                                                'title' => {intl l='View invoice PDF'},
                                                                'href' => {url path="/admin/order/pdf/invoice/%id/%browser" browser=1 id=$ID},
                                                                'attr' => ['target' => '_blank']
                                                            ],
                                                            [
                                                                'type' => 'download',
                                                                'title' => {intl l='Download invoice as PDF'},
                                                                'href' => {url path="/admin/order/pdf/invoice/%id/%browser" browser=0 id=$ID}
                                                            ],
                                                            [
                                                                'type' => 'print',
                                                                'class' => 'js-action-print',
                                                                'title' => {intl l='Print invoice'},
                                                                'href' => {url path="/admin/order/pdf/invoice/%id/%browser" browser=1 id=$ID},
                                                                'data' => [
                                                                    'type' => "invoice"
                                                                ]
                                                            ]
                                                        ]}

                                                        <div class="pull-right">
                                                            {include file='renderer/buttons.html' with_text=false buttons=[
                                                                [
                                                                'type' => 'edit',
                                                                'class' => 'js-update-order-address',
                                                                'title' => {intl l='Edit invoice address'},
                                                                'href' => "#edit_order_address_dialog",
                                                                'data' => [
                                                                'address-id' => $INVOICE_ADDRESS,
                                                                    'toggle' => 'modal'
                                                                    ]
                                                                ]
                                                            ]}
                                                        </div>
                                                    </div>

                                                    <table class="table table-condensed table-left-aligned">
                                                    <tbody>
                                                    <tr>
                                                        <th>{intl l="Invoice reference"}</th>
                                                        <td>{if $INVOICE_REF}
                                                                {$INVOICE_REF}
                                                            {else}
                                                                {intl l="None"}
                                                            {/if}
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>{intl l="Invoice date"}</th>
                                                        <td>{format_date date=$INVOICE_DATE output="date"}</td>
                                                    </tr>

                                                    {loop type="order_address" name="order-invoice-address" id=$INVOICE_ADDRESS}
                                                        <tr data-field-class="js-company">
                                                            <th>{intl l="Company"}</th>
                                                            <td>{$COMPANY}</td>
                                                        </tr>
                                                        <tr data-field-class="js-title" data-value="{$TITLE}">
                                                            <th>{intl l="Title"}</th>
                                                            <td>{loop type="title" name="order-invoice-address-title" id=$TITLE}{$LONG}{/loop}</td>
                                                        </tr>
                                                        <tr data-field-class="js-firstname">
                                                            <th>{intl l="Firstname"}</th>
                                                            <td>{$FIRSTNAME}</td>
                                                        </tr>
                                                        <tr data-field-class="js-lastname">
                                                            <th>{intl l="Lastname"}</th>
                                                            <td>{$LASTNAME}</td>
                                                        </tr>
                                                        <tr data-field-class="js-address1">
                                                            <th>{intl l="Street address"}</th>
                                                            <td>{$ADDRESS1}</td>
                                                        </tr>
                                                        <tr data-field-class="js-address2">
                                                            <th>{intl l="Additional address"}</th>
                                                            <td>{$ADDRESS2}</td>
                                                        </tr>
                                                        <tr data-field-class="js-address3">
                                                            <th>{intl l="Additional address"}</th>
                                                            <td>{$ADDRESS3}</td>
                                                        </tr>
                                                        <tr data-field-class="js-zipcode">
                                                            <th>{intl l="Zip code"}</th>
                                                            <td>{$ZIPCODE}</td>
                                                        </tr>
                                                        <tr data-field-class="js-city">
                                                            <th>{intl l="City"}</th>
                                                            <td>{$CITY}</td>
                                                        </tr>
                                                        <tr data-field-class="js-country" data-value="{$COUNTRY}">
                                                            <th>{intl l="Country"}</th>
                                                            <td>{loop type="country" name="order-invoice-address-country" id=$COUNTRY visible="*"}{$TITLE}{/loop}</td>
                                                        </tr>
                                                        <tr data-field-class="js-state" data-value="{$STATE}">
                                                            <th>{intl l="State"}</th>
                                                            <td>
                                                                {if $STATE}
                                                                    {loop type="state" name="order-invoice-address-state" id=$STATE visible="*"}{$TITLE}{/loop}
                                                                {else}
                                                                    -
                                                                {/if}
                                                            </td>
                                                        </tr>
                                                        <tr data-field-class="js-phone">
                                                            <th>{intl l="Phone"}</th>
                                                            <td>{$PHONE}</td>
                                                        </tr>
                                                        <tr data-field-class="js-cellphone">
                                                            <th>{intl l="Cellphone"}</th>
                                                            <td>{$CELLPHONE}</td>
                                                        </tr>
                                                        <tr>
                                                            <th>{intl l="Email"}</th>
                                                            <td>{loop type="customer" name="customer" id=$CUSTOMER backend_context="on" current="false"}<a href="mailto:{$EMAIL}?Subject={intl l='Order %ref' ref={$REF}}">{$EMAIL}</a>{/loop}</td>
                                                        </tr>
                                                    {/loop}
                                                    </tbody>
                                                </table>
                                                </div>
                                            </div>

                                            <div class="col-md-6">
                                                <div class="panel panel-default">
                                                    <div class="panel-heading">
                                                        <h3 class="panel-title">{intl l='Delivery address'}</h3>
                                                    </div>

                                                    <div class="panel-body">
                                                        {include file='renderer/buttons.html' with_text=false buttons=[
                                                            [
                                                                'type' => 'view',
                                                                'title' => {intl l='View purchase order as PDF'},
                                                                'href' => {url path="/admin/order/pdf/delivery/%id/%browser" browser=1 id=$ID},
                                                                'attr' => ['target' => '_blank']
                                                            ],
                                                            [
                                                                'type' => 'download',
                                                                'title' => {intl l='Download purchase order as PDF'},
                                                                'href' => {url path="/admin/order/pdf/delivery/%id/%browser" browser=0 id=$ID}
                                                            ],
                                                            [
                                                                'type' => 'print',
                                                                'class' => 'js-action-print',
                                                                'title' => {intl l='Print purchase order'},
                                                                'href' => {url path="/admin/order/pdf/delivery/%id/%browser" browser=1 id=$ID},
                                                                'data' => [
                                                                  'type' => "purchase"
                                                                ]
                                                            ]
                                                        ]}

                                                        <div class="pull-right">
                                                            {include file='renderer/buttons.html' with_text=false buttons=[
                                                                [
                                                                    'type' => 'edit',
                                                                    'class' => 'js-update-order-address',
                                                                    'title' => {intl l='Edit delivery address'},
                                                                    'href' => "#edit_order_address_dialog",
                                                                    'data' => [
                                                                    'address-id' => $DELIVERY_ADDRESS,
                                                                        'toggle' => 'modal'
                                                                    ]
                                                                ]
                                                            ]}
                                                        </div>
                                                    </div>

                                                    <table class="table table-condensed table-left-aligned">
                                                        <tbody>
                                                        {ifhook rel="order-edit.bill-delivery-address"}
                                                            {* delivery module can customize the delivery address *}
                                                            {hook name="order-edit.bill-delivery-address" module="$DELIVERY_MODULE" order_id={$order_id}}
                                                        {/ifhook}
                                                        {elsehook rel="order-edit.bill-delivery-address"}
                                                            {loop type="order_address" name="order-delivery-address" id=$DELIVERY_ADDRESS}
                                                                <tr>
                                                                    <td colspan="2">&nbsp;</td>
                                                                </tr>
                                                                <tr>
                                                                    <td colspan="2">&nbsp;</td>
                                                                </tr>
                                                                <tr data-field-class="js-company">
                                                                    <th>{intl l="Company"}</th>
                                                                    <td>{$COMPANY}</td>
                                                                </tr>
                                                                <tr data-field-class="js-title" data-value="{$TITLE}">
                                                                    <th>{intl l="Title"}</th>
                                                                    <td>{loop type="title" name="order-delivery-address-title" id=$TITLE}{$LONG}{/loop}</td>
                                                                </tr>
                                                                <tr data-field-class="js-firstname">
                                                                    <th>{intl l="Firstname"}</th>
                                                                    <td>{$FIRSTNAME}</td>
                                                                </tr>
                                                                <tr data-field-class="js-lastname">
                                                                    <th>{intl l="Lastname"}</th>
                                                                    <td>{$LASTNAME}</td>
                                                                </tr>
                                                                <tr data-field-class="js-address1">
                                                                    <th>{intl l="Street address"}</th>
                                                                    <td>{$ADDRESS1}</td>
                                                                </tr>
                                                                <tr data-field-class="js-address2">
                                                                    <th>{intl l="Additional address"}</th>
                                                                    <td>{$ADDRESS2}</td>
                                                                </tr>
                                                                <tr data-field-class="js-address3">
                                                                    <th>{intl l="Additional address"}</th>
                                                                    <td>{$ADDRESS3}</td>
                                                                </tr>
                                                                <tr data-field-class="js-zipcode">
                                                                    <th>{intl l="Zip code"}</th>
                                                                    <td>{$ZIPCODE}</td>
                                                                </tr>
                                                                <tr data-field-class="js-city">
                                                                    <th>{intl l="City"}</th>
                                                                    <td>{$CITY}</td>
                                                                </tr>
                                                                <tr data-field-class="js-country" data-value="{$COUNTRY}">
                                                                    <th>{intl l="Country"}</th>
                                                                    <td>{loop type="country" name="order-delivery-address-country" id=$COUNTRY visible="*"}{$TITLE}{/loop}</td>
                                                                </tr>
                                                                <tr data-field-class="js-state" data-value="{$STATE}">
                                                                    <th>{intl l="State"}</th>
                                                                    <td>
                                                                        {if $STATE}
                                                                            {loop type="state" name="order-delivery-address-state" id=$STATE visible="*"}{$TITLE}{/loop}
                                                                        {else}
                                                                            -
                                                                        {/if}
                                                                    </td>
                                                                </tr>
                                                                <tr data-field-class="js-phone">
                                                                    <th>{intl l="Phone"}</th>
                                                                    <td>{$PHONE}</td>
                                                                </tr>
                                                                <tr data-field-class="js-cellphone">
                                                                    <th>{intl l="Cellphone"}</th>
                                                                    <td>{$CELLPHONE}</td>
                                                                </tr>
                                                            {/loop}
                                                        {/elsehook}
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>

                                        {hook name="order-edit.bill-bottom" location="order-edit-bill-bottom" order_id=$order_id}

                                    </div> <!-- #bill -->

                                    {$smarty.capture.order_tab_content nofilter}

                                    <div class="tab-pane fade {if $order_tab == 'modules'}active in{/if} clearfix" id="modules">
                                        {* ugly fix : {hook name="order.tab-content"} *}
                                        {include file="includes/module-tab-content.html" hook="order.tab-content" location="order-edit" id=$order_id view="order-edit"}
                                    </div>
                                </div>
                            {/loop}

                        {/ifloop}
                        {elseloop rel="the-order"}

                            DOES NOT EXISTS

                        {/elseloop}

					</div>

            	</div>
            </div>

            {hook name="order-edit.bottom" order_id=$order_id}

        </div>
    </div>

	{* Update an Address *}

    {form name="thelia.order.update.address"}

        {* Capture the dialog body, to pass it to the generic dialog *}
        {capture "edit_order_address_dialog"}

            {form_hidden_fields}

            {form_field field='id'}
                <input type="hidden" id="{$label_attr.for}" name="{$name}" class="js-order-address-id" value="{$value}">
            {/form_field}

            {render_form_field field='company' extra_class="js-company"}

            {custom_render_form_field field='title'}
                <select {form_field_attributes field='title' extra_class="js-title"} >
                    {loop type="title" name="title1"}
                        <option value="{$ID}"{if $value == $ID} selected{/if}>{$LONG}</option>
                    {/loop}
                </select>
            {/custom_render_form_field}

            <div class="row">
                <div class="col-sm-6">{render_form_field field="firstname" extra_class="js-firstname"}</div>
                <div class="col-sm-6">{render_form_field field='lastname' extra_class="js-lastname"}</div>
            </div>

            {render_form_field field='address1' extra_class="js-address1"}
            {render_form_field field='address2' extra_class="js-address2" show_label=false}
            {render_form_field field='address3' extra_class="js-address3" show_label=false}

            <div class="row">
                <div class="col-md-4">{render_form_field field='zipcode' extra_class="js-zipcode"}</div>
                <div class="col-md-8">{render_form_field field='city' extra_class="js-city"}</div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    {custom_render_form_field field='country'}
                        <select {form_field_attributes field='country' extra_class="js-country"}>
                            {loop type="country" name="country.list" visible="*" order="alpha"}
                                <option value="{$ID}" {if {$value} == $ID}selected{/if}>{$TITLE}</option>
                            {/loop}
                        </select>
                        {$countryFieldId=$label_attr.for}
                    {/custom_render_form_field}
                </div>

                <div class="col-md-6 group-state">
                    {custom_render_form_field field='state'}
                        <select {form_field_attributes field='state' extra_class="js-state"}
                            data-thelia-state="1" data-thelia-country="#{$countryFieldId}" data-thelia-toggle=".group-state">
                            {loop type="state" name="state1" visible="*" order="alpha"}
                                <option value="{$ID}" {if {$value} == $ID}selected{/if} data-country="{$COUNTRY}">{$TITLE}</option>
                            {/loop}
                        </select>
                    {/custom_render_form_field}
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">{render_form_field field='phone' extra_class="js-phone"}</div>
                <div class="col-md-6">{render_form_field field='cellphone' extra_class="js-cellphone"}</div>
            </div>
        {/capture}

        {include
            file = "includes/generic-create-dialog.html"

            dialog_id    = "edit_order_address_dialog"
            dialog_title = {intl l="Edit order address"}
            dialog_body  = {$smarty.capture.edit_order_address_dialog nofilter}

            dialog_ok_label     = {intl l="Confirm changes"}
            dialog_cancel_label = {intl l="Cancel"}

            form_action        = {url path="/admin/order/update/%id/address" id=$order_id}
            form_enctype       = {form_enctype}
            form_error_message = $form_error_message
            form_id = 'updateOrderAddressForm'
        }

    {/form}

{/block}

{block name="javascript-initialization"}

    {javascripts file='assets/js/bootstrap-select/bootstrap-select.js'}
        <script src='{$asset_url}'></script>
    {/javascripts}

    {javascripts file='assets/js/thelia-states.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}

    <script type="text/javascript">

        $(function() {
            function print(url, number) {
                var iframeName = 'iframe-print-' + number;
                var iframe = document.createElement('iframe');
                iframe.src = url;
                iframe.id = iframeName;
                iframe.name = iframeName;
                iframe.style.display = "none";
                $('body').append(iframe);
                window.frames[iframeName].focus();
                window.frames[iframeName].print();
            }
            $('.js-action-print').on('click', function(e) {
                e.preventDefault();
                print($(this).attr('href'), this.dataset.type);
            });

            // Atomatic ajax tab load, if data-href is defined.
            $('.nav-tabs a[data-href]').on('shown.bs.tab', function (ev) {
                var $this = $(this);
                $($this.attr('href')).load($this.data('href'), function (ev) {
                    if ($this.data('callback')) {
                        eval($this.data('callback') + '();');
                    }
                });

            });

            $(".js-update-order-status").change(function (e) {
                e.preventDefault();
                $('#order-update-status-form').submit();
            });

            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                $(".js-current-tab").val($(e.target).data("tab-name"));
            });

            $(".js-update-order-address").click(function () {
                $(".js-order-address-id").val(
                        $(this).data("address-id")
                );

                $(this).parents('.panel').find('table tr').each(function (k, v) {
                    var $field = $(v),
                        $formField,
                        fieldValue;

                    if (!$field.data('field-class')) {
                        return true;
                    }
                    if ($field.data('value')) {
                        fieldValue = $field.data('value');
                    } else {
                        fieldValue = $field.children('td').text();
                    }

                    $formField = $('.' + $field.data('field-class'));

                    $formField.val(fieldValue);

                    if ($field.data('field-class') == 'js-country') {
                        $formField.trigger("change");
                    }
                });
            });
        });
    </script>

{/block}

{block name="javascript-last-call"}
    {hook name="order.edit-js" location="order-edit-js" order_id={$order_id} }
{/block}
