{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% import "macro_functions.phtml" as mf %}
{% set active_menu = 'system' %}
{% block meta_title %}{% trans 'Currency settings' %}{% endblock %}

{% block breadcrumbs %}
<ul>
    <li class="firstB"><a href="{{ '/'|alink }}">{% trans 'Home' %}</a></li>
    <li><a href="{{ 'system'|alink }}">{% trans 'Settings' %}</a></li>
    <li class="lastB">{% trans 'Currency settings' %}</li>
</ul>
{% endblock %}


{% block content %}

<div class="widget simpleTabs">

    <ul class="tabs">
        <li><a href="#tab-currencies">{% trans 'Currencies' %}</a></li>
        <li><a href="#tab-new-currency">{% trans 'Add new currency' %}</a></li>
        <li><a href="#tab-api-settings">{% trans 'Settings' %}</a></li>
        <li><a href="#tab-converter">{% trans 'Converter' %}</a></li>
    </ul>

    <div class="tabs_container">
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-currencies">

            <div class="help">
                <h3>{% trans 'Manage currencies' %}</h3>
                <p>{% trans 'Default currency is the one you manage all your prices in admin area. If your client in client area chooses other than default currency, prices will be recalculated according to conversion rate. You can have only one default currency. You can not add same currency once again. Changing default currency is not recommended when active orders are available. Nothing is recalculated on default currency change. Your income is calculated in default currency. Changing default currency after you have paid invoices will distort income statistics. Currency rates are taken from European Central Bank and currencylayer.' %}</p>
            </div>

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <th>{% trans 'ISO code' %}</th>
                        <th>{% trans 'Title' %}</th>
                        <th>{% trans 'Conversion rate' %}</th>
                        <th>{% trans 'Example price' %}</th>
                        <th style="width:17%">&nbsp;</th>
                    </tr>
                </thead>

                <tbody>
                {% for currency in admin.currency_get_list.list %}
                  <tr>
                      <td>{% if currency.default %}<strong>{{ currency.code }}</strong>{% else %}{{ currency.code }}{% endif %}</td>
                      <td>{{ currency.title }}</td>
                      <td>{{ currency.conversion_rate }}</td>
                      <td>{{ mf.currency_format(1) }} = {{ mf.currency(1, currency.code) }}</td>
                      <td class="actions">
                          <a class="bb-button btn14" href="{{ '/currency/manage'|alink }}/{{currency.code}}"><img src="images/icons/dark/pencil.png" alt=""></a>
                          <a class="bb-button btn14 bb-rm-tr api-link" data-api-redirect="{{'extension/settings/currency'|alink}}" data-api-confirm="Are you sure?" href="{{'api/admin/currency/delete'|link({'code' : currency.code})}}"><img src="images/icons/dark/trash.png" alt=""></a>
                          {% if not currency.default %}
                          <a class="bb-button btn14 api-link" data-api-redirect="{{'extension/settings/currency'|alink}}" href="{{'api/admin/currency/set_default' | link({'code' : currency.code})}}" title="Set as default" class="rightDir"><img src="images/icons/dark/star.png" alt=""></a>
                          {% endif %}
                      </td>
                  </tr>
                {% else %}
                  <tr>
                      <td colspan="5">
                          {% trans 'The list is empty' %}
                      </td>
                  </tr>
                {% endfor %}
                </tbody>

                <tfoot>
                    <tr>
                        <td colspan="5">
                            <div class="aligncenter">
                                <a href="{{'api/admin/currency/update_rates'|link}}" data-api-redirect="{{'extension/settings/currency'|alink}}" class="btn55 mr10 api-link"><img src="images/icons/middlenav/refresh4.png" alt=""><span>{% trans 'Update Rates' %}</span></a>
                            </div>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-new-currency">

            <form method="post" action="{{ 'api/admin/currency/create'|link }}" class="mainForm save api-form" data-api-redirect="{{ 'extension/settings/currency'|alink }}">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Code' %}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('code', admin.currency_get_pairs, 'USD', 1) }}
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Title' %}:</label>
                        <div class="formRight">
                            <input type="text" name="title" value="" required="required">
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Conversion rate' %}:</label>
                        <div class="formRight">
                            <input type="text" name="conversion_rate" value="1" required="required">
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Format' %}:</label>
                        <div class="formRight">
                            <input type="text" name="format" value="$ {{'{{price}}'}}" required="required">
                        </div>
                        <div class="fix"></div>
                    </div>

                    <input type="submit" value="{% trans 'Add' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>


        </div>
        
        <div class="tab_content nopadding" id="tab-api-settings">
            <div class="help">
                <h3>{% trans 'Adjust settings for currency rate updater' %}</h3>
                <p>BoxBilling uses data grabbed from <a href="https://www.ecb.europa.eu/home/html/index.en.html">European Central Bank</a> for conversions from Euro to other currencies, and <a href="https://currencylayer.com/">currencylayer</a> for conversations from US Dollar to other currencies.<br />If you choose Euro as your default currency, data from European Central Bank will be used, if you choose US Dollar, data from currencylayer will be used.<br />While European Central Bank decided to keep it's API free to everyone, you won't need an autorization key to use ECB's data. However, you'll need an API key to use currencylayer.<br />You can <a href="https://currencylayer.com/product">grab your API key</a> from currencylayer's website. BoxBilling doesn't make any profits from your purchases on currencylayer.com</p>
                <p><b>BoxBilling does not take any responsibilities for data grabbed from 3rd party sources, including European Central Bank and currencylayer.</b></p>
            </div>
            <form method="post" action="{{ 'api/admin/currency/update_rate_settings'|link }}" class="mainForm save api-form" data-api-msg="Successfully updated settings">
            <fieldset>
                <div class="rowElem noborder">
                    <label>{% trans 'API key for currencylayer' %}:</label>
                    <div class="formRight">
                        <input type="text" name="currencylayer_key" value="{{admin.currency_get_key}}">
                    </div>
                    <div class="fix"></div>
                </div>
                <div class="rowElem">
                <label>{% trans 'Enable CRON jobs' %}:</label>
                <p>If you enable this, conversion rates will be automatically updated whenever the CRON job fires up.</p>
                <div class="formRight">
                    <input type="radio" name="crons_enabled" value="1" {% if admin.currency_is_cron_enabled == 1 %}checked="checked"{% endif %}><label>Enabled</label>
                    <input type="radio" name="crons_enabled" value="0" {% if admin.currency_is_cron_enabled == 0 %}checked="checked"{% endif %}><label>Disabled</label>
                </div>
                <div class="fix"></div>
            </div>
                <input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
            </fieldset>
            </form>
        </div>

        <div class="tab_content nopadding" id="tab-converter">

            <form method="post" action="" class="mainForm">
                <fieldset>
                    <div class="rowElem">
                        <div class="moreFields">
                            <ul>
                                <li style="width: 100px"><input type="text" name="amount" value="" required="required" onkeyup="doConvert();" placeholder="{% trans 'Amount' %}"></li>
                                <li class="sep txt">{{ guest.currency_get.code }} = </li>
                                <li class="sep txt"></li>
                                <li style="width: 100px"><input type="text" id="converted_result" value="" placeholder="{% trans 'Result' %}" readonly="readonly"></li>
                                <li class="sep txt"></li>
                                <li style="width: 100px">{{ mf.selectbox('to', guest.currency_get_pairs) }}</li>
                            </ul>
                        </div> 
                        <div class="fix"></div>
                    </div>
                </fieldset>
            </form>

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

{% endblock %}

{% block js %}
<script type="text/javascript">
    function doConvert() {
        var price = $('input[name="amount"]').val();
        var code = $('select[name="to"]').val();
        bb.post('guest/currency/format', {price:price, code: code}, function(result){
            $('#converted_result').val(result);
        });
        return false;
    };
</script>
{% endblock %}
