{% extends 'buybackprogram/base.html' %}
{% load i18n %}
{% load humanize %}
{% load django_bootstrap5 %}
{% load static %}
{% load price_formats %}
{% load help_icons %}

{% block details %}

{% if buyback_data %}

{% include "buybackprogram/partials/calculate/calculation_success.html" %}

{% include "buybackprogram/partials/calculate/contract_settings.html" %}

<div class="card" style="margin-top:10px;">
    <div class="card-header"><h5>{% trans "Invoice" %} <i class="fas fa-light fa-question" title="{% help 'invoice' %}"></i></h5></div>
    <div class="card-body">
        {% include "buybackprogram/partials/calculate/invoice.html" %}
    </div>
</div>

<div class="card" style="margin-top:10px;">
    <div class="card-header"><h5>{% trans "Item Details" %} <i class="fas fa-light fa-question" title="{% help 'item_details' %}"></i></h5></div>
    <div class="card-body">
        {% include "buybackprogram/partials/calculate/item_details.html" %}
    </div>
</div>

{% endif %}

<div class="card" style="margin-top:10px;">
    <h5 class="card-header">{% trans "Contract calculator" %}</h5>
    <div class="card-body">
        <form class="form" action="{% url 'buybackprogram:program_calculate' program_pk=program.id %}" method="POST">
            {% csrf_token %}
            {% bootstrap_form form %}
            <button type="submit" class="btn btn-primary">{% trans "Calculate" %}</button>
        </form>
    </div>
    <div class="card-footer text-muted">
        <b>{% trans "Selected Program:" %} </b>{% if program.name %}{{program.name}},{% endif %} managed by {% if program.is_corporation %}{{ program.owner.corporation }}{% else %}{{ program.owner.character.character }}{% endif %}
    </div>
</div>

{% endblock %}

{% block extra_javascript %}
<script>
function copyText(elementId) {
    const text = document.getElementById(elementId).innerText.trim();
    navigator.clipboard.writeText(text).catch(() => {
        const tmp = document.createElement('textarea');
        tmp.value = text;
        tmp.style.position = 'absolute';
        tmp.style.left = '-9999px';
        document.body.appendChild(tmp);
        tmp.select();
        document.execCommand('copy');
        document.body.removeChild(tmp);
    });
}

document.querySelectorAll('button[data-type="copy"]')
  .forEach(function(button){
      button.addEventListener('click', function(){
          let val = this.parentNode.parentNode
              .querySelector('td[data-type="copy_value"]')
              .innerText;
          navigator.clipboard.writeText(val).catch(() => {
              const tmp = document.createElement('textarea');
              tmp.value = val;
              tmp.style.position = 'absolute';
              tmp.style.left = '-9999px';
              document.body.appendChild(tmp);
              tmp.select();
              document.execCommand('copy');
              document.body.removeChild(tmp);
          });
      });
  });
</script>
{% endblock %}

{% block extra_css %}
{% include "buybackprogram/bundles/buybackprogram-css-light.html" %}
{% endblock %}

{% block extra_script %}
{% endblock %}
