<div>
  <h2 id="page-heading" data-cy="EPreguntaCerradaHeading">
    <div class="d-flex align-items-center">
      <p class="ds-title ds-contenteditable" contenteditable="true" spellcheck="false" (blur)="updateSurveyName($event)">
        {{ encuesta!.nombre }}
      </p>
      &nbsp;&nbsp;<fa-icon
        class="ds-info--icon"
        [icon]="faQuestion"
        data-toggle="modal"
        data-target="#verParametros"
        (click)="loadAplicationParameters()"
      ></fa-icon>
      &nbsp;&nbsp;<fa-icon class="ds-info--icon" [icon]="faEye" (click)="openPreview()"></fa-icon>
      <div class="d-flex px-4">
        <div class="col-12 ds-list-collabs">
          <div class="row" style="flex-direction: row-reverse">
            <div class="col-mb-2 iconos-colab">
              <div class="add-collab" data-toggle="modal" data-target="#modalAddColaborators">
                <fa-icon icon="sync" [icon]="faPlus"></fa-icon>
              </div>
            </div>

            <div
              class="col-mb-2 iconos-colab"
              *ngFor="let colaborador of usuariosColaboradores"
              (click)="selectColaborator(colaborador)"
              data-toggle="modal"
              data-target="#modalUpdateColaborators"
            >
              <img
                class="photo-collab"
                *ngIf="colaborador.usuarioExtra"
                src="../../../../content/profile_icons/C{{ colaborador.usuarioExtra.iconoPerfil }}.png"
                alt="{{ colaborador.usuarioExtra.nombre }}"
                [attr.data-id]="colaborador.id"
              />
            </div>
          </div>
        </div>
      </div>
    </div>

    <p class="ds-subtitle">Creada el día {{ encuesta!.fechaCreacion | formatShortDatetime | lowercase }}</p>
    <button type="button" class="ds-btn ds-btn--danger" (click)="finalizar()" *ngIf="encuesta!.estado === 'ACTIVE'">
      <fa-icon icon="sync" [icon]="faTimes"></fa-icon>&nbsp;&nbsp;<span>Finalizar</span>
    </button>

    <div class="d-flex justify-content-end">
      <button type="button" class="ds-btn ds-btn--secondary" (click)="previousState()">
        <fa-icon icon="arrow-left"></fa-icon>&nbsp;&nbsp;<span jhiTranslate="entity.action.back">Back</span>
      </button>
      <button type="button" class="ds-btn ds-btn--secondary" (click)="loadAll()" [disabled]="isLoading">
        <fa-icon icon="sync" [spin]="isLoading"></fa-icon>&nbsp;&nbsp;<span>Refrescar preguntas</span>
      </button>

      <button
        type="button"
        class="ds-btn ds-btn--primary"
        (click)="createQuestion()"
        [disabled]="isLoading"
        data-toggle="modal"
        data-target="#crearPregunta"
        *ngIf="encuesta!.estado !== 'FINISHED' && (isAutor() || isEscritor())"
      >
        <fa-icon icon="sync" [icon]="faPlus"></fa-icon>&nbsp;&nbsp;<span>Crear pregunta</span>
      </button>
    </div>
  </h2>

  <jhi-alert-error></jhi-alert-error>

  <!-- <jhi-alert></jhi-alert> -->

  <!-- <div class="alert alert-warning" id="no-result" *ngIf="ePreguntas?.length === 0">
    <span>No se encontraron preguntas</span>
  </div> -->
  <!-- *ngIf="ePreguntas && ePreguntas.length > 0" -->
  <div class="ds-survey" id="entities">
    <div class="ds-survey--all-question-wrapper">
      <ng-container *ngIf="encuesta!.estado === 'ACTIVE'">
        <p class="ds-title text-center">Encuesta en progreso</p>
        <p class="ds-subtitle">No puede modificar la encuesta debido a que esta ya está en progreso.</p>
      </ng-container>
      <ng-container *ngIf="encuesta!.estado === 'FINISHED'">
        <p class="ds-title text-center">Encuesta finalizada</p>
        <p class="ds-subtitle">No puede modificar la encuesta debido a que esta ya ha concluido.</p>
      </ng-container>
      <ng-container *ngIf="encuesta!.estado === 'DRAFT' && ePreguntas && ePreguntas.length === 0">
        <p class="ds-title text-center">Encuesta vacía</p>
        <p class="ds-subtitle">Inicie creando preguntas y opciones para su encuesta.</p>
      </ng-container>
      <ng-container *ngIf="encuesta!.estado === 'DRAFT'">
        <div class="ds-survey--question-wrapper" *ngFor="let ePregunta of ePreguntas; let i = index; trackBy: trackId">
          <div
            [attr.data-index]="ePregunta.id"
            [attr.data-tipo]="ePregunta.tipo"
            [attr.data-opcional]="ePregunta.opcional"
            class="ds-survey--question"
          >
            <div class="ds-survey--titulo">
              <span class="ds-survey--titulo--name">
                <span>{{ i + 1 }}.</span>&nbsp;
                <span
                  class="ds-contenteditable"
                  [attr.data-id]="ePregunta.id"
                  [attr.data-tipo]="ePregunta.tipo"
                  contenteditable="true"
                  spellcheck="false"
                  (blur)="updateQuestionName($event)"
                  >{{ ePregunta.nombre }}</span
                >
              </span>
              <fa-icon
                *ngIf="encuesta!.estado === 'DRAFT' && (isAutor() || isEscritor())"
                class="ds-survey--titulo--icon"
                [icon]="faTimes"
                (click)="deleteQuestion($event)"
                [attr.data-id]="ePregunta.id"
                [attr.data-type]="ePregunta.tipo"
              ></fa-icon>
            </div>
            <div>
              <span *ngIf="ePregunta.tipo === 'SINGLE'" class="ds-subtitle"
                >Pregunta de respuesta {{ 'dataSurveyApp.PreguntaCerradaTipo.SINGLE' | translate | lowercase }}
                {{ ePregunta.opcional ? '(opcional)' : '' }}</span
              >
              <span *ngIf="ePregunta.tipo === 'MULTIPLE'" class="ds-subtitle"
                >Pregunta de respuesta {{ 'dataSurveyApp.PreguntaCerradaTipo.MULTIPLE' | translate | lowercase }}
                {{ ePregunta.opcional ? '(opcional)' : '' }}</span
              >
              <span *ngIf="!ePregunta.tipo" class="ds-subtitle"
                >Pregunta de respuesta abierta {{ ePregunta.opcional ? '(opcional)' : '' }}</span
              >
            </div>
            <ng-container *ngIf="ePregunta.tipo">
              <ng-container *ngFor="let ePreguntaOpcion of ePreguntasOpciones; let j = index; trackBy: trackId">
                <ng-container *ngFor="let ePreguntaOpcionFinal of ePreguntaOpcion">
                  <ng-container *ngIf="ePregunta.id === ePreguntaOpcionFinal.epreguntaCerrada.id">
                    <div
                      class="ds-survey--option ds-survey--option--base ds-survey--closed-option can-delete"
                      [attr.data-id]="ePreguntaOpcionFinal.id"
                    >
                      <!-- <input class="ds-survey--checkbox" id="{{ ePregunta.id }}-{{ ePreguntaOpcionFinal.id }}" type="checkbox" disabled /> -->
                      <label for="{{ ePregunta.id }}-{{ ePreguntaOpcionFinal.id }}">{{ ePreguntaOpcionFinal.nombre }}</label>
                      <fa-icon
                        *ngIf="encuesta!.estado === 'DRAFT' && (isAutor() || isEscritor())"
                        class="ds-survey--titulo--icon ds-survey--titulo--icon--small"
                        [icon]="faTimes"
                        (click)="deleteOption($event)"
                        [attr.data-optionid]="ePreguntaOpcionFinal.id"
                      ></fa-icon>
                    </div>
                  </ng-container>
                </ng-container>
              </ng-container>
              <div
                class="ds-survey--option ds-survey--option--add ds-survey--closed-option"
                (click)="resetForm($event)"
                data-toggle="modal"
                data-target="#crearOpcion"
                [attr.data-id]="ePregunta.id"
                *ngIf="isAutor() || isEscritor()"
              >
                <fa-icon
                  class="ds-survey--add-option--icon"
                  [icon]="faPlus"
                  [attr.data-id]="ePregunta.id"
                  [attr.data-type]="ePregunta.tipo"
                ></fa-icon>
                <span class="ds-survey--add-option">Añadir opción</span>
              </div>
            </ng-container>
            <div class="ds-survey--option ds-survey--option--base ds-survey--open-option" *ngIf="!ePregunta.tipo">
              <textarea name="" id="" cols="30" rows="10" disabled></textarea>
            </div>
          </div>
        </div>
      </ng-container>
    </div>
  </div>
