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

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

{% block meta_title %}{{ 'Support Tickets'|trans }}{% endblock %}

{% block breadcrumbs %}
    <ul class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|url }}">
                <svg class="icon">
                    <use xlink:href="#home" />
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'support'|url }}">{{ 'Support'|trans }}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{{ 'Tickets'|trans }}</li>
    </ul>
{% endblock %}

{% set active_menu = 'support' %}
{% set has_filter_panel = true %}
{% set filter_panel_id = 'page-search-filters' %}
{% set filter_panel_open = request_has_filters %}
{% set support_toolbar_actions %}
    <button class="btn btn-primary" type="button" data-bs-toggle="modal" data-bs-target="#newClientTicketModal">
        <svg class="icon">
            <use xlink:href="#plus" />
        </svg>
        {{ 'New Client Ticket'|trans }}
    </button>
{% endset %}

{% block top_content %}
{% if not filter_panel_open %}
    {% set statuses = admin.support_ticket_get_statuses %}
    <section class="row row-cards mb-3 filter-panel-summary">
        <div class="col-sm-6 col-lg-3">
            <a class="card card-sm card-link" href="{{ 'support'|url(query: { 'status': 'open' }) }}">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-danger text-white avatar">{{ statuses.open }}</span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">{{ 'Awaiting Staff'|trans }}</div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-sm-6 col-lg-3">
            <a class="card card-sm card-link" href="{{ 'support'|url(query: { 'status': 'on_hold' }) }}">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-warning text-white avatar">{{ statuses.on_hold }}</span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">{{ 'Awaiting Client'|trans }}</div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-sm-6 col-lg-3">
            <a class="card card-sm card-link" href="{{ 'support'|url(query: { 'status': 'closed' }) }}">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-secondary text-white avatar">{{ statuses.closed }}</span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">{{ 'Solved'|trans }}</div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-sm-6 col-lg-3">
            <a class="card card-sm card-link" href="{{ 'support'|url }}">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-blue text-white avatar">{{ statuses.open + statuses.on_hold + statuses.closed }}</span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">{{ 'Total'|trans }}</div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
    </section>
    {% endif %}
{% endblock %}

