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

const { t } = useI18n();
const props = defineProps({
  creator: {
    required: true
  }
});

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

const name = ref(props.creator.name);

const client = useClient();

async function save() {
  try {
    await client.post(
        `/api/creators/${props.creator.id}/name`,
        {
          name: name.value
        }
    );

    props.creator.name = name.value;
    emits('closeRequested');
  } catch (error) {
    console.error('Error saving title:', error);
  }
}

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

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

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

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

<style scoped>

</style>

<i18n>
{
  "en": {
    "title": "Change Name",
    "label": "Your name"
  },
  "fr": {
    "title": "Modifier le nom",
    "label": "Votre nom"
  },
  "es": {
    "title": "Cambiar nombre",
    "label": "Tu nombre"
  }
}
</i18n>