</div>

<!-- Create Option Modal -->
<div class="modal fade ds-modal" id="crearOpcion" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <form autocomplete="off" class="ds-form" name="editForm" role="form" novalidate (ngSubmit)="save()" [formGroup]="editForm">
        <div class="modal-header">
          <h1 class="modal-title" id="exampleModalLongTitle">Crear Opción</h1>
        </div>
        <div class="modal-body">
          <!-- Survey Closed Question Create Option Modal -->
          <div>
            <jhi-alert-error></jhi-alert-error>

            <div class="form-group">
              <label class="form-control-label" jhiTranslate="dataSurveyApp.ePreguntaCerradaOpcion.nombre" for="field_nombre">Nombre</label>
              <input type="text" class="form-control" name="nombre" id="field_nombre" data-cy="nombre" formControlName="nombre" />
              <div *ngIf="editForm.get('nombre')!.invalid && (editForm.get('nombre')!.dirty || editForm.get('nombre')!.touched)">
                <small
                  class="form-text text-danger"
                  *ngIf="editForm.get('nombre')?.errors?.required"
                  jhiTranslate="entity.validation.required"
                >
                  This field is required.
                </small>
                <small
                  class="form-text text-danger"
                  *ngIf="editForm.get('nombre')?.errors?.minlength"
                  jhiTranslate="entity.validation.minlength"
                  [translateValues]="{ min: 1 }"
                >
                  This field is required to be at least 1 characters.
                </small>
                <small
                  class="form-text text-danger"
                  *ngIf="editForm.get('nombre')?.errors?.maxlength"
                  jhiTranslate="entity.validation.maxlength"
                  [translateValues]="{ max: 500 }"
                >
                  This field cannot be longer than 500 characters.
                </small>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <input id="createAnother" type="checkbox" (change)="createAnotherChange($event)" />
          <label for="createAnother">Crear otra</label>
          <button id="cancelBtn" type="button" class="ds-btn ds-btn--secondary" data-dismiss="modal">
            <fa-icon icon="arrow-left"></fa-icon>&nbsp;&nbsp;<span jhiTranslate="entity.action.cancel">Cancel</span>
          </button>
          <button
            type="submit"
            id="save-entity"
            data-cy="entityCreateSaveButton"
            class="ds-btn ds-btn--primary"
            [disabled]="editForm.invalid || isSaving"
          >
            <span jhiTranslate="entity.action.create">Create</span>
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- ------------------------------------------------------------------------------------------------- -->

