<template>
  <div>
    <div class="text-left label"><label :for="`phone-number-${getId}`">{{ title }}</label></div>
    <the-mask
      :id="`phone-number-${getId}`"
      type="tel"
      :disabled="disabled"
      class="form-control radius-theme"
      :mask="numberMask"
      :placeholder="placeHolder"
      v-model.trim="getCurrentValue"
    />
    <div class="text-left" v-show="error">
      <small class=" pt-2 text-danger" >Por favor, insira um número.</small>
    </div>
  </div>
</template>

<script>
export default {
  name: 'phone-number',
  props: {
    phoneNumber: {
      type: String,
      required: true,
    },
    title: {
      type: String,
      default: 'Celular:',
    },
    placeHolder: {
      type: String,
      default: '62 9 9090-9090',
    },
    is_cellphone:{
      type: Boolean,
      default: true
    },
    submit:{
      type: Boolean,
      default: false
    },
    disabled:{
      type: Boolean,
      default: false
    }
  },
  model: {
    prop: 'phoneNumber',
    event: 'change',
  },
  computed: {
    error(){
      return this.submit && this.getCurrentValue.length < 10;
    },
    getCurrentValue: {
      get() {
        return this.phoneNumber;
      },
      set(value) {
        this.$emit('change', value);
      },
    },
    numberMask(){
      if(this.is_cellphone){
        return '(##) # ####-####';
      }

      return '(##) ####-####';
    },
    getId() {
      return Math.floor(Math.random() * 100);
    },
  },
};
</script>

<style scoped lang="scss">
@import '../../../assets/css/main.scss';
@import '../../../assets/css/cards.scss';
</style>
