{% extends request.ajax ? 'layout_blank.html.twig' : 'layout_default.html.twig' %}

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

{% block meta_title %}{{ 'Redirects'|trans }}{% endblock %}

{% set active_menu = 'system' %}

{% block breadcrumbs %}
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|alink }}">
                <svg class="icon">
                    <use xlink:href="#home" />
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'system'|alink }}">{{ 'Settings'|trans }}</a></li>
        <li class="breadcrumb-item active" aria-current="page">{{ 'Redirects'|trans }}</li>
    </ol>
{% endblock %}

{% block content %}

    <div class="card">
        <div class="card-header">
            <h3 class="card-title">{{ 'Redirects'|trans }}</h3>
        </div>
        <div class="card-body border-bottom">
            <h3 class="card-title">{{ 'Create new redirect'|trans }}</h3>
            <form method="post" action="{{ 'api/admin/redirect/create'|link }}" class="api-form" data-api-reload="1">
                <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}">
                <div class="row row-cards">
                    <div class="col-sm-5 col-md-4 mb-3">
                        <label class="form-label required" for="path">Path</label>
                        <div class="col-auto">
                            <div class="input-group input-group-flat">
                            <span class="input-group-text">
                                {{ ''|link }}
                            </span>
                                <input type="text" class="form-control ps-0" name="path" id="path">
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-5 col-md-4 mb-3">
                        <label class="form-label required" for="target">Target</label>
                        <div class="col-auto">
                            <div class="input-group input-group-flat">
                            <span class="input-group-text">
                                {{ ''|link }}
                            </span>
                                <input type="text" class="form-control ps-0" name="target" id="target">
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-2 col-md-4 mb-3 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary">
                            <svg class="icon me-2">
                                <use xlink:href="#plus" />
                            </svg>
                            {{ 'Add'|trans }}
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <div class="table-responsive">
            <table class="table card-table table-vcenter table-striped text-nowrap">
                <thead>
                <tr>
                    <th>{{ 'Path'|trans }}</th>
                    <th></th>
                    <th>{{ 'Target'|trans }}</th>
                    <th class="w-1"></th>
                </tr>
                </thead>
                <tbody>
                {% set redirects = admin.redirect_get_list({ 'per_page': 100, 'page': request.page }|merge(request)) %}
                {% for r in redirects %}
                    <tr>
                        <td>
                            <a href="{{ r.path|link }}" target="_blank">{{ r.path | link }}</a>
                        </td>
                        <td>
                            <svg class="icon">
                                <use xlink:href="#arrow-right" />
                            </svg>
                        </td>
                        <td>
                            {% if '://' in r.target %}
                                <a href="{{ r.target }}" target="_blank">{{ r.target }}</a>
                            {% else %}
                                <a href="{{ r.target|link }}" target="_blank">{{ r.target | link }}</a>
                            {% endif %}
                        </td>
                        <td>
                            <a class="btn btn-icon api-link"
                               href="{{ 'api/admin/redirect/delete'|link({ 'id': r.id, 'CSRFToken': CSRFToken }) }}"
                               data-api-confirm="{{ 'Are you sure?'|trans }}"
                               data-api-type="danger"
                               data-api-confirm-btn="Delete"
                               data-api-reload="1">
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                {% else %}
                    <tr>
                        <td class="text-muted" colspan="4">{{ 'The list is empty'|trans }}</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
