{% extends 'base.html' %}
{% load trade_offer_tags card_badge %}

{% block title %}{{title}}{% endblock title %}

{% block content %}
  <h2 class="text-2xl font-bold">Trade Offer Details</h2>
  <div class="card card-border bg-base-100 shadow-lg mx-auto px-2 py-4 m-2 text-sm">
    {% if acceptance_form %}
    <div class="col-span-2 max-w-3/4 mx-auto card card-border bg-base-200 shadow-lg p-6 mb-8">
      <h3 class="text-xl font-semibold mb-2">Accept A Trade</h3>
      <form method="post" action="{% url 'trade_acceptance_create' offer_pk=object.pk %}">
        {% csrf_token %}
        <input type="hidden" name="next" value="{% url 'trade_offer_detail' pk=object.pk %}">
  
        <div class="mb-4">
          <label for="{{ acceptance_form.offered_card.id_for_label }}" class="block text-sm font-medium mb-1">I have:</label>
          {{ acceptance_form.offered_card }}
          {% for error in acceptance_form.offered_card.errors %}
            <p class="mt-1 text-sm text-red-600">{{ error }}</p>
          {% endfor %}
        </div>
  
        <div class="mb-4">
          <label for="{{ acceptance_form.requested_card.id_for_label }}" class="block text-sm font-medium mb-1">I want:</label>
          {{ acceptance_form.requested_card }}
          {% for error in acceptance_form.requested_card.errors %}
            <p class="mt-1 text-sm text-red-600">{{ error }}</p>
          {% endfor %}
        </div>
  
        <div class="mb-4">
          {% if not acceptance_form.accepted_by.field.widget.is_hidden %}
            <label for="{{ acceptance_form.accepted_by.id_for_label }}" class="block text-sm font-medium">
              Accepted By
            </label>
          {% endif %}
          {{ acceptance_form.accepted_by }}
          {% for error in acceptance_form.accepted_by.errors %}
            <p class="mt-1 text-sm text-red-600">{{ error }}</p>
          {% endfor %}
        </div>
  
        <div class="mt-6 flex justify-end">
          {% if is_initiator %}
            <a href="{{ delete_close_url }}" class="btn btn-danger">Delete/Close Trade Offer</a>
          {% elif request.user.is_authenticated %}
            <button type="submit" class="btn btn-primary">Accept Trade</button>
          {% endif %}
        </div>
      </form>
    </div>
    {% endif %}
    <div class="grid grid-cols-2 justify-items-end items-baseline gap-4">
      <div class="font-semibold text-right xs:text-lg">Cards They Have:
        <div class="flex flex-wrap flex-col gap-2 text-left mt-4 items-center">
          {% for card in object.have_cards_available %}
            {% card_badge card.card card.qty_available %}
          {% endfor %}
        </div>
      </div>
      <div class="font-semibold justify-self-start xs:text-lg">Cards They Want:
        <div class="flex flex-wrap flex-col gap-2 mt-4 items-center">
          {% for card in object.want_cards_available %}
            {% card_badge card.card card.qty_available %}
          {% endfor %}
        </div>
      </div>
    </div>
    <div class="divider"></div>
    <div class="grid grid-cols-2 justify-items-end items-baseline gap-4">
      <div class="font-semibold">Initiator:</div>
      <div class="justify-self-start">{{ object.initiated_by.user.username }}</div>
      <div class="font-semibold">ID:</div>
      <div class="justify-self-start">#{{ object.hash }}</div>
      <div class="font-semibold">Created At:</div>
      <div class="justify-self-start">{{ object.created_at }}</div>
      <div class="font-semibold">Last Updated:</div>
      <div class="justify-self-start">{{ object.updated_at }}</div>
    </div>
    <div class="divider"></div>
    <div class="font-semibold text-center sm:text-lg mb-2">Trade History:</div>
    <div class="flex flex-row flex-wrap justify-start items-baseline gap-y-2 w-65 xs:w-88 sm:w-90 lg:w-181 mx-auto">
      {% for trade in object.acceptances.all %}
        <div class="grid grid-cols-2 justify-items-between items-baseline w-65 xs:w-88 sm:w-90 lg:w-181">
          <div class="">{% card_badge trade.offered_card %}</div>
          <div class="">{% card_badge trade.requested_card %}</div>
          <div class="col-span-2 text-center mt-1">{{ trade.accepted_by.user.username }} &bull; <a class="link link-hover" href="{% url 'trade_acceptance_update' pk=trade.pk %}">#{{ trade.hash }}</a> &bull; {{ trade.get_state_display }}</div>
        </div>
      {% empty %}
        <div class="text-center justify-center w-full text-center">No trades yet.</div>
      {% endfor %}
    </div>
  </div>
{% endblock content %} 