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

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

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

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

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

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

            {hook name="currencies.top" location="currencies_top" }

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

                    {if ! empty($undefined_rates)}
                        <div class="alert alert-warning">
                            {$currencyList = ''}
                            {loop type="currency" visible="*" name="err-currencies" id={","|implode:$undefined_rates}}
                                {$currencyList = "$currencyList, $NAME ($ISOCODE)"}
                            {/loop}

                            {intl l="<strong>Warning</strong>, an exchange rate was not found for at least one currency: %list" list=$currencyList|ltrim:', '}

                        </div>
                    {/if}

                    <form action="{url path='/admin/configuration/currencies/update-rates'}" method="post">

                        <div class="general-block-decorator">
                            <div class="table-responsive">
                                <table class="table table-striped table-condensed">
                                    <caption class="clearfix">
                                        {intl l='Currencies'}
                                        {include file='renderer/buttons.html' btn_group=false buttons=[
                                            [
                                                'type' => 'create',
                                                'class' => 'action-btn',
                                                'title' => {intl l='Add a new currency'},
                                                'href' => '#creation_dialog',
                                                'auth' => ['resource' => 'admin.configuration.currency'],
                                                'data' => [
                                                    'toggle' => 'modal'
                                                ]
                                            ],
                                            [
                                                'type' => 'info',
                                                'class' => 'action-btn',
                                                'title' => {intl l='Update rates'},
                                                'auth' => ['resource' => 'admin.configuration.currency'],
                                                'icon' => ['class' => 'glyphicon glyphicon-globe'],
                                                'with_text' => true,
                                                'tag' => 'button'
                                            ]
                                        ]}
                                    </caption>
                                    <thead>
                                    <tr>
                                        <th>
                                            {admin_sortable_header
                                            current_order=$order
                                            order='id'
                                            reverse_order='id_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l='ID'}
                                            }
                                        </th>

                                        <th>
                                            {admin_sortable_header
                                            current_order=$order
                                            order='name'
                                            reverse_order='name_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l='Name'}
                                            }
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='code'
                                            reverse_order='code_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l="ISO 4217 Code"}
                                            }
                                            <a title="{intl l='More information about ISO 4217'}" href="http://fr.wikipedia.org/wiki/ISO_4217" target="_blank"><i class="glyphicon glyphicon-question-sign"></i></a>
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='symbol'
                                            reverse_order='symbol_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l="Symbol"}
                                            }
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='rate'
                                            reverse_order='rate_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l="Exchange rate"}
                                            }
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='visible'
                                            reverse_order='visible_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l="Visible"}
                                            }
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='manual'
                                            reverse_order='manual_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l="Position"}
                                            }
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='is_default'
                                            reverse_order='is_default_reverse'
                                            path='/admin/configuration/currencies'
                                            label={intl l="Default"}
                                            }
                                        </th>

                                        {hook name="currencies.table-header" location="currencies_table_header" }

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

                                    <tbody>
                                    {loop name="currencies" type="currency" backend_context="1" visible="*" lang=$lang_id order=$order}
                                        <tr>
                                            <td>{$ID}</td>

                                            <td>
                                                {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.currency" access="UPDATE"}
                                                    <a title="{intl l='Change this currency'}" href="{url path='/admin/configuration/currencies/update' currency_id=$ID}">{$NAME}</a>
                                                {/loop}
                                                {elseloop rel="can_change"}
                                                {$NAME}
                                                {/elseloop}
                                            </td>

                                            <td class="text-center">{$ISOCODE}</td>

                                            <td class="text-center">{$SYMBOL}</td>

                                            <td class="text-center">{format_number number="$RATE" decimals="4"}</td>

                                            <td class="text-center">
                                                <div class="make-switch switch-small change-visible" 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="checkbox" name="visible" value="{$ID}" {if $VISIBLE}checked="checked"{/if}/>
                                                </div>
                                            </td>

                                            <td class="text-center">
                                                {admin_position_block
                                                resource="admin.configuration.currency"
                                                access="UPDATE"
                                                path="/admin/configuration/currencies/update-position"
                                                url_parameter="currency_id"
                                                in_place_edit_class="currencyPositionChange"
                                                position="$POSITION"
                                                id="$ID"
                                                }
                                            </td>

                                            <td class="text-center">
                                                <div class="make-switch switch-small change-default" 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" value="{$ID}" {if $IS_DEFAULT}checked="checked" disabled="disabled"{/if}/>
                                                </div>
                                            </td>

                                            {hook name="currencies.table-row" location="currencies_table_row" currency_id={$ID} }

                                            <td class="actions">
                                                {$buttons = [
                                                    [
                                                        'type' => 'edit',
                                                        'class' => 'currency-change',
                                                        'title' => {intl l='Change this currency'},
                                                        'href' => {url path='/admin/configuration/currencies/update' currency_id=$ID},
                                                        'auth' => ['resource' => 'admin.configuration.currency']
                                                    ]
                                                ]}

                                                {if !$IS_DEFAULT}
                                                {$buttons[] = [
                                                        'type' => 'delete',
                                                        'class' => 'country-delete',
                                                        'title' => {intl l='Delete this country'},
                                                        'href' => '#delete_dialog',
                                                        'data' => [
                                                            'id' => $ID,
                                                            'toggle' => 'modal'
                                                        ],
                                                        'auth' => ['resource' => 'admin.configuration.country']
                                                ]}
                                                {/if}

                                                {include file='renderer/buttons.html' buttons=$buttons}
                                            </td>
                                        </tr>
                                    {/loop}
                                    {elseloop rel="currencies"}
                                        <tr>
                                            <td colspan="9">
                                                <div class="alert alert-info">
                                                    {intl l="No currency has been created yet. Click the + button to create one."}
                                                </div>
                                            </td>
                                        </tr>
                                    {/elseloop}
                                    </tbody>
                                </table>
                            </div>
                        </div>

                    </form>

                </div>

            </div>

            {hook name="currencies.bottom" location="currencies_bottom" }

        </div>
    </div>


    {* Adding a new currency *}

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

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

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

        {form_field field='name'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label} : </label>

                {loop type="lang" name="default-lang" default_only="1"}
                    <div class="input-group">
                        <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="form-control" value="{$value}" title="{intl l='Currency name'}" placeholder="{intl l='Name'}">
                        <span class="input-group-addon"><img src="{image file="assets/img/flags/`$CODE`.png"}" alt="{$TITLE}" /></span>
                    </div>

                    <div class="help-block">{intl l="Enter here the currency name in the default language (%title)" title={$TITLE}}</div>

                    {* Switch edition to the current locale *}
                    <input type="hidden" name="edit_language_id" value="{$ID}" />

                {form_field field='locale'}
                    <input type="hidden" name="{$name}" value="{$LOCALE}" />
                {/form_field}
                {/loop}
            </div>
        {/form_field}

        {form_field field='code'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label} : </label>
                <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="form-control" value="{$value}" title="{intl l='ISO 4217 code'}" placeholder="{intl l='Code'}">
                <span class="help-block"><a href="http://fr.wikipedia.org/wiki/ISO_4217" target="_blank">{intl l='More information about ISO 4217'}</a></span>
            </div>
        {/form_field}

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

        {form_field field='rate'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label} : </label>
                <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="form-control" value="{$value}" title="{intl l='Currency rate'}" placeholder="{intl l='Rate'}">
                <span class="help-block">{intl l="The rate from Euro (Price in Euro * rate = Price in this currency)"}</span>
            </div>
        {/form_field}

        {form_field field='format'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label} : </label>
                <input type="text" size="10" id="{$label_attr.for}" required="required" name="{$name}" value="{$value}" placeholder="{$label_attr.placeholder}" class="form-control">
                <span class="help-block">
                    {$label_attr.help}
                </span>
            </div>
        {/form_field}

        {hook name="currency.create-form" location="currency_create_form" }

    {/capture}

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

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

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

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


    {* Delete confirmation dialog *}

    {capture "delete_dialog"}
        <input type="hidden" name="currency_id" id="currency_delete_id" value="" />

        {hook name="currency.delete-form" location="currency_delete_form" }

    {/capture}

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

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

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

{block name="javascript-initialization"}

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

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

    <script>
        $(function() {

            // Set proper currency ID in delete from
            $('a.currency-delete').click(function(ev) {
                $('#currency_delete_id').val($(this).data('id'));
            });

            // JS stuff for creation form
            {include
                file      = "includes/generic-js-dialog.html"
                dialog_id = "creation_dialog"
                form_name = "thelia.admin.currency.creation"
            }

            {* Inline editing of object position using bootstrap-editable *}

            $('.currencyPositionChange').editable({
                type        : 'text',
                title       : '{intl l="Enter new currency position"}',
                mode        : 'popup',
                inputclass  : 'input-mini',
                placement   : 'left',
                success     : function(response, newValue) {
                    // The URL template
                    var url = "{url noamp='1' path='/admin/configuration/currencies/update-position' currency_id='__ID__' position='__POS__'}";

                    // Perform subtitutions
                    url = url.replace('__ID__', $(this).data('id'))
                            .replace('__POS__', newValue);

                    // Reload the page
                    location.href = url;
                }
            });

            {* Change default status *}

            $('.change-default').on("switch-change", function(event, data) {
                var url = "{url noamp='1' path='/admin/configuration/currencies/set-default' currency_id='__ID__'}";

                // Perform ID subtitutions
                url = url.replace('__ID__', $("input", this).val());

                // Reload the page
                location.href = url;
            });

            $('.change-visible').on("switch-change", function(event, data) {
                var url = "{url noamp='1' path='/admin/configuration/currencies/set-visible' currency_id='__ID__' visible='__VISIBLE__'}";

                url = url.replace('__ID__', $("input", this).val());

                url = url.replace('__VISIBLE__', $("input", this).is(':checked') ? 1 : 0);

                // Reload the page
                location.href = url;
            });

        });
    </script>
{/block}

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