{% extends "base.html" %}
{% load static %}
{% block title %}Service 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 Management</div>
    <a href="{% url 'service-create' %}" class="btn btn-success ms-auto">Add Service</a>
</div>

<div class="row">
    <hr>
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table id="service_management" class="table table-striped table-bordered table-hover" style="width:100%">
                    <thead>
                        <tr>
                            <th>Service Name</th>
                            <th>Description</th>
                            <th>Image</th>
                            <th>Status</th>
                            <th>Created</th>
                            <th>Conditions</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for service in services %}
                        <tr>
                            <td>{{ service.service_name }}</td>
                            <td>{{ service.description|striptags|truncatechars:80 }}</td>
                            <td>
                                {% if service.image %}
                                    <img src="{{ service.image.url }}" alt="{{ service.service_name }}" width="60" style="object-fit:cover;max-height:60px;max-width:100px;border-radius:4px;">
                                {% endif %}
                            </td>
                            <td>
                                {% if service.status == 'active' %}
                                    <span class="badge bg-success">Active</span>
                                {% else %}
                                    <span class="badge bg-secondary">Inactive</span>
                                {% endif %}
                            </td>
                            <td>{{ service.created_at|date:"M d, Y H:i" }}</td>
                            <td>
                                {% if service.conditions %}
                                    <ul class="mb-0 ps-3">
                                        {% for cond in service.conditions %}
                                            <li><strong>{{ cond.key }}</strong>: {{ cond.value }}</li>
                                        {% endfor %}
                                    </ul>
                                {% endif %}
                            </td>
                            <td>
                                <div class="d-flex gap-1">
                                <a href="{% url 'service-view' service.id %}" class="btn btn-info btn-sm" title="View">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="{% url 'service-edit' service.id %}" class="btn btn-warning btn-sm" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form method="post" action="{% url 'service-delete' service.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 service?');">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div> <!-- Close card-body -->
    </div> <!-- Close card -->
</div> <!-- Close row -->
{% 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_management').DataTable();
        });
    </script>
{% endblock %}
