{% extends "layouts/base.html" %}
{% load i18n %}

{% block title %}{% trans "Acknowledgement Checklist Items" %}{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Page Header -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h1 class="h3 mb-2">
                        <i class="bi bi-list-check me-2"></i>
                        {% trans "Checklist Items Management" %}
                    </h1>
                    <p class="text-muted mb-0">
                        {% trans "Manage acknowledgement checklist items" %}
                    </p>
                </div>
                <button type="button" class="btn btn-primary">
                    <i class="bi bi-plus me-2"></i>
                    {% trans "Add Checklist Item" %}
                </button>
            </div>
        </div>
    </div>

    <!-- Checklist Items List -->
    <div class="card shadow-sm">
        <div class="card-header bg-white py-3">
            <div class="d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="bi bi-list me-2"></i>
                    {% trans "Checklist Items" %}
                </h5>
                <div class="input-group" style="max-width: 300px;">
                    <input type="text" class="form-control" id="searchInput" placeholder="{% trans 'Search items...' %}">
                    <button class="btn btn-outline-secondary" type="button">
                        <i class="bi bi-search"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0 align-middle" id="itemsTable">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Item Text" %}</th>
                            <th>{% trans "Role" %}</th>
                            <th>{% trans "Linked Content" %}</th>
                            <th>{% trans "Required" %}</th>
                            <th>{% trans "Order" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in checklist_items %}
                        <tr>
                            <td>
                                <strong>{{ item.text_en }}</strong>
                                {% if item.code %}
                                    <span class="badge bg-light text-muted ms-2">{{ item.code }}</span>
                                {% endif %}
                                {% if item.description_en %}
                                    <p class="small text-muted mb-0 mt-1">{{ item.description_en }}</p>
                                {% endif %}
                            </td>
                            <td>
                                {% if item.role %}
                                    <span class="badge bg-info text-dark">
                                        {{ item.get_role_display }}
                                    </span>
                                {% else %}
                                    <span class="badge bg-secondary">{% trans "All Roles" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if item.content %}
                                    <span class="badge bg-light text-dark">
                                        {{ item.content.title_en }}
                                    </span>
                                {% else %}
                                    <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td class="text-center">
                                {% if item.is_required %}
                                    <span class="badge bg-danger">
                                        <i class="bi bi-exclamation-circle me-1"></i>
                                        {% trans "Yes" %}
                                    </span>
                                {% else %}
                                    <span class="badge bg-secondary">{% trans "No" %}</span>
                                {% endif %}
                            </td>
                            <td>{{ item.order }}</td>
                            <td>
                                {% if item.is_active %}
                                    <span class="badge bg-success">
                                        <i class="bi bi-check me-1"></i>
                                        {% trans "Active" %}
                                    </span>
                                {% else %}
                                    <span class="badge bg-secondary">
                                        <i class="bi bi-x me-1"></i>
                                        {% trans "Inactive" %}
                                    </span>
                                {% endif %}
                            </td>
                            <td>
                                <small>{{ item.created_at|date:"M d, Y" }}</small>
                            </td>
                            <td>
                                <div class="btn-group" role="group">
                                    <button class="btn btn-sm btn-outline-primary" title="{% trans 'Edit' %}">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    <button class="btn btn-sm btn-outline-danger" title="{% trans 'Delete' %}">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <i class="bi bi-clipboard-data fa-3x text-muted mb-3"></i>
                                <p class="text-muted mb-0">
                                    {% trans "No checklist items found" %}
                                </p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
// Search functionality
document.getElementById('searchInput').addEventListener('keyup', function() {
    const searchValue = this.value.toLowerCase();
    const table = document.getElementById('itemsTable');
    const rows = table.getElementsByTagName('tr');
    
    for (let i = 1; i < rows.length; i++) {
        const row = rows[i];
        const cells = row.getElementsByTagName('td');
        let found = false;
        
        for (let j = 0; j < cells.length; j++) {
            const cellText = cells[j].textContent.toLowerCase();
            if (cellText.includes(searchValue)) {
                found = true;
                break;
            }
        }
        
        row.style.display = found ? '' : 'none';
    }
});
</script>
{% endblock %}
