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

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

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

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

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

        {module_include location='languages_top'}

        <div class="row">
            <div class="col-md-12">
                <div class="general-block-decorator">
                    
                    <form action="" method="">
                        
                        <table class="table table-striped table-condensed table-left-aligned">
                            <caption>
                               {intl l="Languages management"}
                               {loop type="auth" name="can_create" role="ADMIN" resource="admin.configuration.language" access="CREATE"}
                                <a class="btn btn-default btn-primary action-btn" title="{intl l='Add a new language'}" href="#creation_dialog" data-toggle="modal">
                                    <span class="glyphicon glyphicon-plus-sign"></span>
                                </a>
                            {/loop}

                            </caption>
                            <thead>
                                <tr>
                                    <th>{intl l="Language name"}</th>
                                    <th>{intl l="ISO 639 Code"}</th>
                                    <th>{intl l="Locale"}</th>
                                    <th>{intl l="date form"}</th>
                                    <th>{intl l="time form"}</th>
                                    <th>{intl l="Default"}</th>
                                    <th>{intl l="Actions"}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {loop type="lang" name="lang.list" backend_context="1"}
                                <tr>
                                    <td>{$TITLE}</td>
                                    <td>{$CODE}</td>
                                    <td>{$LOCALE}</td>
                                    <td>{$DATE_FORMAT}</td>
                                    <td>{$TIME_FORMAT}</td>
                                    <td>
                                        <div class="make-switch switch-small switch-radio lang-default-change" 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 type="radio" name="is_default" {if $IS_DEFAULT}checked{/if}>
                                        </div>
                                    </td>
                                    <td class="actions">
                                        <div class="btn-group">
                                            {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.language" access="UPDATE"}
                                                <a class="btn btn-default btn-xs lang-change" data-id="{$ID}" title="{intl l='Change this language'}">
                                                    <span class="glyphicon glyphicon-edit"></span>
                                                </a>
                                            {/loop}
                                            {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.language" access="DELETE"}
                                                <a href="#delete_dialog" data-toggle="modal" data-id="{$ID}" class="btn btn-default btn-xs lang-delete" title="{intl l="Delete this language"}">
                                                    <span class="glyphicon glyphicon-trash"></span>
                                                </a>
                                            {/loop}
                                        </div>
                                    </td>
                                </tr>
                                {/loop}
                            </tbody>
                        </table>

                    </form>

                </div>
            </div>



        </div>

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

                    <div class="title title-without-tabs">{intl l="Parameters"}</div>
                    {form name="thelia.lang.defaultBehavior"}
                    <form action="{url path="/admin/configuration/languages/defaultBehavior"}" method="post">
                        {form_hidden_fields form=$form}
                        {form_field form=$form field="behavior"}
                        <div class="form-group {if $error}has-error{/if}" >
                            <label for="{$label_attr.for}" class="label-control">{intl l="If a translation is missing or incomplete :"}</label>
                            <div class="input-group">
                                <select name="{$name}" id="{$label_attr.for}" data-toggle="selectpicker">
                                    {foreach $choices as $choice}
                                        <option value="{$choice->value}" {if $lang_without_translation == $choice->value}selected="selected"{/if}>{$choice->label}</option>
                                    {/foreach}
                                </select>
                                <div class="input-group-btn">
                                    <button type="submit" class="btn btn-default btn-primary"><span class="glyphicon glyphicon-check"></span></button>
                                </div>
                            </div>
                        </div>
                        {/form_field}
                    </form>
                    {/form}
                </div>
            </div>
            <div class="col-md-6">
                <div class="general-block-decorator clearfix">
                    
                    <div class="title title-without-tabs">{intl l="Using a domain or subdomain for each language"}</div>
                    {form name="thelia.lang.url"}
                    <form action="{url path="/admin/configuration/languages/updateUrl"}" method="post">
                        {form_hidden_fields form=$form}
                        <table class="table table-striped table-condensed table-left-aligned">
                            <tbody>
                            {form_tagged_fields form=$form tag='url'}
                                {if $attr_list.url_id}
                                <tr>
                                    <th>{$attr_list.url_title}</th>
                                    <td>
                                        <div class="form-group {if $error}has-error{/if}">
                                        <input type="text" class="form-control" name="{$name}" value="{$value}" placeholder="http://www.domain.com" {if $one_domain_per_lang == 0}readonly{/if}>
                                        </div>
                                    </td>
                                </tr>
                                {/if}
                            {/form_tagged_fields}
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td></td>
                                    <td class="pull-right">
                                        {if $one_domain_per_lang == 0}
                                            <a href="{url path="/admin/configuration/languages/domain/activate"}" class="btn btn-default btn-success"><span class="glyphicon glyphicon-ok-sign"></span> {intl l="activate"}</a>
                                        {else}
                                            <a href="{url path="/admin/configuration/languages/domain/deactivate"}" class="btn btn-default btn-danger"><span class="glyphicon glyphicon-ok-sign"></span> {intl l="deactivate"}</a>
                                        {/if}

                                        <button type="submit" {if $one_domain_per_lang == 0}disabled{/if} class="btn btn-default btn-primary"><span class="glyphicon glyphicon-check"></span> {intl l="Save"}</button>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </form>
                    {/form}
                </div>
            </div>
        </div>

        {module_include location='languages_bottom'}

    </div>
</div>

{form name="thelia.lang.create"}

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

        {form_hidden_fields form=$form}

        {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='Language title'}">
            </div>
        {/form_field}
        {form_field form=$form field='code'}
            <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='locale'}
            <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='en_US'}">
            </div>
        {/form_field}
        {form_field form=$form field='date_format'}
            <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='d-m-Y'}">
            </div>
        {/form_field}
        {form_field form=$form field='time_format'}
            <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='H:i:s'}">
            </div>
        {/form_field}

        {module_include location='language_create_form'}

    {/capture}

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

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

        dialog_ok_label     = {intl l="Create this language"}

        form_action        = {url path='/admin/configuration/languages/add'}
        form_enctype       = {form_enctype form=$form}
        form_error_message = $form_error_message
    }
{/form}

{* Delete confirmation dialog *}

{capture "delete_dialog"}
    <input type="hidden" name="language_id" id="language_delete_id" value="" />

    {module_include location='languages_delete_form'}

{/capture}

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

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

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

    <div id="lang-update-modal"></div>

    <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 languages. 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>
    {/javascripts}

    {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(){
            $("a.lang-change").click(function(e){
                var baseUrl = "{url path="/admin/configuration/languages/update/"}";
                $('body').append('<div class="modal-backdrop fade in" id="loading-event"><div class="loading"></div></div>');
                $.ajax({
                    method: 'get',
                    url: baseUrl+$(this).data('id')
                }).done(function(data){
                    $("#loading-event").remove();
                    $("#lang-update-modal").html(data);
                    $("#edit_lang_dialog").modal("show");
                }).fail(function(){
                    $("#loading-event").remove();
                });
            });

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

            $(".lang-delete").click(function(){
                $("#language_delete_id").val($(this).data("id"));
            });
        });
    </script>
{/block}