{% extends 'layouts/admin.twig' %}

{% block content %}
    <ul class="breadcrumbs el">
        <li><a href="/admin/">Admin</a> /</li>
        <li><a href="{{ route('admin-subscriptions') }}">Subscriptions</a> /</li>
        <li class="end">Logs</li>
    </ul>

    <div class="content-card">
        <h1>{{ title }}</h1>

        <!-- Action Type Summary -->
        <div class="stats-container">
            {% for action_key, count in action_counts %}
                {% if count > 0 %}
                    <div class="card">
                        <a href="{{ route('admin-subscription-logs') }}?action_type={{ action_key }}" class="stat-box">
                            <span class="stat-label">{{ action_types[action_key] ?? action_key }}</span>
                            <span class="stat-value">{{ count }}</span>
                        </a>
                    </div>
                {% endif %}
            {% endfor %}
        </div>

        {% include 'admin/messages.twig' %}

        <!-- Filters -->
        <form action="{{ route('admin-subscription-logs') }}" method="GET" class="style" id="search-form">
            <fieldset class="show">
                <h1><a class="show el">Search <i class="fa fa-caret-right"></i></a></h1>
                <div class="row">
                    <label for="subscription_id">Subscription ID</label>
                    <input type="number" name="subscription_id" id="subscription_id" value="{{ request.subscription_id }}" />
                </div>
                <div class="row">
                    <label for="action_type">Action Type</label>
                    <select name="action_type" id="action_type">
                        <option value="">All Actions</option>
                        {% for action_key, action_label in action_types %}
                            <option value="{{ action_key }}" {% if request.action_type == action_key %}selected{% endif %}>
                                {{ action_label }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="row">
                    <label for="source">Source</label>
                    <select name="source" id="source">
                        <option value="">All Sources</option>
                        {% for source_key, source_label in sources %}
                            <option value="{{ source_key }}" {% if request.source == source_key %}selected{% endif %}>
                                {{ source_label }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="row">
                    <label for="date_start">Start Date</label>
                    <input type="date" name="date_start" id="date_start" value="{{ request.date_start }}" />
                </div>
                <div class="row">
                    <label for="date_end">End Date</label>
                    <input type="date" name="date_end" id="date_end" value="{{ request.date_end }}" />
                </div>
                <div class="search-button">
                    <input type="submit" value="Search" class="BUTTON"/>
                </div>
                <div class="reset-button">
                    <a href="{{ route('admin-subscription-logs') }}" class="BUTTON">Reset</a>
                </div>
            </fieldset>
        </form>
    </div>

    {% if logs|length > 0 %}
        <div class="content-card">
            <table class="manage-items">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Subscription</th>
                        <th>Member</th>
                        <th>Action</th>
                        <th>Description</th>
                        <th>Actor</th>
                        <th>Source</th>
                        <th>Changes</th>
                        <th>Date/Time</th>
                        <th>IP Address</th>
                        <th class="center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for log in logs %}
                        <tr>
                            <td>{{ log.id }}</td>
                            <td>
                                <a href="{{ route('admin-subscriptions-edit', log.subscription_id) }}" target="_blank">
                                    #{{ log.subscription_id }}
                                </a>
                            </td>
                            <td>{{ log.subscription_member }}</td>
                            <td>
                                <span class="badge badge-{{ log.action_type }}">
                                    {{ log.action_type|replace({'_': ' '})|title }}
                                </span>
                            </td>
                            <td>{{ log.action_description|truncate(50) }}</td>
                            <td>{{ log.actor }}</td>
                            <td>
                                <span class="badge badge-{{ log.source }}">
                                    {{ log.source }}
                                </span>
                            </td>
                            <td>
                                {% if log.changes %}
                                    <small class="text-muted">{{ log.changes|truncate(30) }}</small>
                                {% else %}
                                    <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>{{ log.created_at }}</td>
                            <td>
                                <small>{{ log.ip_address ?? '-' }}</small>
                            </td>
                            <td class="center">
                                <a href="{{ log.actions.view }}" class="edit">
                                    <i class="fa fa-eye"></i>
                                </a>
                                <a href="{{ route('admin-subscription-logs-for-subscription', log.subscription_id) }}" class="edit">
                                    <i class="fa fa-history"></i>
                                </a>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>

            {% include 'navigation/pagination.twig' %}
        </div>

        <div class="content-card">
            <form action="{{ route('admin-subscription-logs-export') }}" method="post" class="style">
                <fieldset class="show">
                    <h1><a class="show el">Export Logs <i class="fa fa-caret-down"></i></a></h1>
                    <div class="row">Click export below to export the above subscription logs as a CSV</div>
                </fieldset>
                <div class="row">
                    {% for key, value in request %}
                        {% if key != '_token' %}
                            <input type="hidden" name="{{ key }}" value="{{ value }}"/>
                        {% endif %}
                    {% endfor %}
                    <input type="submit" value="Export"/>
                </div>
            </form>
        </div>
    {% else %}
        <div class="content-card">
            <p>There were no subscription logs found matching your search criteria</p>
        </div>
    {% endif %}
{% endblock %}

{% block head %}
<link rel="stylesheet" href="/plugins/mtc_pharmacy_subscriptions/admin/css/style.css">

<script>
    $(document).ready(function() {
        $('.reset-button').on('click', function(e) {
            e.preventDefault();
            $('#search-form').find('input').val('');
            $('#search-form').find('select').val('');
            $('#search-form').submit();
        });
    });
</script>
{% endblock %}