{% extends "base.html" %}

{% block title %}{{ _('Search') }} - {{ app_name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3 mb-0">
                    <i class="fas fa-search text-primary"></i> {{ _('Search Results') }}
                </h1>
                <form method="GET" class="d-flex" action="{{ url_for('main.search') }}">
                    <input type="text" class="form-control me-2" name="q" placeholder="{{ _('Search notes or tags') }}" value="{{ query }}">
                    <button class="btn btn-primary" type="submit"><i class="fas fa-search"></i></button>
                </form>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-list me-1"></i> {{ _('Results') }}</h5>
                </div>
                <div class="card-body">
                    {% if entries and entries.items %}
                    <div class="table-responsive">
                        <table class="table table-hover table-zebra" data-table-enhanced data-page-size="25" data-sticky-header="true" data-column-visibility="true">
                            <thead>
                                <tr>
                                    <th>{{ _('Project') }}</th>
                                    <th>{{ _('Start') }}</th>
                                    <th>{{ _('End') }}</th>
                                    <th>{{ _('Duration') }}</th>
                                    <th>{{ _('Notes') }}</th>
                                    <th>{{ _('Tags') }}</th>
                                    <th class="text-end">{{ _('Actions') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for entry in entries.items %}
                                <tr>
                                    <td>
                                        {% if entry.project %}
                                            <a href="{{ url_for('projects.view_project', project_id=entry.project.id) }}">{{ entry.project.name }}</a>
                                        {% elif entry.client %}
                                            {{ entry.client.name }} <span class="text-xs text-gray-500">({{ _('Direct') }})</span>
                                        {% else %}
                                            {{ _('N/A') }}
                                        {% endif %}
                                    </td>
                                                                <td>{{ entry.start_time|user_datetime }}</td>
                            <td>{{ entry.end_time|user_datetime if entry.end_time else '-' }}</td>
                                    <td><strong>{{ entry.duration_formatted }}</strong></td>
                                    <td>
                                        {% if entry.notes %}
                                        <span title="{{ entry.notes }}">{{ entry.notes[:80] }}{% if entry.notes|length > 80 %}...{% endif %}</span>
                                        {% else %}<span class="text-muted">-</span>{% endif %}
                                    </td>
                                    <td>{{ entry.tags or '-' }}</td>
                                    <td class="text-end">
                                        <div class="btn-group" role="group">
                                            <a href="{{ url_for('timer.resume_timer_by_id', timer_id=entry.id) }}" 
                                               class="btn btn-sm btn-action btn-action--success" title="{{ _('Resume - Start a new timer with same properties') }}">
                                                <i class="fas fa-play"></i>
                                            </a>
                                            <a href="{{ url_for('timer.edit_timer', timer_id=entry.id) }}" 
                                               class="btn btn-sm btn-action btn-action--edit" title="{{ _('Edit entry') }}">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            {% if current_user.is_admin or entry.user_id == current_user.id %}
                                            <form method="POST" action="{{ url_for('timer.delete_timer', timer_id=entry.id) }}" 
                                                  class="d-inline" data-confirm="{{ _('Are you sure you want to delete this time entry? This action cannot be undone.') }}">
                                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                                <button type="submit" class="btn btn-sm btn-action btn-action--danger" title="{{ _('Delete entry') }}">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <nav aria-label="Search pagination" class="mt-3">
                        <ul class="pagination">
                            {% if entries.has_prev %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('main.search', q=query, page=entries.prev_num) }}">{{ _('Previous') }}</a>
                            </li>
                            {% else %}
                            <li class="page-item disabled"><span class="page-link">Previous</span></li>
                            {% endif %}

                            <li class="page-item disabled"><span class="page-link">{{ _('Page') }} {{ entries.page }} {{ _('of') }} {{ entries.pages }}</span></li>

                            {% if entries.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('main.search', q=query, page=entries.next_num) }}">{{ _('Next') }}</a>
                            </li>
                            {% else %}
                            <li class="page-item disabled"><span class="page-link">Next</span></li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-search fa-3x text-muted mb-3"></i>
                        <h4 class="text-muted">{{ _('No results found') }}</h4>
                        <p class="text-muted">{{ _('Try a different query or check your spelling.') }}</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
<script>
// Generic data-confirm handler
document.addEventListener('DOMContentLoaded', function(){
  document.querySelectorAll('form[data-confirm]').forEach(function(f){
    f.addEventListener('submit', async function(e){
      e.preventDefault();
      var msg = f.getAttribute('data-confirm') || '';
      if (msg) {
        const confirmed = await showConfirm(msg, {
          title: 'Confirm Action',
          confirmText: 'Confirm',
          cancelText: 'Cancel',
          variant: 'warning'
        });
        if (confirmed) {
          f.submit();
        }
      } else {
        f.submit();
      }
    });
  });
});
</script>
{% endblock %}


