{% load widget_tweaks %}
{% load linkornone from filters %}
<div class="card mb-2">
  <div class="card-header">
    Purchase Details
  </div>
  <div class="card-body">
    {% if create or edit or duplicate %}
    <div class="form-group" id="purchased-from-group">
      <label for="{{ form.purchased_from.id_for_label }}">Supplier</label>
      <div class="row">
          <div class="col">
              <select id="{{ form.purchased_from.id_for_label }}" name="{{ form.purchased_from.name }}" class="selectpicker" data-live-search="true" data-sourceurl="{% url 'api_secure' model='supplier' %}">
                {% if object.purchased_from %}
                <option value="{{form.purchased_from.value}}" selected="selected" data-update_url="{% url 'supplier_update' form.purchased_from.value %}">{{ object.purchased_from }}</option>
                {% endif %}
              </select>
          </div>
          <div class="col align-right">
            <div class="btn-group">
                <a href="{% url 'supplier_create' %}" class="btn btn-success modal-href"
                   data-target="#{{ form.purchased_from.id_for_label }}">
                    <span class="fas fa-plus"></span>
                </a>
                <a {% if form.supplier.value %}href="{% url 'supplier_update' form.purchased_from.value %}"{% endif %} class="btn btn-warning modal-href" id="{{ form.purchased_from.id_for_label }}-update" data-target="#{{ form.purchased_from.id_for_label }}">
                    <span class="fas fa-edit"></span>
                </a>
            </div>
          </div>
      </div>
    </div>

    <div class="form-group">
      <label for="{{ form.purchase_price.id_for_label }}">Purchase Price</label>
      <div class="input-group">
        <div class="input-group-prepend"><span class="input-group-text">£</span></div>
        {% render_field form.purchase_price|add_class:'form-control'|set_data:"toggle:tooltip" value=object.purchase_price title="Ex. VAT" %}
      </div>
    </div>

    <div class="form-group">
      <label for="{{ form.salvage_value.id_for_label }}">Replacement Cost</label>
      <div class="input-group">
        <div class="input-group-prepend"><span class="input-group-text">£</span></div>
        {% render_field form.replacement_cost|add_class:'form-control'|set_data:"toggle:tooltip" value=object.replacement_cost title="Ex. VAT" %}
      </div>
    </div>

    <div class="form-group">
      <label for="{{ form.date_acquired.id_for_label }}" >Date Acquired</label>
      {% if object.date_acquired%}
      {% with date_acq=object.date_acquired|date:"Y-m-d" %}
      {% render_field form.date_acquired|add_class:'form-control'|attr:'type="date"' value=date_acq %}
      {% endwith %}
      {% else %}
      <input type="date" name="date_acquired" value="{% now 'Y-m-d' %}"
                    class="form-control" id="id_date_acquired">
                {% endif %}
              </div>

      <div class="form-group">
        <label for="{{ form.date_sold.id_for_label }}">Date Sold</label>
        {% with date_sol=object.form.date_sold|date:"Y-m-d" %}
        {% render_field form.date_sold|add_class:'form-control'|attr:'type="date"' value=date_sol %}
        {% endwith %}
      </div>
      {% else %}
      <dl>
        <dt>Purchased From</dt>
        <dd>{% if object.purchased_from %}<a href="{{object.purchased_from.get_absolute_url}}">{{ object.purchased_from }}</a>{%else%}-{%endif%}</dd>
        <dt>Purchase Price</dt>
        <dd>£{{ object.purchase_price|default_if_none:'-' }}</dd>
        <dt>Replacement Cost</dt>
        <dd>£{{ object.replacement_cost|default_if_none:'-' }}</dd>
        <dt>Date Acquired</dt>
        <dd>{{ object.date_acquired|default_if_none:'-' }}</dd>
        {% if object.date_sold %}
        <dt>Date Sold</dt>
        <dd>{{ object.date_sold|default_if_none:'-' }}</dd>
        {% endif %}
      </dl>
      {% endif %}
    </div>
  </div>
