<template>
    <DialogWidget ref="dialog" title="passwordReset.title" :isTitleTranslated="true" :show-close=true :buttons="buttons" @close="closeDialog" @reset="resetPassword" name="PasswordReset">
        <div>
            <label>{{ $t("passwordReset.email") }} <input type="email" v-model="email" required /></label>
        </div>
    </DialogWidget>
</template>

<script>
import apiClient from '@/utils/axios.js';
import DialogWidget from '@/components/DialogWidget.vue';

export default {
    name: 'PasswordResetDialog',
    components: {
        DialogWidget,
    },
    data() {
        return {
            email: '',
            buttons: [{ text: 'passwordReset.reset', action: 'reset' }]
        };
    },
    methods: {
        open() {
            this.$refs.dialog.open();
        },
        closeDialog() {
            this.$refs.dialog.close();
        },
        async resetPassword() {
            try {
                await apiClient.post('/api/users/requestPasswordReset', {
                    email: this.email
                });
                this.$refs.dialog.close();
                alert(this.$t("passwordReset.success"));
            } catch (error) {
                console.error('Error resetting password:', error);
                alert(this.$t("passwordReset.failure"));
            }
        }
    }
};
</script>
