{% from "components/icon.html" import Icon %}

{% macro UploadInput(field, portfolio_id, show_label=False) -%}
<uploadinput
  inline-template
  {% if not field.errors %}
    v-bind:filename='{{ field.filename.data | tojson }}'
    v-bind:object-name='{{ field.object_name.data | tojson }}'
  {% else %}
    v-bind:initial-errors='true'
  {% endif %}
  v-bind:portfolio-id="'{{ portfolio_id }}'"
  name='{{ field.name }}'
  >
  <div>
    <div v-show="valid" class="uploaded-file">
      {{ Icon("ok") }}
      <a class="uploaded-file__name" v-html="baseName" v-bind:href="downloadLink"></a>
      <a href="#" class="uploaded-file__remove" v-on:click="removeAttachment">Remove</a>
    </div>
    <div v-show="valid === false" v-bind:class='{ "usa-input": true, "usa-input--error": showErrors }'>
      {% if show_label %}
        {{ field.label }}
      {% endif %}
      <p>
        {{ field.description }}
      </p>
      <div v-if="!hideInput" class="upload-widget">
        <label class="upload-label" :for="name">
          <span class="upload-button">
            Browse
          </span>
        </label>
        <input
          v-on:change="addAttachment"
          ref="attachmentInput"
          accept="{{ field.accept }}"
          :id="name"
          :name="name"
          aria-label="Task Order Upload"
          v-bind:value="attachment"
          type="file">
          <input type="hidden" name="{{ field.filename.name }}" id="{{ field.filename.name }}" ref="attachmentFilename">
          <input type="hidden" name="{{ field.object_name.name }}" id="{{ field.object_name.name }}" ref="attachmentObjectName">
      </div>
      <template v-if="uploadError">
        <span class="usa-input__message">{{ "forms.task_order.upload_error" | translate }}</span>
      </template>
      <template v-if="sizeError">
        <span class="usa-input__message">{{ "forms.task_order.size_error" | translate }}</span>
      </template>
      {% for error, error_messages in field.errors.items() %}
        <span class="usa-input__message">{{error_messages[0]}}</span>
      {% endfor %}
    </div>
  </div>
</uploadinput>
{%- endmacro %}
