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

{block name="page-title"}{intl l='Edit a shipping configuration'}{/block}

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

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

    <div id="wrapper" class="container">
        {loop name="area-edit" type="area" id=$area_id}
        <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 configuration"}</a></li>
            <li>{intl l='Editing shipping zone "%name"' name="{$NAME}"}</li>
        </ul>

        <div class="row">
            <div class="col-md-12">
                <div class="general-block-decorator">

                    <div class="title title-without-tabs">
                        <div class="row">
                            <div class="col-md-10">
                                {intl l='Shipping zone "%title"' title={$NAME}}
                            </div>
                            <div class="col-md-2 text-right">
                                <a href="{url path='/admin/configuration/shipping_configuration'}" class="btn btn-sm btn-default">{intl l='Close'} <span class="glyphicon glyphicon-remove"></span></a>
                            </div>
                        </div>
                    </div>

                    <div class="form-container clearfix">
                        <div class="col-md-4">
                            
                            {ifloop rel="country-without-area"}
                            {form name="thelia.admin.area.country"}
                            <form method="POST" action="{url path="/admin/configuration/shipping_configuration/country/add"}">
                                {form_hidden_fields form=$form}

                                {form_field form=$form field='success_url'}
                                    <input type="hidden" name="{$name}" value="{url path="/admin/configuration/shipping_configuration/update/{$area_id}"}" >
                                {/form_field}

                                {form_field form=$form field='area_id'}
                                    <input type="hidden" name="{$name}" value="{$area_id}">
                                {/form_field}
                                {form_field form=$form field='country_id'}
                                <label class="control-label" for="{$label_attr.for}">{intl l="Unassigned countries"}</label>

                                <div class="input-group">                                                            
                                    <select name="{$name}" id="{$label_attr.for}" data-toggle="selectpicker">
                                        {loop name="country-without-area" type="country" with_area="0"  backend_context="1" lang=$lang_id}
                                        <option value="{$ID}">{$TITLE}</option>
                                        {/loop}
                                    </select>
                                    <span class="input-group-btn">
                                        <button type="submit" class="btn btn-default btn-primary"><span class="glyphicon glyphicon-plus-sign"></span> {intl l="Add this country"}</button>
                                    </span>
                                </div>                                    
                                {/form_field}
                            </form>
                            {/form}
                            {/ifloop}
                            {elseloop rel="country-without-area"}
                                <div class="alert alert-info">
                                    {intl l="All countries are assigned to a shipping zone."}
                                </div>
                            {/elseloop}
                        </div>

                        <div class="col-md-8">
                            <div class="table-responsive">
                                <table class="table table-striped table-condensed">
                                    <thead>
                                        <tr>
                                            <th>{intl l="Countries assigned to this zone"}</th>
                                            <th class="text-center">{intl l="Actions"}</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {loop name="country-area" type="country" area=$area_id backend_context="1"}
                                        <tr>
                                            <td>{$TITLE}</td>
                                            <td class="text-center">
                                                <a class="btn btn-default btn-xs btn-delete-country" title="{intl l='Delete this country'}"  href="#delete_country_dialog" data-id="{$ID}" data-toggle="modal">
                                                    <span class="glyphicon glyphicon-trash"></span>
                                                </a>
                                            </td>
                                        </tr>
                                        {/loop}
                                        {elseloop rel="country-area"}
                                            <td colspan="2">
                                                {intl l="This shipping zone does not contains any country."}
                                            </td>
                                        {/elseloop}
                                    </tbody>
                                    {module_include location='shipping-configuration-edit'}
                                </table>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
        {/loop}

        {elseloop rel="area-edit"}
        <div class="row">
            <div class="col-md-12">
                {intl l="No area defined with this id"}
            </div>
        </div>
        {/elseloop}
    </div>
</div>  

    {* Delete related content confirmation dialog *}

    {capture "delete_country_dialog"}
        <input type="hidden" name="area_id" value="{$area_id}">
        <input type="hidden" name="country_id" id="delete-country-id" value="">

        {module_include location='shipping_configuration_delete_form'}
    {/capture}

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

        dialog_id       = "delete_country_dialog"
        dialog_title    = {intl l="Remove country"}
        dialog_message  = {intl l="Do you really want to remove this country from this shipping zone ?"}

        form_action     = {url path='/admin/configuration/shipping_configuration/country/remove'}
        form_content    = {$smarty.capture.delete_country_dialog nofilter}
    }  
{/block}

{block name="javascript-initialization"}
    {javascripts file='assets/js/main.js'}
        <script src="{$asset_url}"></script>        
    {/javascripts}
    {javascripts file='assets/js/bootstrap-select/bootstrap-select.js'}
        <script src="{$asset_url}"></script>        
    {/javascripts}

    <script>
        $(document).ready(function(){
            $(".btn-delete-country").click(function(e){
                $("#delete-country-id").val($(this).data("id"));
            });
        });
    </script>
{/block}

{block name="javascript-last-call"}
    {module_include location='shipping-configuration-edit-js'}
{/block}