{% extends "base.html" %}
{% load static %}
{% block title %}Category 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">Category Management</div>
    <a href="{% url 'category-create' %}" class="btn btn-success ms-auto">Add Category</a>
</div>
                            <td>{{ category.id }}</td>
                            <td>{{ category.name }}</td>
                            <td>{% if category.parent %}{{ category.parent.name|default:category.parent.id }}{% endif %}</td>
                            <td>{{ category.description|striptags|truncatechars:80 }}</td>
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table id="category_management" class="table table-striped table-bordered table-hover" style="width:100%">
                    <thead>
                        <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Parent</th>
                                <th>Description</th>
                                <th>Image</th>
                                <th>Status</th>
                                <th> In Trash </th>
                                <th>Created</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for category in categories %}
                        <tr>
                            <td>{{ category.id }}</td>
                            <td>{{ category.name }}</td>
                            <td>{% if category.parent %}{{ category.parent.name }}{% endif %}</td>
                            <td>{{ category.description|striptags|truncatechars:80 }}</td>
                            <td>
                                {% if category.image %}
                                    <img src="{{ category.image.url }}" alt="Category {{ category.id }}" width="60" style="object-fit:cover;max-height:60px;max-width:100px;border-radius:4px;">
                                {% endif %}
                            </td>
                            <td>
                                {% if category.status == 'active' %}
                                    <span class="badge bg-success">Active</span>
                                {% else %}
                                    <span class="badge bg-secondary">Inactive</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if category.trash %}
                                    Yes
                                {% else %}
                                    No
                                {% endif %}
                            </td>
                            <td>{{ category.created_at|date:"M d, Y H:i" }}</td>
                            <td>
                                <div class="d-flex gap-1">
                                <a href="{% url 'category-view' category.id %}" class="btn btn-info btn-sm" title="View">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="{% url 'category-edit' category.id %}" class="btn btn-warning btn-sm" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form method="post" action="{% url 'category-delete' category.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 category?');">
                                        <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() {
            $('#category_management').DataTable();
        });
    </script>
{% endblock %}
