{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}

{% import "macro_functions.phtml" as mf %}
{% set active_menu = 'system' %}
{% block meta_title %}{{ 'Currency settings'|trans }}{% endblock %}

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

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

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

            <div class="help">
                <h3>{{ 'Manage currencies'|trans }}</h3>
                <p>{{ '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.'|trans }}</p>
            </div>

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <th>{{ 'ISO code'|trans }}</th>
                        <th>{{ 'Title'|trans }}</th>
                        <th>{{ 'Conversion rate'|trans }}</th>
                        <th>{{ 'Example price'|trans }}</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">
                          {{ 'The list is empty'|trans }}
                      </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>{{ 'Update Rates'|trans }}</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>{{ 'Code'|trans }}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('code', admin.currency_get_pairs, 'USD', 1) }}
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{{ 'Title'|trans }}:</label>
                        <div class="formRight">
                            <input type="text" name="title" value="" required="required">
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{{ 'Conversion rate'|trans }}:</label>
                        <div class="formRight">
                            <input type="text" name="conversion_rate" value="1" required="required">
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{{ 'Format'|trans }}:</label>
                        <div class="formRight">
                            <input type="text" name="format" value="$ {{'{{price}}'}}" required="required">
                        </div>
                        <div class="fix"></div>
                    </div>

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


        </div>
        
        <div class="tab_content nopadding" id="tab-api-settings">
            <div class="help">
                <h3>{{ 'Adjust settings for currency rate updater'|trans }}</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>{{ 'API key for currencylayer'|trans }}:</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>{{ 'Enable CRON jobs'|trans }}:</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="{{ 'Update'|trans }}" 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="{{ 'Amount'|trans }}">
                                </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="{{ 'Result'|trans }}" 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 %}
