{% extends "shop/base.html" %}
{% load static %}

{% block title %}
  Koszyk na zakupy
{% endblock %}

{% block content %}
  <h1>Koszyk na zakupy</h1>
  <table class="cart">
    <thead>
      <tr>
        <th>Zdjęcie</th>
        <th>Produkt</th>
        <th>Ilość</th>
        <th>Usuń</th>
        <th>Cena jednostkowa</th>
        <th>Cena</th>
      </tr>
    </thead>
    <tbody>
      {% for item in cart %}
        {% with product=item.product %}
          <tr>
            <td>
              <a href="{{ product.get_absolute_url }}">
                <img src="{% if product.image %}{{ product.image.url }}
                {% else %}{% static "img/no_image.png" %}{% endif %}">
              </a>
            </td>
            <td>{{ product.name }}</td>
            <td>
                <form action="{% url "cart:cart_add" product.id %}" method="post">
                  {{ item.update_quantity_form.quantity }}
                  {{ item.update_quantity_form.update }}
                  <input type="submit" value="Zmień">
                  {% csrf_token %}
                </form>
             </td>
            <td> 
              <form action="{% url "cart:cart_remove" product.id %}" method="post"> 
                <input type="submit" value="Usuń">
                {% csrf_token %}
              </form>
            </td>
            <td class="num">{{ item.price }} zł</td>
            <td class="num">{{ item.total_price }} zł</td>
          </tr>
        {% endwith %}
      {% endfor %}
      {% if cart.coupon %}
      <tr class="subtotal">
        <td>Suma częściowa</td>
        <td colspan="4"></td>
        <td class="num">{{ cart.get_total_price|floatformat:2 }} zł</td>
      </tr>
      <tr>
        <td>
          Kupon "{{ cart.coupon.code }}"
          ({{ cart.coupon.discount }}% rabatu)
        </td>
        <td colspan="4"></td>
        <td class="num neg">
          - {{ cart.get_discount|floatformat:"2" }} zł
        </td>
      </tr>
    {% endif %}
    <tr class="total">
      <td>Razem</td>
      <td colspan="4"></td>
      <td class="num">
        {{ cart.get_total_price_after_discount|floatformat:"2" }} zł
      </td>
    </tr>
    
    </tbody>
  </table>
  {% if recommended_products %}
  <div class="recommendations cart">
    <h3>Z tym produktem klienci kupowali również:</h3>
    {% for p in recommended_products %}
      <div class="item">
        <a href="{{ p.get_absolute_url }}">
          <img src="{% if p.image %}{{ p.image.url }}{% else %}
          {% static "img/no_image.png" %}{% endif %}">
        </a>
        <p><a href="{{ p.get_absolute_url }}">{{ p.name }}</a></p>
      </div>
    {% endfor %}
  </div>
{% endif %}

  <p>Kupon rabatowy:</p>
 <form action="{% url "coupons:apply" %}" method="post">
  {{ coupon_apply_form }}
  <input type="submit" value="Wprowadź">
  {% csrf_token %}
</form>

  <p class="text-right"> 
    <a href="{% url "shop:product_list" %}" class="button 
    light">Kontynuuj zakupy</a> 
    <a href="{% url "orders:order_create" %}" class="button">
        Do kasy
    </a>
  </p>
{% endblock %}
