﻿<template>
  <div class="card dialog">
    <div class="card-title">
      {{ t('title') }}
    </div>

    <div class="card-content">
      <v-text-field
          v-model="email"
          :label="t('label')"
          variant="outlined"
      ></v-text-field>
    </div>

    <div class="card-actions">
      <button class="secondary"
              @click="cancel">
        {{ t('cancel') }}
      </button>
      <button class="primary"
              @click="save">
        {{ t('save') }}
      </button>
    </div>
  </div>
</template>

<script setup>
import {ref} from 'vue';
import {useClient} from "@/plugins/api.js";
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
const props = defineProps({
  email: {
    required: true,
    type: String
  }
});

const emits = defineEmits(['closeRequested']);

const email = ref(props.email);

const client = useClient();
const save = async () => {
  try {
    await client.post(
        `/api/users/email`,
        {
          email: email.value
        });

    emits('closeRequested');
  } catch (error) {
    console.error(error);
  }
};

const cancel = () => {
  emits('closeRequested');
};
</script>

<i18n>
{
  "en": {
    "title": "Change your Email",
    "label": "Your email"
  },
  "fr": {
    "title": "Changez votre Courriel",
    "label": "Votre email"
  },
  "es": {
    "title": "Cambia tu correo electrónico",
    "label": "Tu correo electrónico"
  }
}
</i18n>


