<template>
  <div>
    <h2 id="page-heading" data-cy="AuditLogHeading">
      <span id="audit-log">{{ t$('smartbookingApp.auditLog.home.title') }}</span>
      <div class="d-flex justify-content-end">
        <button class="btn btn-info me-2" @click="handleSyncList" :disabled="isFetching">
          <font-awesome-icon icon="sync" :spin="isFetching"></font-awesome-icon>
          <span>{{ t$('smartbookingApp.auditLog.home.refreshListLabel') }}</span>
        </button>
        <router-link :to="{ name: 'AuditLogCreate' }" custom v-slot="{ navigate }">
          <button
            @click="navigate"
            id="jh-create-entity"
            data-cy="entityCreateButton"
            class="btn btn-primary jh-create-entity create-audit-log"
          >
            <font-awesome-icon icon="plus"></font-awesome-icon>
            <span>{{ t$('smartbookingApp.auditLog.home.createLabel') }}</span>
          </button>
        </router-link>
      </div>
    </h2>
    <br />
    <div class="alert alert-warning" v-if="!isFetching && auditLogs?.length === 0">
      <span>{{ t$('smartbookingApp.auditLog.home.notFound') }}</span>
    </div>
    <div class="table-responsive" v-if="auditLogs?.length > 0">
      <table class="table table-striped" aria-describedby="auditLogs">
        <thead>
          <tr>
            <th scope="col" @click="changeOrder('id')">
              <span>{{ t$('global.field.id') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'id'"></jhi-sort-indicator>
            </th>
            <th scope="col" @click="changeOrder('entitaTipo')">
              <span>{{ t$('smartbookingApp.auditLog.entitaTipo') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'entitaTipo'"></jhi-sort-indicator>
            </th>
            <th scope="col" @click="changeOrder('entitaId')">
              <span>{{ t$('smartbookingApp.auditLog.entitaId') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'entitaId'"></jhi-sort-indicator>
            </th>
            <th scope="col" @click="changeOrder('azione')">
              <span>{{ t$('smartbookingApp.auditLog.azione') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'azione'"></jhi-sort-indicator>
            </th>
            <th scope="col" @click="changeOrder('dettagli')">
              <span>{{ t$('smartbookingApp.auditLog.dettagli') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'dettagli'"></jhi-sort-indicator>
            </th>
            <th scope="col" @click="changeOrder('ipAddress')">
              <span>{{ t$('smartbookingApp.auditLog.ipAddress') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'ipAddress'"></jhi-sort-indicator>
            </th>
            <th scope="col" @click="changeOrder('createdAt')">
              <span>{{ t$('smartbookingApp.auditLog.createdAt') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'createdAt'"></jhi-sort-indicator>
            </th>
            <th scope="col" @click="changeOrder('utente.username')">
              <span>{{ t$('smartbookingApp.auditLog.utente') }}</span>
              <jhi-sort-indicator :current-order="propOrder" :reverse="reverse" :field-name="'utente.username'"></jhi-sort-indicator>
            </th>
            <th scope="col"></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="auditLog in auditLogs" :key="auditLog.id" data-cy="entityTable">
            <td>
              <router-link :to="{ name: 'AuditLogView', params: { auditLogId: auditLog.id } }">{{ auditLog.id }}</router-link>
            </td>
            <td>{{ t$('smartbookingApp.TipoEntita.' + auditLog.entitaTipo) }}</td>
            <td>{{ auditLog.entitaId }}</td>
            <td>{{ t$('smartbookingApp.AzioneAudit.' + auditLog.azione) }}</td>
            <td>{{ auditLog.dettagli }}</td>
            <td>{{ auditLog.ipAddress }}</td>
            <td>{{ formatDateShort(auditLog.createdAt) || '' }}</td>
            <td>
              <div v-if="auditLog.utente">
                <router-link :to="{ name: 'UtenteAppView', params: { utenteAppId: auditLog.utente.id } }">{{
                  auditLog.utente.username
                }}</router-link>
              </div>
            </td>
            <td class="text-end">
              <div class="btn-group">
                <router-link
                  :to="{ name: 'AuditLogView', params: { auditLogId: auditLog.id } }"
                  class="btn btn-info btn-sm details"
                  data-cy="entityDetailsButton"
                >
                  <font-awesome-icon icon="eye"></font-awesome-icon>
                  <span class="d-none d-md-inline">{{ t$('entity.action.view') }}</span>
                </router-link>
                <router-link
                  :to="{ name: 'AuditLogEdit', params: { auditLogId: auditLog.id } }"
                  class="btn btn-primary btn-sm edit"
                  data-cy="entityEditButton"
                >
                  <font-awesome-icon icon="pencil-alt"></font-awesome-icon>
                  <span class="d-none d-md-inline">{{ t$('entity.action.edit') }}</span>
                </router-link>
                <b-button
                  @click="prepareRemove(auditLog)"
                  variant="danger"
                  class="btn btn-sm"
                  data-cy="entityDeleteButton"
                  v-b-modal.removeEntity
                >
                  <font-awesome-icon icon="times"></font-awesome-icon>
                  <span class="d-none d-md-inline">{{ t$('entity.action.delete') }}</span>
                </b-button>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <b-modal ref="removeEntity" id="removeEntity">
      <template #title>
        <span id="smartbookingApp.auditLog.delete.question" data-cy="auditLogDeleteDialogHeading">{{ t$('entity.delete.title') }}</span>
      </template>
      <div class="modal-body">
        <p id="jhi-delete-auditLog-heading">{{ t$('smartbookingApp.auditLog.delete.question', { id: removeId }) }}</p>
      </div>
      <template #footer>
        <div>
          <button type="button" class="btn btn-secondary" @click="closeDialog()">{{ t$('entity.action.cancel') }}</button>
          <button
            type="button"
            class="btn btn-primary"
            id="jhi-confirm-delete-auditLog"
            data-cy="entityConfirmDeleteButton"
            @click="removeAuditLog"
          >
            {{ t$('entity.action.delete') }}
          </button>
        </div>
      </template>
    </b-modal>
    <div v-show="auditLogs?.length > 0">
      <div class="d-flex justify-content-center">
        <jhi-item-count :page="page" :total="queryCount" :items-per-page="itemsPerPage"></jhi-item-count>
      </div>
      <div class="d-flex justify-content-center">
        <b-pagination size="md" :total-rows="totalItems" v-model="page" :per-page="itemsPerPage"></b-pagination>
      </div>
    </div>
  </div>
</template>

<script lang="ts" src="./audit-log.component.ts"></script>
