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

{% block scripts %}
    <script type="module" src="{{ vite_asset('plugins/mtc_pharmacy_subscriptions/admin/js/admin_app.js') }}"></script>
{% endblock %}

{% block content %}

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

    <h1>{{ title }}</h1>

    <ul class="tabnav el">
        <li class="{{ selected_tab == 'manage' ? 'selected' : '' }}">
            <a href="{{ route('admin-subscriptions') }}">All</a>
        </li>
        <li class="{{ selected_tab == 'edit' ? 'selected' : '' }}">
            <a href="">Edit</a>
        </li>
    </ul>

    <div class="page-actions" style="margin-bottom: 20px;">
        <a href="{{ route('admin-subscription-logs-for-subscription', subscription.id) }}" class="BUTTON">
            <i class="fa fa-eye"></i> View Logs
        </a>

    </div>

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

    {% set form_errors = messages.errors %}

    <form action="" method="POST" class="style">
        <fieldset class="show">
            <h1><a class="show el">Configured values<i class="fa fa-caret-right"></i></a></h1>

            <div class="row">
                <label>Member</label>
                <div class="defaultValue">
                    <a href="{{ route('admin-manage-member', { member_id: subscription.member.id }) }}">{{ subscription.member.email }}</a>
                </div>
            </div>

            {{ form_field({
                'type': 'text',
                'name': 'interval_multiplier',
                'label': 'Interval Multiplier',
                'value': subscription.interval_multiplier,
                'errors': form_errors,
            }) }}

            {{ form_field({
                'type': 'select',
                'name': 'interval',
                'label': 'Interval',
                'value': subscription.interval,
                'values': allowed_intervals,
                'errors': form_errors,
            }) }}

            {{ form_field({
                'type': 'select',
                'name': 'status',
                'label': 'Status',
                'value': subscription.status,
                'values': allowed_statuses,
                'errors': form_errors,
            }) }}

            {{ form_field({
                'type': 'number',
                'name': 'max_number_of_issues',
                'label': 'Max number of issues',
                'value': subscription.max_number_of_issues,
                'notes': 'Calculated effective value: ' ~ subscription.getMaxNumberOfIssues(),
                'errors': form_errors,
            }) }}

            {{ form_field({
                'type': 'text',
                'name': 'expiry_date',
                'label': 'Expiry date',
                'value': subscription.expiry_date,
                'notes': '(based on the related assessment form)',
                'class': 'datetim',
                'errors': form_errors,
                'readonly': false,
            }) }}

            {{ form_field({
                'type': 'text',
                'name': 'next_issue_date',
                'label': 'Next issue date',
                'value': subscription.next_issue_date,
                'class': 'datetim',
                'errors': form_errors,
                'readonly': false,
            }) }}

            {{ form_field({
                'type': 'checkbox',
                'name': 'can_be_paused',
                'id': 'can_be_paused',
                'label': 'Can be paused',
                'value': subscription.can_be_paused,
                'notes': 'Calculated effective value: ' ~ subscription.getCanBePausedString(),
                'field_value': 1,
                'errors': form_errors,
            }) }}

            <div class="row">
                <label>Price per issue</label>
                <div class="defaultValue">
                   {{ subscription.getPricePerIssue() }}
                </div>
            </div>


            <div class="row">
                <button type="submit" name="button_pressed" value="save" class="btn">
                    Save
                </button>
            </div>

        </fieldset>

        <fieldset class="show">
            <h1><a class="show el">Manage items <i class="fa fa-caret-right"></i></a></h1>

            <button type="submit" name="button_pressed" value="add_item"  class="btn hidden">
                Add selected item
            </button>

            {% include 'admin/subscription__manage_items.twig' with {
                'items': subscription.items,
                'subscription': subscription,
                'delete_route_name': 'admin-subscriptions-items-delete',
            } %}
        </fieldset>

        <fieldset class="show hidden">
            <h1><a class="show el">Test actions<i class="fa fa-caret-right"></i></a></h1>

            <div class="row">
                <a href="{{ route('admin-subscriptions-generate-order', { subscription_id: subscription.id }) }}"
                    class="btn"
                    target="_blank"
                >
                    Generate Order (no payment)
                </a>
            </div>

            <div class="row">
                <a href="{{ route('admin-subscriptions-generate-order', { subscription_id: subscription.id, attempt_token_payment: 'y' }) }}"
                    class="btn"
                    target="_blank"
                >
                    Generate Order (attempt payment)
                </a>
            </div>

            <div class="row">
                <a href="{{ route('admin-subscriptions-generate-order', { subscription_id: subscription.id, ignore_checks: 'y' }) }}"
                    class="btn"
                    target="_blank"
                >
                    Generate Order (no checks + no payment)
                </a>
            </div>

            <div class="row">
                <a href="{{ route('admin-subscriptions-generate-order', { subscription_id: subscription.id, attempt_token_payment: 'y', ignore_checks: 'y' }) }}"
                    class="btn"
                    target="_blank"
                >
                    Generate Order (no checks + attempt payment)
                </a>
            </div>
        </fieldset>

        {% if subscription.cancellations.count > 0 %}
            <fieldset class="show">
                <h1><a class="show el">Cancellation Reasons<i class="fa fa-caret-right"></i></a></h1>

                <table class="manage-items">
                    <tbody class="tabular">
                    <tr>
                        <th>Reason</th>
                        <th>Date</th>
                    </tr>
                    {% for cancellation in subscription.cancellations %}
                        <tr>
                            <td>
                                {{ cancellation.reason }}
                            </td>
                            <td>
                                {{ cancellation.created_at }}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </fieldset>
        {% endif %}
    </form>

{% endblock %}
