<template>
  <div class="register-form">
    <!-- Header -->
    <div class="header p-4">
      <b-container class="container">
        <div class="header-body text-center mb-7">
          <b-row class="justify-content-center">
            <b-col xl="5" lg="6" md="8" class="px-2">
              <h1>{{ $t('site.signup.title') }}</h1>
              <p class="text-lead">{{ $t('site.signup.subtitle') }}</p>
            </b-col>
          </b-row>
        </div>
      </b-container>
    </div>
    <!-- Page content -->
    <b-container class="mt--8 p-1">
      <!-- Table -->
      <b-row class="justify-content-center">
        <b-col lg="6" md="8">
          <b-card no-body class="border-0" style="background-color: #ebebeba3 !important">
            <b-card-body class="p-4">
              <div class="text-center text-muted mb-4">
                <small>{{ $t('signup') }}</small>
              </div>

              <validation-observer ref="observer" v-slot="{ handleSubmit }">
                <b-form role="form" @submit.prevent="handleSubmit(onSubmit)">
                  <validation-provider
                    :name="$t('form.firstname')"
                    :rules="{ required: true, min: 3 }"
                    v-slot="validationContext"
                  >
                    <b-form-group
                      class="mb-3"
                      :label="$t('form.firstname')"
                      label-for="registerFirstname"
                    >
                      <b-form-input
                        id="registerFirstname"
                        :name="$t('form.firstname')"
                        v-model="form.firstname"
                        :placeholder="$t('form.firstname')"
                        :state="getValidationState(validationContext)"
                        aria-describedby="registerFirstnameLiveFeedback"
                      ></b-form-input>

                      <b-form-invalid-feedback id="registerFirstnameLiveFeedback">
                        {{ validationContext.errors[0] }}
                      </b-form-invalid-feedback>
                    </b-form-group>
                  </validation-provider>

                  <validation-provider
                    :name="$t('form.lastname')"
                    :rules="{ required: true, min: 2 }"
                    v-slot="validationContext"
                  >
                    <b-form-group
                      class="mb-3"
                      :label="$t('form.lastname')"
                      label-for="registerLastname"
                    >
                      <b-form-input
                        id="registerLastname"
                        :name="$t('form.lastname')"
                        v-model="form.lastname"
                        :placeholder="$t('form.lastname')"
                        :state="getValidationState(validationContext)"
                        aria-describedby="registerLastnameLiveFeedback"
                      ></b-form-input>

                      <b-form-invalid-feedback id="registerLastnameLiveFeedback">
                        {{ validationContext.errors[0] }}
                      </b-form-invalid-feedback>
                    </b-form-group>
                  </validation-provider>

                  <input-email v-model="form.email"></input-email>

                  <hr />
                  <input-password-confirmation
                    v-model="form.password"
                    :register="register"
                  ></input-password-confirmation>

                  <b-row>
                    <b-col cols="12">
                      {{ $t('language') }}
                      <b-form-select
                        id="selectedLanguage"
                        v-model="selected"
                        :options="options"
                        class="mb-3"
                      ></b-form-select>
                    </b-col>
                  </b-row>

                  <b-row class="my-4">
                    <b-col cols="12">
                      <b-form-checkbox
                        id="registerCheckbox"
                        v-model="form.agree"
                        :name="$t('site.signup.agree')"
                      >
                        <span class="text-muted" v-html="$t('site.signup.agree')"></span>
                      </b-form-checkbox>
                    </b-col>
                  </b-row>
                  <b-alert
                    v-if="showError"
                    show
                    dismissible
                    variant="warning"
                    @dismissed="closeAlert"
                  >
                    <span class="alert-icon"><i class="ni ni-point"></i></span>
                    <span class="alert-text">
                      <strong>{{ $t('error.error') }}!</strong>
                      {{ messageError }}
                    </span>
                  </b-alert>

                  <div
                    class="text-center"
                    v-if="namesFilled && emailFilled && form.agree && languageFilled"
                  >
                    <div class="text-center">
                      <b-button class="ml-2" @click="resetForm()">{{ $t('form.reset') }}</b-button>
                      <b-button type="submit" variant="primary">{{ $t('signup') }}</b-button>
                    </div>
                  </div>
                </b-form>
              </validation-observer>
            </b-card-body>
          </b-card>
        </b-col>
      </b-row>
      <div class="text-center py-lg-4">
        <router-link to="/login" class="mt-3">{{ $t('back') }}</router-link>
      </div>
    </b-container>
  </div>
</template>
<script>
import InputEmail from '../../components/Inputs/InputEmail.vue'
import InputPasswordConfirmation from '../../components/Inputs/InputPasswordConfirmation.vue'
import { resgisterUserQuery } from '../../graphql/queries'

export default {
  components: { InputPasswordConfirmation, InputEmail },
  name: 'register',
  data() {
    return {
      form: {
        firstname: '',
        lastname: '',
        email: '',
        agree: false,
        password: {
          password: '',
          passwordRepeat: '',
        },
      },
      selected: null,
      options: [
        { value: null, text: this.$t('select_language') },
        { value: 'de', text: this.$t('languages.de') },
        { value: 'en', text: this.$t('languages.en') },
      ],
      submitted: false,
      showError: false,
      messageError: '',
      register: true,
    }
  },
  methods: {
    getValidationState({ dirty, validated, valid = null }) {
      return dirty || validated ? valid : null
    },
    resetForm() {
      this.form = {
        firstname: '',
        lastname: '',
        email: '',
        password: {
          password: '',
          passwordRepeat: '',
        },
        agree: false,
      }
      this.form.password.password = ''
      this.form.password.passwordRepeat = ''
      this.$nextTick(() => {
        this.$refs.observer.reset()
      })
    },
    async onSubmit() {
      this.$apollo
        .query({
          query: resgisterUserQuery,
          variables: {
            email: this.form.email,
            firstName: this.form.firstname,
            lastName: this.form.lastname,
            password: this.form.password.password,
            language: this.selected,
          },
        })
        .then(() => {
          this.form.email = ''
          this.form.firstname = ''
          this.form.lastname = ''
          this.form.password.password = ''
          this.form.password.passwordRepeat = ''
          this.selected = null
          this.$router.push('/thx/register')
        })
        .catch((error) => {
          this.showError = true
          this.messageError = error.message
        })
    },
    closeAlert() {
      this.showError = false
      this.messageError = ''
      this.form.email = ''
      this.form.firstname = ''
      this.form.lastname = ''
      this.form.password.password = ''
      this.form.password.passwordRepeat = ''
      this.selected = null
    },
  },
  computed: {
    namesFilled() {
      return (
        this.form.firstname !== '' &&
        this.form.firstname.length > 2 &&
        this.form.lastname !== '' &&
        this.form.lastname.length > 1
      )
    },
    emailFilled() {
      return this.form.email !== ''
    },
    languageFilled() {
      return this.selected !== null
    },
  },
}
</script>
<style></style>
