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

<div class="row">
    <hr>
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table id="product_management" class="table table-striped table-bordered table-hover" style="width:100%">
                    <thead>
                        <tr>
                            <th>Product Name</th>
                            <th>Category</th>
                            <th>Price</th>
                            <th>Unit</th>
                            <th>Description</th>
                            <th>In Trash</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for product in products %}
                        <tr>
                            <td>{{ product.product_name }}</td>
                            <td>{% if product.category %}{{ product.category.name }}{% endif %}</td>
                            <td>{{ product.price }}</td>
                            <td>{{ product.unit }}</td>
                            <td>{{ product.description|striptags|truncatechars:80 }}</td>
                            <td>
                                {% if product.trash %}
                                    Yes
                                {% else %}
                                    No
                                {% endif %}
                            </td>

                            <td>
                                <div class="d-flex gap-1">
                                <a href="{% url 'product-view' product.id %}" class="btn btn-info btn-sm" title="View">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="{% url 'product-edit' product.id %}" class="btn btn-warning btn-sm" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form method="post" action="{% url 'product-delete' product.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 product?');">
                                        <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() {
            $('#product_management').DataTable();
        });
    </script>
{% endblock %}
