{% extends "base.html" %}

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h2">Call Queue</h1>
        <div class="d-flex gap-2">
            <span class="badge bg-primary d-flex align-items-center px-3">{{ calls.paginator.count }} Pending Calls</span>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover mb-0 align-middle">
                <thead class="bg-light">
                    <tr>
                        <th class="ps-4">Voter</th>
                        <th>Phone</th>
                        <th>Assigned Volunteer</th>
                        <th>Comments</th>
                        <th>Scheduled</th>
                        <th class="pe-4 text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for call in calls %}
                    <tr>
                        <td class="ps-4">
                            <a href="{% url 'voter_detail' call.voter.id %}" class="fw-semibold text-primary text-decoration-none d-block">
                                {{ call.voter.first_name }} {{ call.voter.last_name }}
                            </a>
                        </td>
                        <td>
                            {% if call.voter.phone %}
                                <div class="d-flex align-items-center">
                                    <a href="tel:{{ call.voter.phone }}" class="text-decoration-none text-dark fw-medium me-2">{{ call.voter.phone }}</a>
                                    <a href="tel:{{ call.voter.phone }}" class="text-primary me-2" title="Call"><i class="bi bi-telephone" style="font-size: 0.85rem;"></i></a>
                                </div>
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            {% if call.volunteer %}
                                <a href="{% url 'volunteer_detail' call.volunteer.id %}" class="text-decoration-none text-dark">
                                    {{ call.volunteer }}
                                </a>
                            {% else %}
                                <span class="text-muted small">Unassigned</span>
                            {% endif %}
                        </td>
                        <td><small class="text-muted">{{ call.comments|default:"-" }}</small></td>
                        <td><small class="text-muted" title="{{ call.created_at }}">{{ call.created_at|timesince }} ago</small></td>
                        <td class="pe-4 text-end">
                            <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#callNotesModal" 
                                    data-call-id="{{ call.id }}" data-voter-name="{{ call.voter.first_name }} {{ call.voter.last_name }}">
                                <i class="bi bi-telephone me-1"></i>Make Call
                            </button>
                            <form action="{% url 'delete_call' call.id %}" method="POST" class="d-inline ms-1">
                                {% csrf_token %}
                                <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Remove this call from queue?')">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-center py-5 text-muted">
                            <p class="mb-0">The call queue is currently empty.</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        {% if calls.paginator.num_pages > 1 %}
        <div class="card-footer bg-white border-0 py-3">
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center mb-0">
                    {% if calls.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ calls.previous_page_number }}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    {% endif %}
                    <li class="page-item active"><span class="page-link">{{ calls.number }} of {{ calls.paginator.num_pages }}</span></li>
                    {% if calls.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ calls.next_page_number }}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
        {% endif %}
    </div>
</div>

<!-- Call Notes Modal -->
<div class="modal fade" id="callNotesModal" tabindex="-1" aria-labelledby="callNotesModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content border-0 shadow">
      <form id="completeCallForm" method="POST">
        {% csrf_token %}
        <div class="modal-header bg-success text-white">
          <h5 class="modal-title" id="callNotesModalLabel">Call Notes for <span id="modalVoterName"></span></h5>
          <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <div class="mb-3">
            <label for="call_notes" class="form-label fw-bold">Call Results & Notes</label>
            <textarea class="form-control" id="call_notes" name="call_notes" rows="4" placeholder="Enter notes from the call..."></textarea>
            <div class="form-text">These notes will be saved to the voter's interaction history.</div>
          </div>
        </div>
        <div class="modal-footer bg-light">
          <button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-success px-4">Save Call & Complete</button>
        </div>
      </form>
    </div>
  </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const callNotesModal = document.getElementById('callNotesModal');
        if (callNotesModal) {
            callNotesModal.addEventListener('show.bs.modal', function(event) {
                const button = event.relatedTarget;
                const callId = button.getAttribute('data-call-id');
                const voterName = button.getAttribute('data-voter-name');
                
                const modalVoterName = callNotesModal.querySelector('#modalVoterName');
                const form = callNotesModal.querySelector('#completeCallForm');
                
                modalVoterName.textContent = voterName;
                // Construct the URL dynamically. The placeholder '0' will be replaced by the actual callId.
                let baseUrl = "{% url 'complete_call' 0 %}";
                form.action = baseUrl.replace('0', callId);
                
                // Clear the notes textarea each time the modal opens
                callNotesModal.querySelector('#call_notes').value = '';
            });
            
            // Focus on the textarea when the modal is shown
            callNotesModal.addEventListener('shown.bs.modal', function () {
                callNotesModal.querySelector('#call_notes').focus();
            });
        }
    });
</script>
{% endblock %}