<!-- Create Question Modal -->
<div
  class="modal fade ds-modal"
  id="crearPregunta"
  tabindex="-1"
  role="dialog"
  aria-labelledby="exampleModalCenterTitle"
  aria-hidden="true"
>
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <form
        autocomplete="off"
        class="ds-form"
        name="editFormQuestion"
        role="form"
        novalidate
        (ngSubmit)="saveQuestion()"
        [formGroup]="editFormQuestion"
      >
        <div class="modal-header">
          <h1 class="modal-title" id="exampleModalLongTitle1">Crear Pregunta</h1>
        </div>
        <div class="modal-body">
          <!-- Survey Create Question Modal -->
          <div>
            <jhi-alert-error></jhi-alert-error>

            <div class="form-group">
              <label class="form-control-label" for="field_nombre">Pregunta</label>
              <input type="text" class="form-control" name="nombre" id="field_nombre2" data-cy="nombre" formControlName="nombre" />
              <div
                *ngIf="
                  editFormQuestion.get('nombre')!.invalid &&
                  (editFormQuestion.get('nombre')!.dirty || editFormQuestion.get('nombre')!.touched)
                "
              >
                <small
                  class="form-text text-danger"
                  *ngIf="editFormQuestion.get('nombre')?.errors?.required"
                  jhiTranslate="entity.validation.required"
                >
                  This field is required.
                </small>
                <small
                  class="form-text text-danger"
                  *ngIf="editFormQuestion.get('nombre')?.errors?.minlength"
                  jhiTranslate="entity.validation.minlength"
                  [translateValues]="{ min: 1 }"
                >
                  This field is required to be at least 1 characters.
                </small>
                <small
                  class="form-text text-danger"
                  *ngIf="editFormQuestion.get('nombre')?.errors?.maxlength"
                  jhiTranslate="entity.validation.maxlength"
                  [translateValues]="{ max: 500 }"
                >
                  This field cannot be longer than 500 characters.
                </small>
              </div>
            </div>

            <!-- Custom Form Group (Closed & Open Question Validation) -->
            <div class="form-group">
              <label class="form-control-label" for="field_tipo">Tipo de pregunta</label>
              <select class="form-control" name="tipopregunta" formControlName="tipopregunta" id="field_tipo2" data-cy="tipopregunta">
                <option selected value="CLOSED">Opción multiple</option>
                <option value="OPEN">Respuesta abierta</option>
              </select>
              <div
                *ngIf="
                  editFormQuestion.get('tipopregunta')!.invalid &&
                  (editFormQuestion.get('tipopregunta')!.dirty || editFormQuestion.get('tipopregunta')!.touched)
                "
              >
                <small
                  class="form-text text-danger"
                  *ngIf="editFormQuestion.get('tipopregunta')?.errors?.required"
                  jhiTranslate="entity.validation.required"
                >
                  This field is required.
                </small>
              </div>
            </div>

            <ng-container *ngIf="editFormQuestion.get('tipopregunta')!.value === 'CLOSED'">
              <div class="form-group">
                <label class="form-control-label" jhiTranslate="dataSurveyApp.ePreguntaCerrada.tiporespuesta" for="field_tipo">Tipo</label>
                <select class="form-control" name="tipo" formControlName="tipo" id="field_tipo" data-cy="tipo">
                  <option selected value="SINGLE">{{ 'dataSurveyApp.PreguntaCerradaTipo.SINGLE' | translate }}</option>
                  <option value="MULTIPLE">{{ 'dataSurveyApp.PreguntaCerradaTipo.MULTIPLE' | translate }}</option>
                </select>
                <div
                  *ngIf="
                    editFormQuestion.get('tipo')!.invalid && (editFormQuestion.get('tipo')!.dirty || editFormQuestion.get('tipo')!.touched)
                  "
                >
                  <small
                    class="form-text text-danger"
                    *ngIf="editFormQuestion.get('tipo')?.errors?.required"
                    jhiTranslate="entity.validation.required"
                  >
                    This field is required.
                  </small>
                </div>
              </div>
            </ng-container>

            <div class="form-group">
              <label class="form-control-label" for="field_opcional">Opcional</label>
              <input type="checkbox" class="form-check" name="opcional" id="field_opcional" data-cy="opcional" formControlName="opcional" />
              <div
                *ngIf="
                  editFormQuestion.get('opcional')!.invalid &&
                  (editFormQuestion.get('opcional')!.dirty || editFormQuestion.get('opcional')!.touched)
                "
              >
                <small
                  class="form-text text-danger"
                  *ngIf="editFormQuestion.get('opcional')?.errors?.required"
                  jhiTranslate="entity.validation.required"
                >
                  This field is required.
                </small>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <input id="createAnotherQuestion" type="checkbox" (change)="createAnotherQuestionChange($event)" />
          <label for="createAnotherQuestion">Crear otra</label>
          <button id="cancelBtnQuestion" type="button" class="ds-btn ds-btn--secondary" data-dismiss="modal">
            <fa-icon icon="arrow-left"></fa-icon>&nbsp;&nbsp;<span jhiTranslate="entity.action.cancel">Cancel</span>
          </button>
          <button
            type="submit"
            id="save-question"
            data-cy="entityCreateSaveButton"
            class="ds-btn ds-btn--primary"
            [disabled]="editFormQuestion.invalid || isSaving"
          >
            <span jhiTranslate="entity.action.create">Create</span>
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- ------------------------------------------------------------------------------------------------- -->

