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

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

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

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

        <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/countries'}">{intl l="Countries"}</a></li>
            </ul>

            {module_include location='countries_top'}

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

                    <form action="" method="post">

                        <div class="general-block-decorator">
                            <div class="table-responsive">
                                <table class="table table-striped table-condensed">
                                   <caption class="clearfix">
                                        {intl l='Countries'}
                                        {loop type="auth" name="can_create" role="ADMIN" resource="admin.configuration.country" access="CREATE"}
                                            <a class="btn btn-default btn-primary action-btn" title="{intl l='Add a new country'}" href="#add_country_dialog" data-toggle="modal">
                                                <span class="glyphicon glyphicon-plus-sign"></span>
                                            </a>
                                        {/loop}
                                    </caption>
                                    <thead>
                                        <tr>
                                            <th>{intl  l="ID"}</th>
                                            <th>{intl l="Name"}</th>
                                            <th>{intl l="Default"}</th>
                                            <th>{intl l="Shop"}</th>
                                            <th>{intl l="N° ISO"}</th>
                                            <th>{intl l="ISO Code"}</th>

                                            {module_include location='countries_table_header'}

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

                                    <tbody>
                                        {loop name="countries" type="country" backend_context="1" lang=$lang_id order=$order}
                                            <tr>
                                                <td>{$ID}</td>
                                                <td><a href="{url path="/admin/configuration/country/update/{$ID}"}">{$TITLE}</a></td>
                                                <td>
                                                    <div class="make-switch switch-small switch-radio change-default-toggle" data-id="{$ID}" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok'></i>" data-off-label="<i class='glyphicon glyphicon-remove'></i>">
                                                        <input class="change-default-toggle" type="radio" name="by_default" value="{$ID}" {if $IS_DEFAULT}checked="checked"{/if}/>
                                                    </div>
                                                </td>
{*                                                <td>
                                                    <div class="make-switch switch-small switch-radio" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok'></i>" data-off-label="<i class='glyphicon glyphicon-remove'></i>">
                                                        <input class="change-default" type="radio" name="" value="{$ID}" {if $IS_DEFAULT}selected="selected"{/if}/>
                                                    </div>                                                
                                                </td> *}
                                                <td>{$ISOCODE}</td>
                                                <td>{$ISOALPHA3}</td>

                                                {module_include location='countries_table_row'}

                                                <td class="actions">
                                                    <div class="btn-group">
                                                        {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.country" access="UPDATE"}
                                                            <a class="btn btn-default btn-xs country-change" title="{intl l='Change this country'}" href="{url path="/admin/configuration/country/update/{$ID}"}">
                                                                <span class="glyphicon glyphicon-edit"></span>
                                                            </a>
                                                        {/loop}

                                                        {loop type="auth" name="can_delete" role="ADMIN" resource="admin.configuration.country" access="DELETE"}
                                                            <a class="btn btn-default btn-xs country-delete" title="{intl l='Delete this country'}" href="#delete_dialog" data-id="{$ID}" data-toggle="modal">
                                                                <span class="glyphicon glyphicon-trash"></span>
                                                            </a>
                                                        {/loop}
                                                    </div>
                                                </td>
                                            </tr>
                                        {/loop}
                                        {elseloop rel="countries"}
                                            <tr>
                                                <td colspan="8">
                                                    <div class="alert alert-info">
                                                        {intl l="No country has been created yet. Click the + button to create one."}
                                                    </div>
                                                </td>
                                            </tr>
                                        {/elseloop}
                                    </tbody>
                                </table>
                            </div>
                        </div>

                    </form>

                </div>

            </div>

            {module_include location='countries_bottom'}

        </div>
    </div>

    {* Adding a new Country *}

    {form name="thelia.admin.country.creation"}

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

            {form_hidden_fields form=$form}

            {form_field form=$form field='success_url'}
               {* on success, redirect to the edition page, _ID_ is replaced with the created object ID, see controller  *}
               <input type="hidden" name="{$name}" value="{url path='/admin/configuration/country/update/_ID_'}" />
            {/form_field}

            {form_field form=$form field='title'}
                <div class="form-group {if $error}has-error{/if}">
                    <label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label>
                    <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{intl l="{$label}"}" placeholder="{intl l='Country title'}">
                </div>
            {/form_field}

            {form_field form=$form field='area'}
                <div class="form-group {if $error}has-error{/if}">
                    <label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label>
                    <select name="{$name}" id="{$label_attr.for}" class="form-control">                        
                        <option value="{$ID}">{$TITLE}</option>                        
                    </select>
                </div>
            {/form_field}

            {form_field form=$form field='isocode'}
                <div class="form-group {if $error}has-error{/if}">
                    <label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label>
                    <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{intl l="{$label}"}" placeholder="{intl l='ISO Code'}">
                </div>
            {/form_field}

            {form_field form=$form field='isoalpha2'}
                <div class="form-group {if $error}has-error{/if}">
                    <label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label>
                    <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{intl l="{$label}"}" placeholder="{intl l='Alpha code 2'}">
                </div>
            {/form_field}

            {form_field form=$form field='isoalpha3'}
                <div class="form-group {if $error}has-error{/if}">
                    <label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label>
                    <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{intl l="{$label}"}" placeholder="{intl l='Alpha code 3'}">
                </div>
            {/form_field}
            {loop type="lang" name="default-lang" default_only="1"}
                <input type="hidden" name="edit_language_id" value="{$ID}" />
                {form_field form=$form field='locale'}
                    <input type="hidden" name="{$name}" value="{$LOCALE}" />
                {/form_field}
            {/loop}

            {module_include location='country_create_form'}

        {/capture}

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

            dialog_id    = "add_country_dialog"
            dialog_title = {intl l="Create a new country"}
            dialog_body  = {$smarty.capture.country_creation_dialog nofilter}

            dialog_ok_label     = {intl l="Create this country"}
            dialog_cancel_label = {intl l="Cancel"}

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


    {* Delete confirmation dialog *}

    {capture "delete_dialog"}
        <input type="hidden" name="country_id" id="country_delete_id" value="" />

        {module_include location='country_delete_form'}

    {/capture}

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

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

        form_action         = {url path='/admin/configuration/countries/delete'}
        form_content        = {$smarty.capture.delete_dialog nofilter}
    }

<div class="modal fade" id="toggle-default-failed" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content alert alert-block alert-danger ">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h2>{intl l="Error"}</h2>
            </div>
            <div class="modal-body">
                <strong>{intl l="Impossible to change default country. Please contact your administrator or try later"}</strong>
            </div>


        </div>
    </div>
</div>

{/block}

{block name="javascript-initialization"}

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

        <script>
            $(document).ready(function(){
                // Toogle switch on input radio
                $('.switch-radio').on('switch-change', function () {
                    $('.switch-radio').bootstrapSwitch('toggleRadioState');
                });

                $('.country-delete').click(function(ev){
                    $('#country_delete_id').val($(this).data('id'));
                });

                $('.change-default-toggle').on('switch-change', function(e, data){
                    if(data.value) {
                        $.ajax({
                            url : "{url path='/admin/configuration/country/toggleDefault'}",
                            data : {
                                country_id: $(this).data('id')
                            }
                        }).fail(function(){
                            $('#toggle-default-failed').modal('show');
                        });
                    }
                });
            });

        </script>
    {/javascripts}

{/block}