{% extends "base.html" %}
{% load static %}
{% block title %}Back to Base{% endblock %}

{% block extra_css %}
    <link href="{% static 'assets/plugins/datatable/css/dataTables.bootstrap5.min.css' %}" rel="stylesheet" />
{% endblock %}

{% block content %}
<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
    <div class="breadcrumb-title pe-3">Back to Base</div>
    <div class="ms-auto">
        <a href="{% url 'back-to-base-create' %}" class="btn btn-primary">Add Entry</a>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <ul class="nav nav-tabs mb-3">
            <li class="nav-item">
                <a class="nav-link {% if status_filter == 'active' %}active{% endif %}" href="?status=active">Active</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if status_filter == 'resolved' %}active{% endif %}" href="?status=resolved">Resolved (History)</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if status_filter == 'all' %}active{% endif %}" href="?status=all">All</a>
            </li>
        </ul>

        <form method="get" class="row g-2 mb-3">
            <input type="hidden" name="status" value="{{ status_filter }}">
            <div class="col-md-4">
                <input type="search" name="q" value="{{ query|default:'' }}" class="form-control" placeholder="Search Booking ID, Customer Name/Mobile">
            </div>
            <div class="col-md-2">
                <input type="date" name="start_date" value="{{ start_date|default:'' }}" class="form-control" placeholder="Start Date">
            </div>
            <div class="col-md-2">
                <input type="date" name="end_date" value="{{ end_date|default:'' }}" class="form-control" placeholder="End Date">
            </div>
            <div class="col-md-2">
                <button class="btn btn-primary">Search</button>
            </div>
        </form>

        <div class="table-responsive">
            <table id="back_to_base_table" class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>Booking ID</th>
                        <th>Customer</th>
                        <th>Rider</th>
                        <th>POC Name</th>
                        <th>POC Mobile</th>
                        <th>Reason</th>
                        <th>Date Added</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in page_obj %}
                    <tr>
                        <td>
                            <a href="{% url 'booking-view' item.booking.id %}">{{ item.booking.booking_id }}</a>
                        </td>
                        <td>
                            {{ item.customer.first_name }} {{ item.customer.last_name }}<br>
                            <small class="text-muted">{{ item.customer.mobile }}</small>
                        </td>
                        <td>
                            {% if item.rider %}
                                {{ item.rider.full_name }}
                            {% else %}
                                <span class="text-muted">Unassigned</span>
                            {% endif %}
                        </td>
                        <td>{{ item.poc_name|default:"-" }}</td>
                        <td>{{ item.poc_mobile|default:"-" }}</td>
                        <td>{{ item.reason|default:"-"|truncatechars:30 }}</td>
                        <td>{{ item.created_at|date:"d M Y H:i" }}</td>
                        <td>
                            <div class="d-flex gap-2">
                                <a href="{% url 'back-to-base-edit' item.id %}" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-pencil"></i> Edit
                                </a>
                                {% if not item.is_resolved %}
                                <form action="{% url 'back-to-base-resolve' item.id %}" method="post" onsubmit="return confirm('Are you sure you want to resolve this entry?');">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-sm btn-outline-success">
                                        <i class="bi bi-check-circle"></i> Resolve
                                    </button>
                                </form>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-center">No entries found.</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        {% if page_obj.has_other_pages %}
        <nav aria-label="Page navigation">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}&q={{ query|default:'' }}&status={{ status_filter }}&start_date={{ start_date|default:'' }}&end_date={{ end_date|default:'' }}">Previous</a>
                </li>
                {% endif %}
                
                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li class="page-item"><a class="page-link" href="?page={{ num }}&q={{ query|default:'' }}&status={{ status_filter }}&start_date={{ start_date|default:'' }}&end_date={{ end_date|default:'' }}">{{ num }}</a></li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}&q={{ query|default:'' }}&status={{ status_filter }}&start_date={{ start_date|default:'' }}&end_date={{ end_date|default:'' }}">Next</a>
                </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block extra_js %}
    <script src="{% static 'assets/plugins/datatable/js/jquery.dataTables.min.js' %}"></script>
    <script src="{% static 'assets/plugins/datatable/js/dataTables.bootstrap5.min.js' %}"></script>
    
    <script>
        $(document).ready(function() {
            $('#back_to_base_table').DataTable({
                searching: false,
                info: false,
                dom: 'Bfrtip',
                buttons: [
                    { extend: 'csv', exportOptions: { columns: ':not(:last-child)' } },
                    { extend: 'pdf', exportOptions: { columns: ':not(:last-child)' } },
                ]
            });
        });
    </script>
{% endblock %}