{% block content %}
    <div class="card">
        <div class="card-header">
            <div>
                <h3 class="card-title">{{ 'Support Tickets'|trans }}</h3>
                <p class="card-subtitle">{{ 'Manage client and public tickets, filter conversations, and open new support requests.'|trans }}</p>
            </div>
        </div>
        <div class="card-body p-0">
            {{ include('partial_search.html.twig', {
                search_body_class: filter_panel_open ? 'pb-2 border-bottom-0' : '',
                search_toolbar_actions: support_toolbar_actions
            }) }}
            <section id="{{ filter_panel_id }}" class="card-body pt-0 px-3 pb-3 border-bottom collapse{% if filter_panel_open %} show{% endif %}">
                <form method="get" class="border border-top-0 rounded-top-0 rounded-bottom-3 bg-body-tertiary p-3">
                    {% if request.search %}
                        <input type="hidden" name="search" value="{{ request.search }}">
                    {% endif %}
                    <div class="row g-3">
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="filter-ticket-client-id">{{ 'Client ID'|trans }}</label>
                                <input class="form-control" type="text" id="filter-ticket-client-id" name="client_id" value="{{ request.client_id }}" placeholder="{{ 'Client ID'|trans }}">
                            </div>
                        </div>
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="filter-ticket-order-id">{{ 'Order ID'|trans }}</label>
                                <input class="form-control" type="text" id="filter-ticket-order-id" name="order_id" value="{{ request.order_id }}" placeholder="{{ 'Order ID'|trans }}">
                            </div>
                        </div>
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="filter-ticket-subject">{{ 'Ticket Subject'|trans }}</label>
                                <input class="form-control" type="text" id="filter-ticket-subject" name="subject" value="{{ request.subject }}" placeholder="{{ 'Ticket Subject'|trans }}">
                            </div>
                        </div>
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="filter-ticket-message">{{ 'Ticket Messages'|trans }}</label>
                                <input class="form-control" type="text" id="filter-ticket-message" name="content" value="{{ request.content }}" placeholder="{{ 'Search Ticket Messages'|trans }}">
                            </div>
                        </div>
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="filter-ticket-priority">{{ 'Priority'|trans }}</label>
                                <input class="form-control" type="text" id="filter-ticket-priority" name="priority" value="{{ request.priority }}" placeholder="{{ 'Priority'|trans }}">
                            </div>
                        </div>
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="status">{{ 'Status'|trans }}</label>
                                {{ mf.selectbox('status', admin.support_ticket_get_statuses({ 'titles': true }), request.status, 0, 'All Statuses'|trans) }}
                            </div>
                        </div>
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="support_helpdesk_id">{{ 'Helpdesk'|trans }}</label>
                                {{ mf.selectbox('support_helpdesk_id', admin.support_helpdesk_get_pairs, request.support_helpdesk_id, 0, 'All Helpdesks'|trans) }}
                            </div>
                        </div>
                        <div class="col-md-6 col-xl-3">
                            <div>
                                <label class="form-label text-muted small mb-1" for="created_at">{{ 'Created At'|trans }}</label>
                                <div class="input-group">
                                    <div class="input-icon w-100">
                                        <input class="form-control datepicker"
                                               id="created_at"
                                               placeholder="{{ 'Select Date Range'|trans }}"
                                               value="{% if request.date_from %}{{ request.date_from|date('Y-m-d') }}{% endif %}{% if request.date_to %} to {{ request.date_to|date('Y-m-d') }}{% endif %}"
                                               data-name-from="date_from"
                                               data-name-to="date_to"
                                        >
                                        <span class="input-icon-addon">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                                <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                                <path d="M4 5m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z"></path>
                                                <path d="M16 3l0 4"></path>
                                                <path d="M8 3l0 4"></path>
                                                <path d="M4 11l16 0"></path>
                                                <path d="M11 15l1 0"></path>
                                                <path d="M12 15l0 3"></path>
                                            </svg>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="d-flex justify-content-end gap-2 mt-3">
                        <a class="btn btn-outline-secondary" href="{{ request_path|url }}">{{ 'Reset'|trans }}</a>
                        <button class="btn btn-primary" type="submit">
                            <svg class="icon">
                                <use xlink:href="#filter" />
                            </svg>
                            {{ 'Apply Filters'|trans }}
                        </button>
                    </div>
                </form>
            </section>
            <div class="table-responsive">
            <table class="table card-table table-vcenter table-striped text-nowrap sortable">
                <thead>
                    <tr>
                        <th class="w-1 no-sort">
                            <input class="form-check-input m-0 align-middle batch-delete-master-checkbox" type="checkbox">
                        </th>
                        <th>{{ 'Client'|trans }}</th>
                        <th>{{ 'Subject'|trans }}</th>
                        <th>{{ 'Status'|trans }}</th>
                        <th>{{ 'Helpdesk'|trans }}</th>
                        <th class="w-1 no-sort"></th>
                    </tr>
                </thead>
                <tbody>
                    {% set tickets = admin.support_ticket_get_list({ 'per_page': 25, 'page': request.page, 'deep': 0, 'include_first': 0 }|merge(request)) %}
                    {% for i, ticket in tickets.list %}
                    <tr class="priority_{{ ticket.priority }}">
                        <td>
                            <input class="form-check-input m-0 align-middle batch-delete-checkbox" type="checkbox" data-item-id="{{ ticket.id }}">
                        </td>
                        <td>
                            <div class="d-flex py-1 align-items-center">
                                <a href='{{ "client/manage/#{ticket.client_id}"|url }}'>
                                    {{ avatar(ticket.client.email, 40, 'avatar me-2') }}
                                </a>
                                <div class="flex-fill">
                                    <div class="font-weight-medium">
                                        <a href='{{ "client/manage/#{ticket.client_id}"|url }}'>{{ ticket.client.first_name }} {{ ticket.client.last_name }}</a>
                                    </div>
                                    <div class="text-muted">{{ ticket.client.email }}</div>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div>
                                <a href='{{ "support/ticket/#{ticket.id}"|url }}#reply-box'>#{{ ticket.id }} - {{ ticket.subject|truncate(50) }} ({{ ticket.replies }})</a>
                            </div>
                            {% if ticket.notes|length %}
                                <a href="#" rel="{{ ticket.id }}" title="{{ ticket.notes|length }}" class="show-notes">
                                    <svg class="icon">
                                        <use xlink:href="#notes" />
                                    </svg>
                                </a>
                            {% endif %}
                            <span class="text-muted">{{ ticket.updated_at|timeago }} {{ 'ago'|trans }}</span>
                        </td>
                        <td>{{ mf.status_badge(ticket.status, 'ticket') }}</td>
                        <td>{{ ticket.helpdesk.name }}</td>
                        <td>
                            <a class="btn btn-icon" href='{{ "support/ticket/#{ticket.id}"|url }}#reply-box'>
                                <svg class="icon">
                                    <use xlink:href="#edit" />
                                </svg>
                            </a>
                            <a class="btn btn-icon" href="{{ 'support/ticket_delete'|api_url(query: { 'id': ticket.id }) }}"
                                {{ fb_api_link({ message: 'Are you sure?'|trans, modal: {type: 'confirm', title: 'Are you sure?'|trans}, redirect: 'support'|url }) }}>
                                <svg class="icon">
                                    <use xlink:href="#delete" />
                                </svg>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td class="text-muted" colspan="6">{{ 'The list is empty'|trans }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            </div>

            <div class="card-footer d-flex align-items-center justify-content-between">
                {% include 'partial_batch_delete.html.twig' with { 'action': 'support/batch_delete' } %}
                {% include 'partial_pagination.html.twig' with { 'list': tickets, 'url': 'support' } %}
            </div>
        </div>

        <div class="modal modal-blur fade" id="newClientTicketModal" tabindex="-1" aria-labelledby="newClientTicketModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-scrollable modal-fullscreen-md-down modal-lg">
                <form class="modal-content fb-modal-form" method="post" action="{{ 'support/ticket_create'|api_url }}" {{ fb_api_form({ redirect: 'support'|url }) }}>
                    <div class="modal-header">
                        <h3 class="modal-title" id="newClientTicketModalLabel">{{ 'Open Ticket for Existing Client'|trans }}</h3>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ 'Close'|trans }}"></button>
                    </div>
                    <div class="modal-body">
                        <section class="fb-modal-section">
                            <div class="fb-modal-section-header">
                                <div class="fb-modal-section-heading-row">
                                    <h4 class="fb-modal-section-title">{{ 'Ticket Details'|trans }}</h4>
                                </div>
                                <p class="fb-modal-section-description">{{ 'Choose the client and helpdesk, then draft the opening message.'|trans }}</p>
                            </div>
                            <div class="row g-3">
                                <div class="col-12">
                                    <label class="form-label" for="client_id">{{ 'Client'|trans }}</label>
                                    {% if not request.client_id %}
                                        <select class="form-control autocomplete-selector"
                                                placeholder="{{ 'Start Typing the Client Name or ID'|trans }}"
                                                id="client_id"
                                                name="client_id"
                                                data-resturl="/api/admin/client/get_pairs">
                                        </select>
                                    {% else %}
                                        {% set client = admin.client_get({ 'id': request.client_id }) %}
                                        <input type="text" id="client_id" class="form-control" disabled value="{{ client.first_name }} {{ client.last_name }}" />
                                        <input type="hidden" name="client_id" value="{{ request.client_id }}" />
                                    {% endif %}
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">{{ 'Helpdesk'|trans }}</label>
                                    {{ mf.selectbox('support_helpdesk_id', admin.support_helpdesk_get_pairs, '', 1) }}
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label" for="new-client-ticket-subject">{{ 'Subject'|trans }}</label>
                                    <input class="form-control" type="text" id="new-client-ticket-subject" name="subject" value="{{ request.subject|default('') }}" required>
                                </div>
                                <div class="col-12">
                                    <label class="form-label" for="new-client-ticket-message">{{ 'Message'|trans }}</label>
                                    <textarea class="form-control editor-textarea" name="content" rows="10" required id="new-client-ticket-message">{{ profile.signature }}</textarea>
                                </div>
                            </div>
                        </section>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ 'Cancel'|trans }}</button>
                        <button type="submit" class="btn btn-primary">{{ 'Create'|trans }}</button>
                    </div>
                </form>
            </div>
        </div>

    </div>
{% endblock %}

