{% extends "base.html" %}

{% block styles %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/dashboard.css') }}">
{% endblock %}

{% block content %}
<h2>Admin Dashboard</h2>
<a href="{{ url_for('admin.add_product') }}" class="btn btn-success">Add New Product</a>
<table class="admin-table">
  <thead>
    <tr>
      <th>Image</th>
      <th>ID</th>
      <th>Name</th>
      <th>Description</th>
      <th>Price</th>
      <th>Stock</th>
      <th>Category</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    {% for p in products %}
    <tr>
      <td>
        <img src="{{ p.get_image_url() }}" alt="{{ p.name }}" style="width: 60px; height: 60px; object-fit: cover;">
      </td>
      <td>{{ p.id }}</td>
      <td>{{ p.name }}</td>
      <td>{{ p.description[:50] }}{% if p.description|length > 50 %}...{% endif %}</td>
      <td>${{ "%.2f"|format(p.price) }}</td>
      <td>
        {% if p.stock > 10 %}
        <span class="stock-high">{{ p.stock }}</span>
        {% elif p.stock > 0 %}
        <span class="stock-low">{{ p.stock }}</span>
        {% else %}
        <span class="stock-out">Out of Stock</span>
        {% endif %}
      </td>
      <td>{{ p.category.name if p.category else 'N/A' }}</td>
      <td class="actions">
        <a href="{{ url_for('admin.edit_product', id=p.id) }}" class="btn btn-sm btn-primary">Edit</a>
        <form method="POST" action="{{ url_for('admin.delete_product', id=p.id) }}" style="display:inline;"
          onsubmit="return confirm('Delete this product?')">
          <button type="submit" class="btn btn-sm btn-danger">Delete</button>
        </form>
      </td>
    </tr>
    {% endfor %}
  </tbody>
</table>
{% endblock %}
