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

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

{block name="page-title"}{intl l='Orders'}{/block}

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

{block name="main-content"}
    {assign order_page {$smarty.get.page|default:1}}
    {assign view_status {$smarty.get.status}}
    {assign status_filter {$smarty.get.status|assertType:'IntListType'}}
<div class="orders">

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

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

        {hook name="orders.top" location="orders_top" }

        <div class="row">
            <div class="col-md-12">
                <div class="general-block-decorator">
                	<div class="table-responsive">
	                    <table class="table table-striped table-condensed table-left-aligned">
	                        <caption class="clearfix">
	                           {intl l='Orders'}
	                        </caption>

                            {ifloop rel="order-list"}

	                        <thead>
		                        <tr>
                                    <th class="object-title">
                                        {admin_sortable_header
                                        current_order=$orders_order
                                        order='id'
                                        reverse_order='id-reverse'
                                        path={url path='/admin/orders'}
                                        request_parameter_name='orders_order'
                                        label="{intl l='ID'}"
                                    } </th>

                                    <th class="object-title">
                                    {admin_sortable_header
                                        current_order=$orders_order
                                        order='reference'
                                        reverse_order='reference-reverse'
                                        path={url path='/admin/orders'}
                                        request_parameter_name='orders_order'
                                        label="{intl l='Reference'}"
                                    } </th>

                                    <th class="object-title">
                                    {admin_sortable_header
                                        current_order=$orders_order
                                        order='create-date'
                                        reverse_order='create-date-reverse'
                                        path={url path='/admin/orders'}
                                        request_parameter_name='orders_order'
                                        label="{intl l='Date & Hour'}"
                                    } </th>

                                    <th class="object-title">
                                    {admin_sortable_header
                                        current_order=$orders_order
                                        order='company'
                                        reverse_order='company-reverse'
                                        path={url path='/admin/orders'}
                                        request_parameter_name='orders_order'
                                        label="{intl l='Company'}"
                                    } </th>

                                    <th class="object-title">
                                    {admin_sortable_header
                                        current_order=$orders_order
                                        order='customer-name'
                                        reverse_order='customer-name-reverse'
                                        path={url path='/admin/orders'}
                                        request_parameter_name='orders_order'
                                        label="{intl l='Cutomer Name'}"
                                     } </th>

                                    <th class="object-title text-right">{intl l='Amount'}</th>

                                    <th class="object-title text-center">
                                    {admin_sortable_header
                                        current_order=$orders_order
                                        order='status'
                                        reverse_order='status-reverse'
                                        path={url path='/admin/orders'}
                                        request_parameter_name='orders_order'
                                        label="{intl l='Status'}"
                                    } </th>

	                               {hook name="orders.table-header" location="orders_table_header" }

	                               <th class="actions">{intl l="Actions"}</th>
		                        </tr>
	                        </thead>

                            <tbody>

                                {loop type="order" name="order-list" customer="*" order=$orders_order backend_context="1" page={$order_page} limit={config key="number_default_results_per_page.order_list" default=20} status=$status_filter|default:'*' search_term=$search_term search_in="$search_in"}

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

                                    {loop type="order-status" name="order-status" id=$STATUS}
                                        {assign "orderStatus" $TITLE}
                                        {assign "orderStatusCodeColor" $COLOR}
                                    {/loop}

                                    <tr>
                                        <td><a href="{url path="/admin/order/update/%id" id=$ID}">{$ID}</a></td>
                                       	<td><a href="{url path="/admin/order/update/%id" id=$ID}">{$REF}</a></td>
                                       	<td>{format_date date=$CREATE_DATE}</td>
                                       	<td>{$orderInvoiceCompany}</td>
                                       	<td><a href="{url path='/admin/customer/update' customer_id=$CUSTOMER}">{$orderInvoiceFirstName|ucwords} {$orderInvoiceLastName|upper}</a></td>
                                       	<td class="text-right">{format_money number=$TOTAL_TAXED_AMOUNT currency_id=$CURRENCY}</td>
                                        <td class="text-center"><span class="label" style="background-color: {$orderStatusCodeColor};">{$orderStatus}</span></td>
                                        {* *}
                                        {hook name="orders.table-row" location="orders_table_row" order_id={$ID} }

                                        <td class="actions">
                                            {$buttons = [
                                                [
                                                    'text' => {intl l='Details'},
                                                    'type' => 'edit',
                                                    'title' => {intl l='View details and edit this order'},
                                                    'href' => {url path="/admin/order/update/%id" id=$ID},
                                                    'auth' => ['resource' => 'admin.order']
                                                ]
                                            ]}

                                            {if $STATUS !== 5}
                                            {$buttons[] = [
                                                    'text' => {intl l='Cancel'},
                                                    'type' => 'delete',
                                                    'class' => 'order-cancel',
                                                    'title' => {intl l='Cancel this order'},
                                                    'href' => '#cancel_order_dialog',
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ],
                                                    'auth' => ['resource' => 'admin.order']
                                            ]}
                                            {/if}

                                            {include file='renderer/buttons.html' buttons=$buttons}
		                                </td>
		                            </tr>

                                {/loop}

	                        </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="100">
                                        {include
                                            file = "includes/pagination.html"

                                            loop_ref       = "order-list"
                                            max_page_count = 10
                                            page_url       = {url path="/admin/orders" status=$status_filter orders_order=$orders_order search_term=$search_term search_in="$search_in"}
                                        }

                                    </td>
                                </tr>
                            </tfoot>
                            {/ifloop}

                            {elseloop rel="order-list"}
                                <tr>
                                    <td colspan="8">
                                        <div class="alert alert-info">
                                            {intl l='You have no orders for the moment.'}
                                        </div>
                                    </td>
                                </tr>
                            {/elseloop}
	                    </table>
	                </div>
                </div>
            </div>
        </div>

        {hook name="orders.bottom" location="orders_bottom" }

    </div>
</div>

{* Cancel order confirmation dialog *}

{capture "cancel_order_dialog"}
    <input type="hidden" name="order_page" value="{$order_page}">
    <input type="hidden" name="status" value="{$view_status}">

    <input type="hidden" name="order_id" id="cancel_order_id" />
    <input type="hidden" name="status_id" value="5" />
{/capture}

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

    dialog_id       = "cancel_order_dialog"
    dialog_title    = {intl l="Delete an order"}
    dialog_message  = {intl l="Do you really want to cancel this order ?"}

    form_action         = {token_url path='/admin/order/update/status'}
    form_content        = {$smarty.capture.cancel_order_dialog nofilter}
    form_id             = "cancel-order-form"
}

{/block}

{block name="javascript-initialization"}

    <script type="text/javascript">
        $(".order-cancel").click(function(){
            $("#cancel_order_id").val($(this).attr("data-id"));
        });
    </script>

{/block}

{block name="javascript-last-call"}
    {hook name="orders.js" location="orders-js" }
{/block}