{% block js %}
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const modalConfigs = [
            { elementId: 'newClientTicketModal', hash: '#tab-new' }
        ];

        for (const { elementId, hash } of modalConfigs) {
            const modalElement = document.getElementById(elementId);
            if (!modalElement) {
                continue;
            }

            const modal = bootstrap.Modal.getOrCreateInstance(modalElement);
            if (window.location.hash === hash) {
                modal.show();
            }

            modalElement.addEventListener('hidden.bs.modal', function () {
                if (window.location.hash !== hash) {
                    return;
                }

                const url = new URL(window.location.href);
                url.hash = '';
                window.history.replaceState({}, '', url);
            });
        }
    });

    document.querySelectorAll('a.show-notes').forEach(notes => {
        notes.addEventListener('click', function(e) {
            e.preventDefault();

            FOSSBilling.api.admin.post('support/ticket_get', { id: e.currentTarget.getAttribute('rel') }, function(result){
                var html = document.createElement('div');

                result.notes.forEach(function(entry) {
                    var div = document.createElement('div');
                    div.innerHTML = entry.note;

                    html.appendChild(div);
                    html.appendChild(document.createElement('hr'));
                });

                Modals.create({
                    title: "{{ 'Notes'|trans }}",
                    content: html.outerHTML,
                });
            });
        });
    })

</script>
{% endblock %}

{% block head %}{{ wysiwyg('.editor-textarea') }}{% endblock %}
