<span class="fas fa-circle-notch fa-spin" ng-show="!document && isEdit()"></span>

<div ng-show="document || !isEdit()">
  <form name="documentForm" class="form-horizontal">
    <div class="pull-right btn-group" ng-init="form = documentForm">
      <button type="submit" class="btn btn-primary" ng-disabled="!documentForm.$valid || fileIsUploading" ng-click="edit()">
        <span class="fas fa-save"></span>
        {{ 'save' | translate }}
      </button>
    </div>
    <h1 class="page-header">{{ document.title }}&nbsp;</h1>

    <div uib-alert ng-class="'alert-' + alert.type" ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">{{ alert.msg }}</div>

    <div class="row" ng-show="fileIsUploading">
      <h4>{{ 'document.edit.uploading_files' | translate }}</h4>
      <div class="col-md-6"><uib-progressbar value="fileProgress" class="progress-info active"></uib-progressbar></div>
    </div>

    <fieldset>
      <legend>{{ 'document.edit.primary_metadata' | translate }}</legend>
      <div class="form-group" ng-class="{ 'has-error': !documentForm.title.$valid && documentForm.$dirty }">
        <label class="col-sm-2 control-label" for="inputTitle">{{ 'document.title' | translate }}</label>
        <div class="col-sm-10">
          <input required ng-maxlength="100" class="form-control" type="text" id="inputTitle"
            ng-attr-placeholder="{{ 'document.edit.title_placeholder' | translate }}" name="title" ng-model="document.title" autocomplete="off"
            uib-typeahead="document for document in getTitleTypeahead($viewValue)"
            typeahead-wait-ms="200" ng-disabled="fileIsUploading" />
        </div>
      </div>
      <div class="form-group" ng-class="{ 'has-error': !documentForm.description.$valid }">
        <label class="col-sm-2 control-label" for="inputDescription">{{ 'document.description' | translate }}</label>
        <div class="col-sm-10">
          <pell-editor name="description" id="inputDescription"
                       ng-maxlength="4000" ng-model="document.description" ng-disabled="fileIsUploading"></pell-editor>
          <p class="help-block" ng-show="documentForm.description.$error.maxlength && documentForm.$dirty">{{ 'validation.too_long' | translate }}</p>
        </div>
      </div>
      <div class="form-group" ng-class="{ 'has-error': !documentForm.createDate.$valid }">
        <label class="col-sm-2 control-label" for="inputCreateDate">{{ 'document.creation_date' | translate }}</label>
        <div class="col-sm-10">
          <div class="input-group">
            <input type="text" id="inputCreateDate" name="createDate"
                   current-text="{{ 'directive.datepicker.current' | translate }}"
                   clear-text="{{ 'directive.datepicker.clear' | translate }}"
                   close-text="{{ 'directive.datepicker.close' | translate }}"
                   uib-datepicker-popup="{{ dateFormat }}" class="form-control"
                   ng-model="document.create_date" datepicker-options="{ startingDay: 1, showWeeks: false }"
                   is-open="datepickerOpened" ng-disabled="fileIsUploading" />
            <span class="input-group-btn">
              <button type="button" class="btn btn-default" ng-click="datepickerOpened = true" ng-disabled="fileIsUploading"><i class="fas fa-calendar"></i></button>
            </span>
          </div>
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputLanguage">{{ 'document.language' | translate }}</label>
        <div class="col-sm-10">
          <select class="form-control" id="inputLanguage" ng-model="document.language" ng-disabled="fileIsUploading">
            <option ng-repeat="language in acceptedLanguages" value="{{ language.key }}">{{ language.label }}</option>
          </select>
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputFiles">{{ 'document.edit.new_files' | translate }}</label>
        <div class="col-sm-10">
          <input type="file" ngf-select class="form-control" id="inputFiles" ngf-multiple="true" ng-model="newFiles" ng-disabled="fileIsUploading"></input>
        </div>
        <div class="col-sm-4" ng-if="orphanFiles.length > 0"
             translate="document.edit.orphan_files"
             translate-values="{ count: orphanFiles.length }">
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-2 control-label" for="inputTags">{{ 'document.tags' | translate }}</label>
        <div class="col-sm-10">
          <select-tag tags="document.tags" ref="inputTags" ng-disabled="fileIsUploading"></select-tag>
        </div>
      </div>

      <!-- Custom metadata -->
      <div class="form-group"
           ng ng-repeat="meta in document.metadata"
           ng-class="{ 'has-error': !documentForm[meta.id].$valid && documentForm.$dirty }">
        <label class="col-sm-2 control-label" for="inputTitle">{{ meta.name }}</label>
        <div class="col-sm-10">
          <input ng-if="meta.type == 'STRING'"
                 ng-maxlength="4000" class="form-control" type="text" id="input{{ meta.id }}"
                 name="{{ meta.id }}" ng-model="meta.value" autocomplete="off"
                 ng-disabled="fileIsUploading" />

          <div class="input-group" ng-if="meta.type == 'DATE'">
            <input type="text" id="input{{ meta.id }}" name="{{ meta.id }}"
                   current-text="{{ 'directive.datepicker.current' | translate }}"
                   clear-text="{{ 'directive.datepicker.clear' | translate }}"
                   close-text="{{ 'directive.datepicker.close' | translate }}"
                   uib-datepicker-popup="{{ dateFormat }}" class="form-control"
                   ng-model="meta.value" datepicker-options="{ startingDay: 1, showWeeks: false }"
                   is-open="datepickerOpenedMeta[meta.id]" ng-disabled="fileIsUploading" />
            <span class="input-group-btn">
              <button type="button" class="btn btn-default" ng-click="datepickerOpenedMeta[meta.id] = true" ng-disabled="fileIsUploading"><i class="fas fa-calendar"></i></button>
            </span>
          </div>

          <input ng-if="meta.type == 'INTEGER'"
                 ng-pattern="/^[0-9]*$/" class="form-control" type="text" id="input{{ meta.id }}"
                 name="{{ meta.id }}" ng-model="meta.value" autocomplete="off"
                 ng-disabled="fileIsUploading" />

          <input ng-if="meta.type == 'FLOAT'"
                 ng-pattern="/^-?[0-9]*\.?[0-9]*$/" class="form-control" type="text" id="input{{ meta.id }}"
                 name="{{ meta.id }}" ng-model="meta.value" autocomplete="off"
                 ng-disabled="fileIsUploading" />

          <input type="checkbox" ng-if="meta.type == 'BOOLEAN'"
                 id="input{{ meta.id }}" name="{{ meta.id }}"
                 ng-model="meta.value" ng-disabled="fileIsUploading" />
        </div>
      </div>
    </fieldset>

    <fieldset ng-init="additionalMetadataCollapsed = true">
      <legend class="pointer" ng-click="additionalMetadataCollapsed = !additionalMetadataCollapsed">
        {{ 'document.edit.additional_metadata' | translate }}
        <span class="pull-right fas fa-plus"></span>
      </legend>
      <div ng-show="!additionalMetadataCollapsed">
        <div class="form-group" ng-class="{ 'has-error': !documentForm.subject.$valid }">
          <label class="col-sm-2 control-label" for="inputSubject">{{ 'document.subject' | translate }}</label>
          <div class="col-sm-10">
            <input ng-maxlength="500" class="form-control" type="text" id="inputSubject"
                   ng-attr-placeholder="{{ 'document.edit.subject_placeholder' | translate }}" name="subject" ng-model="document.subject" ng-disabled="fileIsUploading" />
          </div>
        </div>
        <div class="form-group" ng-class="{ 'has-error': !documentForm.identifier.$valid }">
          <label class="col-sm-2 control-label" for="inputIdentifier">{{ 'document.identifier' | translate }}</label>
          <div class="col-sm-10">
            <input ng-maxlength="500" class="form-control" type="text" id="inputIdentifier"
                   ng-attr-placeholder="{{ 'document.edit.identifier_placeholder' | translate }}" name="identifier" ng-model="document.identifier" ng-disabled="fileIsUploading" />
          </div>
        </div>
        <div class="form-group" ng-class="{ 'has-error': !documentForm.publisher.$valid }">
          <label class="col-sm-2 control-label" for="inputPublisher">{{ 'document.publisher' | translate }}</label>
          <div class="col-sm-10">
            <input ng-maxlength="500" class="form-control" type="text" id="inputPublisher"
                   ng-attr-placeholder="{{ 'document.edit.publisher_placeholder' | translate }}" name="publisher" ng-model="document.publisher" ng-disabled="fileIsUploading" />
          </div>
        </div>
        <div class="form-group" ng-class="{ 'has-error': !documentForm.format.$valid }">
          <label class="col-sm-2 control-label" for="inputFormat">{{ 'document.format' | translate }}</label>
          <div class="col-sm-10">
            <input ng-maxlength="500" class="form-control" type="text" id="inputFormat"
                   ng-attr-placeholder="{{ 'document.edit.format_placeholder' | translate }}" name="format" ng-model="document.format" ng-disabled="fileIsUploading" />
          </div>
        </div>
        <div class="form-group" ng-class="{ 'has-error': !documentForm.source.$valid }">
          <label class="col-sm-2 control-label" for="inputSource">{{ 'document.source' | translate }}</label>
          <div class="col-sm-10">
            <input ng-maxlength="500" class="form-control" type="text" id="inputSource"
                   ng-attr-placeholder="{{ 'document.edit.source_placeholder' | translate }}" name="source" ng-model="document.source" ng-disabled="fileIsUploading" />
          </div>
        </div>

        <div class="form-group">
          <label class="col-sm-2 control-label" for="inputType">{{ 'document.type' | translate }}</label>
          <div class="col-sm-10">
            <select class="form-control" id="inputType" name="type" ng-model="document.type" ng-disabled="fileIsUploading">
              <option value=""></option>
              <option ng-repeat="vocabulary in vocabularies['type']">{{ vocabulary.value }}</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label" for="inputCoverage">{{ 'document.coverage' | translate }}</label>
          <div class="col-sm-10">
            <select class="form-control" id="inputCoverage" name="coverage" ng-model="document.coverage" ng-disabled="fileIsUploading">
              <option value=""></option>
              <option ng-repeat="vocabulary in vocabularies['coverage']">{{ vocabulary.value }}</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label" for="inputRights">{{ 'document.rights' | translate }}</label>
          <div class="col-sm-10">
            <select class="form-control" id="inputRights" name="rights" ng-model="document.rights" ng-disabled="fileIsUploading">
              <option value=""></option>
              <option ng-repeat="vocabulary in vocabularies['rights']">{{ vocabulary.value }}</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label" for="inputRelations">{{ 'document.relations' | translate }}</label>
          <div class="col-sm-10">
            <select-relation id="document.id" relations="document.relations" ref="inputRelations" ng-disabled="fileIsUploading"></select-relation>
          </div>
        </div>
      </div>
    </fieldset>

    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-primary" ng-disabled="!documentForm.$valid || fileIsUploading" ng-click="edit()">
          <span class="fas fa-save"></span>
          {{ 'save' | translate }}
        </button>
        <button type="submit" class="btn btn-default" ng-click="cancel()" ng-disabled="fileIsUploading">{{ 'cancel' | translate }}</button>
      </div>
    </div>
  </form>
</div>