<template>
  <global-form
    :steps="['request', 'redirect']"
    :submit-button-text="$_tr('signUp')"
    :show-success-logs="false"
    endpoint="content/events_subscriptions"
    form-id="register"
    class="register-layout-container"
    @onRedirect="handleRedirect"
    @onRequestSuccess="handleSuccess"
  >
    <template slot="fields">
      <input-text
        :model-name="content_type"
        :default-value="title"
        :callback="() => uuid"
        :readonly="true"
        :label="$_tr('event')"
        :rules="['required']"
        form-id="register"
      />

      <input-text
        model-name="firstname"
        :default-value="getUserInfos.firstname"
        :label="$_tr('firstname')"
        :readonly="true"
        :standalone="true"
        form-id="register"
      />

      <input-text
        model-name="lastname"
        :default-value="getUserInfos.lastname"
        :label="$_tr('lastname')"
        :readonly="true"
        :standalone="true"
        form-id="register"
      />

      <input-textarea
        form-id="register"
        modelName="message"
        :label="$_tr('comment')"
      />

      <slot />

      <RegisterLanguagesSelectbox form-id="register" />

      <input-hidden
        form-id="register"
        model-name="custom_form_type"
        :default-value="register_forms.custom_form_type"
        :rules="['required']"
      />

      <input-hidden
        form-id="register"
        model-name="bundle"
        default-value="events_subscriptions"
        :rules="['required']"
      />

      <input-hidden
        form-id="register"
        model-name="type"
        default-value="node"
        :rules="['required']"
      />

      <input-hidden
        form-id="register"
        model-name="title"
        :default-value="contentTitle"
        :rules="['required']"
      />
    </template>
  </global-form>
</template>

<script>
import RegisterLanguagesSelectbox from './RegisterLanguagesSelectbox'
import RegisterFeedbackFormsMixin from './../../../mixins/register-feedback-forms-mixin'

import Random from '@inside-library/helpers/GenerateID'
import { formatHref } from '@inside-library/helpers/Langs'

export default {
  name: 'register-layout',
  $_i18nRootPath: 'customs.FORMS.REGISTER',
  components: { RegisterLanguagesSelectbox },
  mixins: [RegisterFeedbackFormsMixin],
  props: {
    formType: {
      type: Number,
      required: true
    },
    content_type: {
      type: String,
      required: true
    },
    register_forms: {
      type: Object
    },
    title: {
      type: String,
      required: true
    },
    uuid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      contentTitle: `event_subscription${Random.ID()}`
    }
  },
  methods: {
    handleSuccess() {
      this.$emit('onSubscribeSuccess')
    },
    handleRedirect() {
      this.$router.push(
        formatHref({
          name: 'EventPage',
          params: { slug: this.$route.params.slug }
        })
      )
    }
  }
}
</script>

<style lang="scss">
.register-layout-container {
  .input-container {
    margin: 50px 0;
  }

  .language {
    max-width: 300px;
  }
}
</style>
