<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)="updateTemplateName($event)">
        {{ plantilla!.nombre }}
      </p>
    </div>

    <p class="ds-subtitle">Creada el día {{ plantilla!.fechaCreacion | formatShortDatetime | lowercase }}</p>

    <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"
      >
        <fa-icon icon="sync" [icon]="faPlus"></fa-icon>&nbsp;&nbsp;<span>Crear pregunta</span>
      </button>

      <button
        type="button"
        class="ds-btn ds-btn--primary"
        (click)="publishTemplateToStore()"
        [disabled]="isLoading"
        *ngIf="plantilla!.estado === 'DRAFT' && pPreguntas && pPreguntas.length > 0"
      >
        <fa-icon icon="sync" [icon]="faStore"></fa-icon>&nbsp;&nbsp;<span>Publicar en tienda</span>
      </button>

      <button
        type="button"
        class="ds-btn ds-btn--danger"
        (click)="deleteTemplateFromStore()"
        [disabled]="isLoading"
        *ngIf="plantilla!.estado === 'ACTIVE'"
      >
        <fa-icon icon="sync" [icon]="faStore"></fa-icon>&nbsp;&nbsp;<span>Eliminar de tienda</span>
      </button>
    </div>
  </h2>

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

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

  <!-- <div class="alert alert-warning" id="no-result" *ngIf="pPreguntas?.length === 0">
    <span>No se encontraron preguntas</span>
  </div> -->
  <!-- *ngIf="pPreguntas && pPreguntas.length > 0" -->
  <div class="ds-survey" id="entities">
    <div class="ds-survey--all-question-wrapper">
      <ng-container *ngIf="plantilla!.estado === 'ACTIVE'">
        <p class="ds-title text-center">Plantilla en tienda</p>
        <p class="ds-subtitle">No puede modificar la plantilla debido a que esta ya está en la tienda.</p>
      </ng-container>
      <ng-container *ngIf="plantilla!.estado === 'DRAFT' && pPreguntas && pPreguntas.length === 0">
        <p class="ds-title text-center">Plantilla vacía</p>
        <p class="ds-subtitle">Inicie creando preguntas y opciones para la plantilla.</p>
      </ng-container>
      <ng-container *ngIf="plantilla!.estado === 'DRAFT'">
        <div class="ds-survey--question-wrapper" *ngFor="let pPregunta of pPreguntas; let i = index; trackBy: trackId">
          <div
            [attr.data-index]="pPregunta.id"
            [attr.data-tipo]="pPregunta.tipo"
            [attr.data-opcional]="pPregunta.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]="pPregunta.id"
                  [attr.data-tipo]="pPregunta.tipo"
                  contenteditable="true"
                  spellcheck="false"
                  (blur)="updateQuestionName($event)"
                  >{{ pPregunta.nombre }}</span
                >
              </span>
              <fa-icon
                *ngIf="plantilla!.estado === 'DRAFT'"
                class="ds-survey--titulo--icon"
                [icon]="faTimes"
                (click)="deleteQuestion($event)"
                [attr.data-id]="pPregunta.id"
                [attr.data-type]="pPregunta.tipo"
              ></fa-icon>
            </div>
            <div>
              <span *ngIf="pPregunta.tipo === 'SINGLE'" class="ds-subtitle"
                >Pregunta de respuesta {{ 'dataSurveyApp.PreguntaCerradaTipo.SINGLE' | translate | lowercase }}
                {{ pPregunta.opcional ? '(opcional)' : '' }}</span
              >
              <span *ngIf="pPregunta.tipo === 'MULTIPLE'" class="ds-subtitle"
                >Pregunta de respuesta {{ 'dataSurveyApp.PreguntaCerradaTipo.MULTIPLE' | translate | lowercase }}
                {{ pPregunta.opcional ? '(opcional)' : '' }}</span
              >
              <span *ngIf="!pPregunta.tipo" class="ds-subtitle"
                >Pregunta de respuesta abierta {{ pPregunta.opcional ? '(opcional)' : '' }}</span
              >
            </div>
            <ng-container *ngIf="pPregunta.tipo">
              <ng-container *ngFor="let pPreguntaOpcion of pPreguntasOpciones; let j = index; trackBy: trackId">
                <ng-container *ngFor="let pPreguntaOpcionFinal of pPreguntaOpcion">
                  <ng-container *ngIf="pPregunta.id === pPreguntaOpcionFinal.ppreguntaCerrada.id">
                    <div
                      class="ds-survey--option ds-survey--option--base ds-survey--closed-option can-delete"
                      [attr.data-id]="pPreguntaOpcionFinal.id"
                    >
                      <!-- <input class="ds-survey--checkbox" id="{{ pPregunta.id }}-{{ pPreguntaOpcionFinal.id }}" type="checkbox" disabled /> -->
                      <label for="{{ pPregunta.id }}-{{ pPreguntaOpcionFinal.id }}">{{ pPreguntaOpcionFinal.nombre }}</label>
                      <fa-icon
                        *ngIf="plantilla!.estado === 'DRAFT'"
                        class="ds-survey--titulo--icon ds-survey--titulo--icon--small"
                        [icon]="faTimes"
                        (click)="deleteOption($event)"
                        [attr.data-optionid]="pPreguntaOpcionFinal.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]="pPregunta.id"
              >
                <fa-icon
                  class="ds-survey--add-option--icon"
                  [icon]="faPlus"
                  [attr.data-id]="pPregunta.id"
                  [attr.data-type]="pPregunta.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="!pPregunta.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.pPreguntaCerradaOpcion.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>