<!-- Survey Parameters Information -->
<div
  class="modal fade ds-modal"
  id="verParametros"
  tabindex="-1"
  role="dialog"
  aria-labelledby="exampleModalCenterTitle"
  aria-hidden="true"
>
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title" id="exampleModalLongTitle2">Información de Encuesta</h1>
      </div>

      <!-- {
        "id": 1,
        "maxDiasEncuesta": 20,
        "minDiasEncuesta": 2,
        "maxCantidadPreguntas": 10,
        "minCantidadPreguntas": 2
    } -->

      <div class="modal-body">
        <div>
          <div class="mb-5">
            <p class="ds-subtitle">Duración de encuesta permitida</p>
            <p>{{ parametrosAplicacion!.minDiasEncuesta }} - {{ parametrosAplicacion!.maxDiasEncuesta }} días</p>
          </div>
          <div>
            <p class="ds-subtitle">Cantidad de preguntas por encuesta</p>
            <p>{{ parametrosAplicacion!.minCantidadPreguntas }} - {{ parametrosAplicacion!.maxCantidadPreguntas }} preguntas</p>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button id="cancelBtnVerParametros" type="button" class="ds-btn ds-btn--secondary" data-dismiss="modal">
          <fa-icon icon="arrow-left"></fa-icon>&nbsp;&nbsp;<span>Volver</span>
        </button>
      </div>
    </div>
  </div>
