{% extends 'layout_default.html.twig' %}

{% import 'macro_functions.html.twig' as mf %}

{% block meta_title %}Domain management{% endblock %}

{% set active_menu = 'system' %}

{% block content %}
    <ul class="nav nav-tabs" role="tablist">
        <li class="nav-item" role="presentation">
            <a class="nav-link active" href="#tab-tlds" data-bs-toggle="tab">{{ 'Top level domains'|trans }}</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-new-tld" data-bs-toggle="tab">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New top level domain'|trans }}
            </a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-registrars" data-bs-toggle="tab">{{ 'Registrars'|trans }}</a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-new-registrar" data-bs-toggle="tab">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'New domain registrar'|trans }}
            </a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-extension-directory" data-bs-toggle="tab">
                <svg class="icon me-2">
                    <use xlink:href="#plus" />
                </svg>
                {{ 'Install from the Extension Directory'|trans }}
            </a>
        </li>
        <li class="nav-item" role="presentation">
            <a class="nav-link" href="#tab-nameservers" data-bs-toggle="tab">{{ 'Nameservers'|trans }}</a>
        </li>
    </ul>

<div class="card">
    <div class="tab-content">
        <div class="tab-pane fade show active" id="tab-tlds" role="tabpanel">
            <div class="card-body">
                <h5>{{ 'Manage TLDs'|trans }}</h5>
                <p class="text-muted">{{ 'Setup domain pricing and allowed operations. Assign specific domain registrars for each Top Level Domain (TLD)'|trans }}</p>
            </div>

            <table class="table card-table table-vcenter table-striped text-nowrap sortable">
                <thead>
                    <tr>
                        <th>{{ 'TLD'|trans }}</th>
                        <th>{{ 'Registration'|trans }}</th>
                        <th>{{ 'Renew'|trans }}</th>
                        <th>{{ 'Transfer'|trans }} </th>
                        <th>{{ 'Operations'|trans }}</th>
                        <th>{{ 'Registrar'|trans }}</th>
                        <th class="w-1"></th>
                    </tr>
                </thead>
                <tbody>
                    {% for tld in admin.servicedomain_tld_get_list({ 'per_page': 99 }).list %}
                    <tr>
                        <td>
                            <a href="{{ 'servicedomain/id'|alink }}/{{ tld.id }}">{{ tld.tld }}</a>
                        </td>
                        <td>{{ mf.currency_format(tld.price_registration) }}</td>
                        <td>{{ mf.currency_format(tld.price_renew) }}</td>
                        <td>{{ mf.currency_format(tld.price_transfer) }}</td>
                        <td>
                        {{ 'Allow register:'|trans }} {% if tld.allow_register %}{{ 'Yes'|trans }}{% else %}{{ 'No'|trans }}{% endif %}<br/>
                        {{ 'Allow transfer:'|trans }} {% if tld.allow_transfer %}{{ 'Yes'|trans }}{% else %}{{ 'No'|trans }}{% endif %}<br/>
                        {{ 'Active:'|trans }} {% if tld.active %}{{ 'Yes'|trans }}{% else %}{{ 'No'|trans }}{% endif %}
                        </td>
                        <td>
                            <a href="{{ 'servicedomain/registrar'|alink }}/{{ tld.registrar.id }}">{{ tld.registrar.title }}</a>
                        </td>
                        <td>
                            <a class="btn btn-icon" href="{{ 'servicedomain/id'|alink }}/{{ tld.id }}">
                                <svg class="icon">
                                    <use xlink:href="#edit" />
                                </svg>
                            </a>
                            <a class="btn btn-icon api-link" data-api-confirm="{{ 'Are you sure?'|trans }}" data-api-reload="1" href="{{ 'api/admin/servicedomain/tld_delete'|link({ 'tld': tld.tld, 'CSRFToken': CSRFToken }) }}">
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab-pane fade" id="tab-new-tld" role="tabpanel">
            <div class="card-body">
                <h5>{{ 'Add new top level domain'|trans }}</h5>
                <p class="text-muted">{{ 'Setup new TLD prices and properties'|trans }}</p>

                <form method="post" action="{{ 'api/admin/servicedomain/tld_create'|link }}" class="api-form" data-api-reload="1">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Tld'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="tld" value="." required title="{{ 'Must start with a dot'|trans }}">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Registrar'|trans }}:</label>
                        <div class="col">
                            <select class="form-select" name="tld_registrar_id" required>
                                {% for id,title in admin.servicedomain_registrar_get_pairs %}
                                <option value="{{ id }}">{{ title }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Registration price'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="price_registration" required>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Renewal price'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="price_renew" required>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Transfer price'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="price_transfer" required>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Minimum years of registration'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="min_years" value="1" required>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Allow registration'|trans }}:</label>
                        <div class="col">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioAllowRegisterYes" type="radio" name="allow_register" value="1" checked>
                                <label class="form-check-label" for="radioAllowRegisterYes">{{ 'Yes'|trans }}</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioAllowRegisterNo" type="radio" name="allow_register" value="0">
                                <label class="form-check-label" for="radioAllowRegisterNo">{{ 'No'|trans }}</label>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Allow transfer'|trans }}:</label>
                        <div class="col">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioAllowTransferYes" type="radio" name="allow_transfer" value="1" checked>
                                <label class="form-check-label" for="radioAllowTransferYes">{{ 'Yes'|trans }}</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioAllowTransferNo" type="radio" name="allow_transfer" value="0">
                                <label class="form-check-label" for="radioAllowTransferNo">{{ 'No'|trans }}</label>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Active'|trans }}:</label>
                        <div class="col">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioActiveYes" type="radio" name="active" value="1" checked>
                                <label class="form-check-label" for="radioActiveYes">{{ 'Yes'|trans }}</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" id="radioActiveNo" type="radio" name="active" value="0">
                                <label class="form-check-label" for="radioActiveNo">{{ 'No'|trans }}</label>
                            </div>
                        </div>
                    </div>

                    <input type="submit" value="{{ 'Add'|trans }}" class="btn btn-primary w-100">
                </form>
            </div>
        </div>

        <div class="tab-pane fade" id="tab-registrars" role="tabpanel">
            <div class="card-body">
                <h5>{{ 'Domain registrars'|trans }}</h5>
                <p class="text-muted">{{ 'Manage domain registrars'|trans }}</p>
            </div>

            <table class="table card-table table-vcenter table-striped text-nowrap">
                <thead>
                    <tr>
                        <th>{{ 'Title'|trans }}</th>
                        <th class="w-1"></th>
                    </tr>
                </thead>
                <tbody>
                    {% for registrar in admin.servicedomain_registrar_get_list.list %}
                    <tr>
                        <td>
                            <a href="{{ 'servicedomain/registrar/'|alink }}/{{ registrar.id }}">{{ registrar.title }}</a>
                        </td>
                        <td>
                            <a class="btn btn-icon api-link" href="{{ 'api/admin/servicedomain/registrar_copy'|link({ 'id': registrar.id, 'CSRFToken': CSRFToken }) }}" data-api-reload="1" title="Install">
                                <svg class="icon">
                                    <use xlink:href="#copy" />
                                </svg>
                            </a>
                            <a class="btn btn-icon" href="{{ 'servicedomain/registrar/'|alink }}/{{ registrar.id }}">
                                <svg class="icon">
                                    <use xlink:href="#edit" />
                                </svg>
                            </a>
                            <a class="btn btn-icon api-link" href="{{ 'api/admin/servicedomain/registrar_delete'|link({ 'id': registrar.id, 'CSRFToken': CSRFToken }) }}" data-api-confirm="{{ 'Are you sure?'|trans }}" data-api-reload="1">
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab-pane fade" id="tab-new-registrar" role="tabpanel">
            <div class="card-body">
                <h1>{{ 'Adding new domain registrar'|trans }}</h1>
                <p>{{ 'Follow instructions below to install new domain registrar.'|trans }}</p>

                <div class="">
                    <ul>
                        <li>{{ 'Check domain registrar you are looking for is available at'|trans }} <a href="http://extensions.fossbilling.org/" target="_blank">FOSSBilling extensions site</a></li>
                        <li>{{ 'Download domain registrar file and place to'|trans }} <strong>{{ constant('PATH_LIBRARY') }}/Registrar/Adapter</strong></li>
                        <li>{{ 'Reload this page to see newly detected domain registrar'|trans }}</li>
                        <li>{{ 'Click on install button. Now you will be able to create top level domains with new domain registrar'|trans }}</li>
                        <li>{{ 'For developers. Read'|trans }} <a href="https://fossbilling.org/docs" target="_blank">{{ 'FOSSBilling documentation'|trans }}</a> {{ 'to learn how to create your own domain registrar.'|trans }}</li>
                    </ul>
                </div>
            </div>

            {% if admin.servicedomain_registrar_get_available|length > 0 %}
            <table class="table card-table table-vcenter table-striped text-nowrap">
                <thead>
                    <tr>
                        <th>{{ 'Code'|trans }}</th>
                        <th class="w-1">{{ 'Install'|trans }}</th>
                    </tr>
                </thead>
                <tbody>
                {% for code in admin.servicedomain_registrar_get_available %}
                <tr>
                    <td>{{ code }}</td>
                    <td>
                        <a class="btn btn-icon api-link" href="{{ 'api/admin/servicedomain/registrar_install'|link({ 'code': code, 'CSRFToken': CSRFToken }) }}" data-api-msg="{{ 'Domain registrar installed'|trans }}" title="Install">
                            <svg  class="icon">
                                <use xlink:href="#cog" />
                            </svg>
                        </a>
                    </td>
                </tr>
                </tbody>
                {% else %}
                <tbody>
                    <tr>
                        <td class="text-muted" colspan="5">{{ 'All payment gateways installed'|trans }}</td>
                    </tr>
                </tbody>
                {% endfor %}
            </table>
            {% endif %}
        </div>

        <div class="tab-pane fade" id="tab-extension-directory" role="tabpanel">
            {% include "partial_extensions.html.twig" with { 'type': 'domain-registrar', 'header': 'List of domain registrars on the Extension Directory' } only %}
        </div>

        <div class="tab-pane fade" id="tab-nameservers" role="tabpanel">
            <div class="card-body">
                <h5>{{ 'Nameservers'|trans }}</h5>
                <p class="text-muted">{{ 'Setup default nameservers that will be used for new domain registrations if client have not provided his own nameservers in order form'|trans }}</p>

                <form method="post" action="{{ 'api/admin/system/update_params'|link }}" class="api-form" data-api-msg="{{ 'Nameservers updated'|trans }}">
                    <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
                    {% set params = admin.system_get_params %}
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Nameserver 1'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="nameserver_1" value="{{ params.nameserver_1 }}">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Nameserver 2'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="nameserver_2" value="{{ params.nameserver_2 }}">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Nameserver 3'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="nameserver_3" value="{{ params.nameserver_3 }}">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label class="form-label col-3 col-form-label">{{ 'Nameserver 4'|trans }}:</label>
                        <div class="col">
                            <input class="form-control" type="text" name="nameserver_4" value="{{ params.nameserver_4 }}">
                        </div>
                    </div>

                    <input type="submit" value="{{ 'Update nameservers'|trans }}" class="btn btn-primary w-100">
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
