<template>
  <div>
    <div class="d-flex justify-content-center">
      <div class="col-md-8 toastify-container">
        <h2 v-if="username" id="settings-title">
          <span v-html="t$('settings.title', { username })"></span>
        </h2>

        <div class="alert alert-success" role="alert" v-if="success" v-html="t$('settings.messages.success')"></div>

        <div class="alert alert-danger" role="alert" v-if="errorEmailExists" v-html="t$('register.messages.error.emailexists')"></div>

        <form name="form" id="settings-form" @submit.prevent="save()" v-if="settingsAccount" novalidate>
          <div class="mb-3">
            <label class="form-control-label" for="firstName">{{ t$('settings.form.firstname') }}</label>
            <input
              type="text"
              class="form-control"
              id="firstName"
              name="firstName"
              :placeholder="t$('settings.form[\'firstname.placeholder\']')"
              :class="{ 'is-valid': !v$.settingsAccount.firstName.$invalid, 'is-invalid': v$.settingsAccount.firstName.$invalid }"
              v-model="v$.settingsAccount.firstName.$model"
              minlength="1"
              maxlength="50"
              required
              data-cy="firstname"
            />
            <div v-if="v$.settingsAccount.firstName.$anyDirty && v$.settingsAccount.firstName.$invalid">
              <small class="form-text text-danger" v-if="v$.settingsAccount.firstName.required.$invalid">{{
                t$('settings.messages.validate.firstname.required')
              }}</small>
              <small class="form-text text-danger" v-if="v$.settingsAccount.firstName.minLength.$invalid">{{
                t$('settings.messages.validate.firstname.minlength')
              }}</small>
              <small class="form-text text-danger" v-if="v$.settingsAccount.firstName.maxLength.$invalid">{{
                t$('settings.messages.validate.firstname.maxlength')
              }}</small>
            </div>
          </div>
          <div class="mb-3">
            <label class="form-control-label" for="lastName">{{ t$('settings.form.lastname') }}</label>
            <input
              type="text"
              class="form-control"
              id="lastName"
              name="lastName"
              :placeholder="t$('settings.form[\'lastname.placeholder\']')"
              :class="{ 'is-valid': !v$.settingsAccount.lastName.$invalid, 'is-invalid': v$.settingsAccount.lastName.$invalid }"
              v-model="v$.settingsAccount.lastName.$model"
              minlength="1"
              maxlength="50"
              required
              data-cy="lastname"
            />
            <div v-if="v$.settingsAccount.lastName.$anyDirty && v$.settingsAccount.lastName.$invalid">
              <small class="form-text text-danger" v-if="v$.settingsAccount.lastName.required.$invalid">{{
                t$('settings.messages.validate.lastname.required')
              }}</small>
              <small class="form-text text-danger" v-if="v$.settingsAccount.lastName.minLength.$invalid">{{
                t$('settings.messages.validate.lastname.minlength')
              }}</small>
              <small class="form-text text-danger" v-if="v$.settingsAccount.lastName.maxLength.$invalid">{{
                t$('settings.messages.validate.lastname.maxlength')
              }}</small>
            </div>
          </div>
          <div class="mb-3">
            <label class="form-control-label" for="email">{{ t$("global.form['email.label']") }}</label>
            <input
              type="email"
              class="form-control"
              id="email"
              name="email"
              :placeholder="t$('global.form[\'email.placeholder\']')"
              :class="{ 'is-valid': !v$.settingsAccount.email.$invalid, 'is-invalid': v$.settingsAccount.email.$invalid }"
              v-model="v$.settingsAccount.email.$model"
              minlength="5"
              maxlength="254"
              email
              required
              data-cy="email"
            />
            <div v-if="v$.settingsAccount.email.$anyDirty && v$.settingsAccount.email.$invalid">
              <small class="form-text text-danger" v-if="v$.settingsAccount.email.required.$invalid">{{
                t$('global.messages.validate.email.required')
              }}</small>
              <small class="form-text text-danger" v-if="v$.settingsAccount.email.email.$invalid">{{
                t$('global.messages.validate.email.invalid')
              }}</small>
              <small class="form-text text-danger" v-if="v$.settingsAccount.email.minLength.$invalid">{{
                t$('global.messages.validate.email.minlength')
              }}</small>
              <small class="form-text text-danger" v-if="v$.settingsAccount.email.maxLength.$invalid">{{
                t$('global.messages.validate.email.maxlength')
              }}</small>
            </div>
          </div>
          <div class="mb-3" v-if="languages && Object.keys(languages).length > 1">
            <label for="langKey">{{ t$('settings.form.language') }}</label>
            <select class="form-control" id="langKey" name="langKey" v-model="settingsAccount.langKey" data-cy="langKey">
              <option v-for="(language, key) in languages" :value="key" :key="`lang-${key}`">{{ language.name }}</option>
            </select>
          </div>
          <button type="submit" :disabled="v$.settingsAccount.$invalid" class="btn btn-primary" data-cy="submit">
            {{ t$('settings.form.button') }}
          </button>
        </form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" src="./settings.component.ts"></script>
