{% extends "base.html" %}
{% load static %}
{% block title %}Service Request Management{% 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-1 justify-content-between">
    <div class="breadcrumb-title pe-3">Service Request Management</div>
</div>

<div class="row">
    <hr>
    <div class="card">
        <div class="card-body">
            <form method="get" class="row g-2 mb-3">
                <div class="col-md-3">
                    <input type="search" name="q" value="{{ query|default:'' }}" class="form-control" placeholder="Search request id or mobile">
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-select">
                        <option value="">All Status</option>
                        {% for code, label in status_choices %}
                            <option value="{{ code }}" {% if status == code %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_from" value="{{ date_from|default:'' }}" class="form-control" placeholder="From">
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_to" value="{{ date_to|default:'' }}" class="form-control" placeholder="To">
                </div>
                <div class="col-md-1">
                    <button class="btn btn-primary">Filter</button>
                </div>
            </form>
            <div class="table-responsive">
                <table id="service_request_table" class="table table-striped table-bordered table-hover" style="width:100%">
                <thead>
                    <tr>
                        <th>Request ID</th>
                        <th>User</th>
                        <th>Category</th>
                        <th>Service Date</th>
                        <th>Status</th>
                        <th>Total Price</th>
                        <th>Created</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for req in requests %}
                    <tr>
                        <td>{{ req.request_id }}</td>
                        <td>
                            {{ req.contact_name|default:req.user.get_full_name|default:req.user.username }}<br>
                            <small class="text-muted">{{ req.contact_mobile|default:"" }}</small>
                        </td>
                        <td>{{ req.category.name }}</td>
                        <td>{{ req.service_date }} {{ req.service_time }}</td>
                        <td>
                            {% if req.status == 'pending' %}
                                <span class="badge bg-warning text-dark">Pending</span>
                            {% elif req.status == 'confirmed' %}
                                <span class="badge bg-info text-dark">Confirmed</span>
                            {% elif req.status == 'completed' %}
                                <span class="badge bg-success">Completed</span>
                            {% elif req.status == 'cancelled' %}
                                <span class="badge bg-danger">Cancelled</span>
                            {% else %}
                                <span class="badge bg-secondary">{{ req.get_status_display }}</span>
                            {% endif %}
                        </td>
                        <td>{{ req.total_price }}</td>
                        <td>{{ req.created_at|date:"M d, Y H:i" }}</td>
                        <td>
                            <div class="d-flex gap-1">
                                <a href="{% url 'service-request-detail' req.id %}" class="btn btn-info btn-sm" title="View">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="{% url 'service-request-edit' req.id %}" class="btn btn-primary btn-sm" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form method="post" action="{% url 'service-request-delete' req.id %}" style="display:inline;">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-danger btn-sm" title="Delete" onclick="return confirm('Are you sure you want to delete this request?');">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</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() {
            $('#service_request_table').DataTable({
                "order": [[ 6, "desc" ]],
                "searching": false,
                "paging": false,

            });
        });
    </script>
{% endblock %}