</div>

<!-- ------------------------------------------------------------------------------------------------- -->

<!-- ------------------------------------------------------------------------------------------------- -->

<!-- Survey Update Colaborator -->
<div
  class="modal fade ds-modal"
  id="modalUpdateColaborators"
  tabindex="-1"
  role="dialog"
  aria-labelledby="verColaboradoresTitle"
  aria-hidden="true"
  *ngIf="colaborador && isAutor()"
>
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <form class="ds-form" name="editFormUpdateCollab" role="form" (ngSubmit)="saveCollab()" [formGroup]="editFormUpdateCollab">
        <div class="modal-header">
          <h1 class="modal-title" id="modalColaboradores">Colaborador</h1>
        </div>
        <div class="modal-body">
          <div>
            <div class="mb-5" *ngIf="colaborador.usuarioExtra">
              <p class="ds-subtitle">Nombre</p>
              <p>{{ colaborador!.usuarioExtra.nombre }}</p>
            </div>
            <div>
              <label for="field_update_rol">Rol</label>
              <select class="form-control" name="rol" formControlName="rol" id="field_update_rol" data-cy="rol">
                <option value="READ" [selected]="colaborador.rol === 'READ'">Lector</option>
                <option value="WRITE" [selected]="colaborador.rol === 'WRITE'">Escritor</option>
              </select>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button id="btnCancelUbdateColaboradores" type="button" class="ds-btn ds-btn--secondary" data-dismiss="modal">
            <fa-icon icon="arrow-left"></fa-icon>&nbsp;&nbsp;<span>Cancelar</span>
          </button>
          <button type="button" (click)="deleteCollab(colaborador)" class="ds-btn ds-btn--danger btn-sm" data-cy="entityDeleteButton">
            <fa-icon icon="times"></fa-icon>
            <span class="d-none d-md-inline" jhiTranslate="dataSurveyApp.usuarioEncuesta.delete.action">Delete</span>
          </button>
          <button
            id="btnUpdateColaboradores"
            type="submit"
            class="ds-btn ds-btn--primary"
            data-cy="entityUpdateButton"
            [disabled]="isSavingCollab"
          >
            &nbsp;<span>Guardar</span>
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- ------------------------------------------------------------------------------------------------- -->

