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

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

{block name="page-title"}{intl l='Thelia Shipping configuration'}{/block}

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

{block name="main-content"}
<div class="shipping-configurations">

    <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/configuration'}">{intl l="Configuration"}</a></li>
            <li><a href="{url path='/admin/configuration/shipping_configuration'}">{intl l="Shipping zones management"}</a></li>
        </ul>

        {hook name="shipping-configuration.top" location="shipping_configuration_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='Shipping zones management'}
                                {include file='renderer/buttons.html' btn_group=false buttons=[
                                    [
                                        'type' => 'create',
                                        'class' => 'action-btn',
                                        'title' => {intl l='Add a new shipping zone'},
                                        'href' => '#creation_dialog',
                                        'auth' => ['resource' => 'admin.configuration.shipping-configuration'],
                                        'data' => [
                                            'toggle' => 'modal'
                                        ]
                                    ]
                                ]}
                            </caption>
                            <thead>
    	                        <tr>
                                   <th>{intl l="Zone name"}</th>
                                   <th>{intl l="Included countries"}</th>
                                   <th>{intl l="Assigned modules"}</th>

                                   {hook name="shipping-configuration.table-header" location="shipping_configuration_table_header" }

                                   <th class="text-center">{intl l="Actions"}</th>
    	                        </tr>
                            </thead>

                            <tbody>
                                {loop name="area-list" type="area" backend_context="1"}
                                <tr>
                                    <td nowrap><a href="{url path="/admin/configuration/shipping_configuration/update/%id" id=$ID}">{$NAME}</a></td>
                                    <td>
                                        {loop name="country-area" type="country" area=$ID backend_context="1" limit="6" visible="*"}
                                            {$TITLE}{if $LOOP_COUNT < $LOOP_TOTAL},{/if}
                                        {/loop}

                                        {ifloop rel="country-area-more"}
                                            {$countries = ''}
                                            {loop name="country-area-more" type="country" area=$ID backend_context="1" offset="6" visible="*"}
                                                {$countries = "$countries, $TITLE"}
                                                {$ctry_count=$LOOP_COUNT}
                                            {/loop}
                                            {$countries = ltrim($countries, ', ')}

                                            <span class="badge" title="{$countries}">{intl l="%count more..." count={$ctry_count}}</span>
                                        {/ifloop}
                                        {elseloop rel="country-area"}
                                            <span class="text-warning"><i class="glyphicon glyphicon-warning-sign"></i> {intl l='None'}</span>
                                        {/elseloop}
                                    </td>
                                    <td>
                                        {loop name="module-area" type="module" area=$ID backend_context="1" order="code"}
                                            {$CODE} - {$TITLE}{if $LOOP_COUNT < $LOOP_TOTAL}<br />{/if}
                                        {/loop}
                                        {elseloop rel="module-area"}
                                            <span class="text-warning"><i class="glyphicon glyphicon-warning-sign"></i> {intl l='None'}</span>
                                        {/elseloop}
                                    </td>

                                    {hook name="shipping-configuration.table-row" location="shipping_configuration_table_row" area_id={$ID} }

                                    <td nowrap class="actions">
                                       {include file='renderer/buttons.html' buttons=[
                                            [
                                                'type' => 'edit',
                                                'class' => 'area-configuration-change',
                                                'title' => {intl l='Change this shipping zone'},
                                                'href' => {url path="/admin/configuration/shipping_configuration/update/%id" id=$ID},
                                                'auth' => ['resource' => 'admin.configuration.shipping-configuration']
                                            ],
                                            [
                                                'type' => 'delete',
                                                'class' => 'area-configuration-delete',
                                                'title' => {intl l='Delete this shipping zone'},
                                                'href' => '#delete_dialog',
                                                'data' => [
                                                    'id' => $ID,
                                                    'toggle' => 'modal'
                                                ],
                                                'auth' => ['resource' => 'admin.configuration.shipping-configuration']
                                            ]
                                        ]}
                                    </td>
                                </tr>
                                {/loop}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        {hook name="shipping-configuration.bottom" location="shipping_configuration_bottom" }

    </div>
</div>

    {* Adding a new Shipping zone *}

    {* Capture the dialog body, to pass it to the generic dialog *}
    {capture "creation_dialog"}
        {form name="thelia.admin.area.create"}
            {form_hidden_fields}

            {* on success, redirect to the edition page, _ID_ is replaced with the created object ID, see controller  *}
            {render_form_field field='success_url' value={url path='/admin/configuration/shipping_configuration/update/_ID_'}}
            {render_form_field field='name'}

            {hook name="shipping-configuration.create-form" location="shipping_configuration_create_form" }
        {/form}
    {/capture}

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

        dialog_id    = "creation_dialog"
        dialog_title = {intl l="Create a new shipping zone"}
        dialog_body  = {$smarty.capture.creation_dialog nofilter}

        dialog_ok_label     = {intl l="Create this shipping zone"}
        dialog_cancel_label = {intl l="Cancel"}

        form_action        = {url path='/admin/configuration/shipping_configuration/create'}
        form_enctype       = ''
        form_error_message = $form_error_message
    }

    {* Delete confirmation dialog *}

    {capture "delete_dialog"}
        <input type="hidden" name="area_id" id="shipping_configuration_delete_id" value="" />

        {hook name="shipping-configuration.delete-form" location="shipping_configuration_delete_form" }
    {/capture}

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

        dialog_id       = "delete_dialog"
        dialog_title    = {intl l="Delete shipping zone"}
        dialog_message  = {intl l="Do you really want to delete this shipping zone ?"}

        form_action         = {token_url path='/admin/configuration/shipping_configuration/delete'}
        form_content        = {$smarty.capture.delete_dialog nofilter}
    }
{/block}

{block name="javascript-initialization"}
    <script>
        $(document).ready(function(){
            $(".area-configuration-delete").click(function(e){
                $("#shipping_configuration_delete_id").val($(this).data("id"));
            });
        });
    </script>
{/block}

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