<template>
  <q-card-section class="no-padding">
    <div class="">
      <div class="text-text">
        <span class="text-weight-medium">{{ $t('auth.full_name') }}</span>
      </div>
      <q-input
        v-model="form.name"
        no-error-icon
        outlined
        rounded
        class="bg-surface q-mt-sm q-mb-md"
        input-class="text-text"
        :placeholder="$t('auth.full_name')"
        hide-bottom-space
        :rules="[inputRules.required]"
        lazy-rules
      />
    </div>

    <div class="">
      <div class="text-text">
        <span class="text-weight-medium">{{ $t('common.terms.cpf') }}</span>
      </div>
      <q-input
        v-model="form.document"
        no-error-icon
        outlined
        rounded
        class="bg-surface q-mt-sm q-mb-md"
        input-class="text-text"
        placeholder="000.000.000-00"
        hide-bottom-space
        :rules="[inputRules.required, inputRules.cpf]"
        lazy-rules
        mask="###.###.###-##"
      />
    </div>

    <div class="">
      <div class="text-text">
        <span class="text-weight-medium">{{ $t('common.terms.cep') }}</span>
      </div>
      <q-input
        v-model="form.zip_code"
        no-error-icon
        outlined
        rounded
        class="bg-surface q-mt-sm q-mb-md"
        input-class="text-text"
        placeholder="00000-00"
        hide-bottom-space
        :rules="[inputRules.required, inputRules.cep]"
        lazy-rules
        mask="#####-###"
        :loading="loadingCep"
        @update:model-value="onCepChange"
      />
    </div>

    <div class="">
      <div class="text-text">
        <span class="text-weight-medium">{{ $t('common.terms.address') }}</span>
      </div>
      <q-input
        v-model="form.address"
        no-error-icon
        outlined
        rounded
        class="bg-surface q-mt-sm q-mb-md"
        input-class="text-text"
        :placeholder="`${$t('common.terms.address')}...`"
        hide-bottom-space
        :rules="[inputRules.required]"
        lazy-rules
        readonly
      />
    </div>

    <div class="">
      <q-checkbox
        v-model="form.no_complement"
        :label="$t('auth.no_complement')"
        color="primary"
        class="q-mb-md text-text"
      />
    </div>
    <div class="">
      <template v-if="!form.no_complement">
        <div class="text-text">
          <span class="text-weight-medium">{{ $t('common.terms.complement') }}</span>
        </div>
        <q-input
          v-model="form.complement"
          no-error-icon
          outlined
          rounded
          class="bg-surface q-mt-sm q-mb-md"
          input-class="text-text"
          :placeholder="`${$t('common.ui.misc.example')}: Apartamento, Conjunto, Casa`"
          hide-bottom-space
          :rules="!form.no_complement ? [inputRules.required] : []"
          lazy-rules
        />
      </template>
    </div>
    <div class="">
      <div class="text-text">
        <span class="text-weight-medium">{{ $t('auth.address_nickname') }}</span>
      </div>
      <q-input
        v-model="form.nickname"
        no-error-icon
        outlined
        rounded
        class="bg-surface q-mt-sm q-mb-md"
        input-class="text-text"
        :placeholder="`${$t('common.ui.misc.example')}: Casa`"
        hide-bottom-space
        lazy-rules
      />
    </div>

    <div class="">
      <div class="text-text">
        <span class="text-weight-medium">{{ $t('auth.address_instructions') }}</span>
      </div>
      <q-input
        v-model="form.instructions"
        no-error-icon
        outlined
        rounded
        class="bg-surface q-mt-sm q-mb-md"
        input-class="text-text"
        type="textarea"
        rows="3"
        autogrow
        hide-bottom-space
        lazy-rules
      />
    </div>
    <div class="">
      <div class="row q-gutter-sm q-mt-xs">
        <q-chip
          v-for="type in addressTypes"
          :key="type.value"
          :selected="form.address_type === type.value"
          clickable
          color="primary"

          :outline="form.address_type !== type.value"
          text-color="surface"
          :icon="type.icon"
          :icon-selected="type.icon"
          @click="form.address_type = type.value"
        >
          {{ $t(type.label) }}
        </q-chip>
      </div>
    </div>
  </q-card-section>
</template>

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

const form = defineModel({ type: Object, required: true });

const { inputRules } = useInputRules();
const loadingCep = ref(false);

const addressTypes = [
  { value: 'home', label: 'auth.address_type_home', icon: 'mdi-home-outline' },
  { value: 'commercial', label: 'auth.address_type_commercial', icon: 'mdi-briefcase-variant-outline' },
  { value: 'other', label: 'auth.address_type_other', icon: 'mdi-map-marker-outline' },
];

const fetchCep = async (rawCep) => {
  const cleaned = rawCep.replace(/\D/g, '');
  if (cleaned.length !== 8) return;

  loadingCep.value = true;
  try {
    const { data } = await axios.get(`https://viacep.com.br/ws/${cleaned}/json/`);
    if (!data.erro) {
      form.value.address = `${data.logradouro}, ${data.bairro} - ${data.localidade}/${data.uf}`;
      form.value.city = data.localidade;
      form.value.state = data.uf;
    } else {
      form.value.address = '';
    }
  } catch {
    form.value.address = '';
  } finally {
    loadingCep.value = false;
  }
};

const onCepChange = (val) => {
  const cleaned = val?.replace(/\D/g, '') ?? '';
  if (cleaned.length === 8) {
    fetchCep(val);
  } else {
    form.value.address = '';
  }
};
</script>