<!-- Survey Add Colaborator -->
<div
  class="modal fade ds-modal"
  id="modalAddColaborators"
  tabindex="-1"
  role="dialog"
  aria-labelledby="verColaboradoresTitle"
  aria-hidden="true"
  *ngIf="isAutor()"
>
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <form class="ds-form" name="editFormAddCollab" role="form" (ngSubmit)="saveAddCollab()" [formGroup]="editFormAddCollab">
        <div class="modal-header">
          <h1 class="modal-title" id="modalAddColaboradores">Añadir Colaborador</h1>
        </div>
        <div class="modal-body">
          <div *ngIf="userCollabNotExist" class="alert alert-danger alert-dismissible fade show" role="alert">
            No existe un usuario con ese correo
          </div>
          <div *ngIf="userCollabIsCollab" class="alert alert-danger alert-dismissible fade show" role="alert">
            Este usuario ya se encuentra colaborando
          </div>
          <div *ngIf="userCollabIsAutor" class="alert alert-danger alert-dismissible fade show" role="alert">
            Usted es el autor de la encuesta, no puede ser colaborador
          </div>
          <div>
            <div class="mb-5">
              <p class="ds-subtitle">Correo electrónico</p>
              <input type="email" class="form-control" name="email_add" id="field_add_email" data-cy="email" formControlName="email_add" />
              <div
                *ngIf="
                  editFormAddCollab.get('email_add')!.invalid &&
                  (editFormAddCollab.get('email_add')!.dirty || editFormAddCollab.get('email_add')!.touched)
                "
              >
                <small
                  class="form-text text-danger"
                  *ngIf="editFormAddCollab.get('email_add')?.errors?.required"
                  jhiTranslate="entity.validation.required"
                >
                  This field is required.
                </small>
                <small
                  class="form-text text-danger"
                  *ngIf="editFormAddCollab.get('email_add')?.errors?.invalid"
                  jhiTranslate="global.messages.validate.email.invalid"
                >
                  Your email is invalid.
                </small>
              </div>
            </div>
            <div>
              <label for="field_update_rol">Rol</label>
              <select class="form-control" name="rol_add" formControlName="rol_add" id="field_rol_add" data-cy="rol_add">
                <option value="READ">Lector</option>
                <option value="WRITE">Escritor</option>
              </select>
              <div
                *ngIf="
                  editFormAddCollab.get('rol_add')!.invalid &&
                  (editFormAddCollab.get('rol_add')!.dirty || editFormAddCollab.get('rol_add')!.touched)
                "
              >
                <small
                  class="form-text text-danger"
                  *ngIf="editFormAddCollab.get('rol_add')?.errors?.required"
                  jhiTranslate="entity.validation.required"
                >
                  This field is required.
                </small>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button
            id="btnCancelAddColaboradores"
            (click)="resetFormAddCollab()"
            type="button"
            class="ds-btn ds-btn--secondary"
            data-dismiss="modal"
          >
            <fa-icon icon="arrow-left"></fa-icon>&nbsp;&nbsp;<span>Cancelar</span>
          </button>
          <button
            id="btnAddColaboradores"
            type="submit"
            class="ds-btn ds-btn--primary"
            data-cy="entityAddButton"
            [disabled]="editFormAddCollab.invalid || isSavingAddCollab"
          >
            <span>Añadir</span>
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- ------------------------------------------------------------------------------------------------- -->
