<template>
  <q-card-section class="full-width">
    <div class="text-text">
      <span class="font12 fontbold">
        {{ $t('common.terms.email') }}
      </span>
    </div>

    <q-input
      v-model="email"
      class="bg-surface q-mt-sm"
      hide-bottom-space
      input-class="font12 fontmedium text-text"
      lazy-rules
      no-error-icon
      outlined
      rounded
      :bottom-slots="false"
      :placeholder="$t('common.terms.email')"
      :rules="!smsEnabled || !phone
        ? [inputRules.requiredHideMessage, inputRules.email]
        : [inputRules.email]"
    />

    <template v-if="smsEnabled">
      <div class="col-12 q-py-lg row">
        <div class="col-5 q-my-auto">
          <q-separator class="bg-grey-4 q-my-sm" />
        </div>

        <span class="col font12 fontmedium text-center text-text">
          {{ $t('common.ui.misc.or') }}
        </span>

        <div class="col-5 q-my-auto">
          <q-separator class="bg-grey-4 q-my-sm" />
        </div>
      </div>

      <div class="text-text">
        <span class="font12 fontbold">
          {{ $t('common.terms.phone') }}
        </span>

        <span class="font12 fontlight">
          {{ ` (${$t('common.terms.celular')})` }}
        </span>
      </div>

      <q-input
        v-model="phone"
        class="bg-surface q-mt-sm"
        hide-bottom-space
        input-class="text-text"
        lazy-rules
        mask="(##) #####-####"
        no-error-icon
        :bottom-slots="false"
        :placeholder="$t('common.terms.phone')"
        :rules="!email
          ? [inputRules.requiredHideMessage, validatePhone]
          : [validatePhone]"
      />
    </template>
  </q-card-section>
</template>

<script setup>
import { useInputRules } from 'src/composables/useInputRules';

defineProps({
  smsEnabled: {
    type: Boolean,
    default: false,
  },
});

const email = defineModel('email', {
  required: true,
  type: String,
});

const phone = defineModel('phone', {
  required: true,
  type: String,
});

const { inputRules } = useInputRules();

const validatePhone = (value) => {
  if (!value) {
    return true;
  }

  return value.replace(/\D/g, '').length >= 10 || 'Telefone inválido';
};
</script>
