<script setup>

import { ref, useTemplateRef } from 'vue';
import { PasswordInput, Dialog, FormGroup } from '@cloudron/pankow';
import ProfileModel from '../models/ProfileModel.js';

const emit = defineEmits([ 'success' ]);

const profileModel = ProfileModel.create();

const dialog = useTemplateRef('dialog');
const formError = ref({});
const busy = ref (false);
const newPassword = ref('');
const newPasswordRepeat = ref('');
const password = ref('');

const form = useTemplateRef('form');
const isFormValid = ref(false);
function checkValidity() {
  isFormValid.value = form.value ? form.value.checkValidity() : false;

  if (isFormValid.value) {
      if (newPasswordRepeat.value !== newPassword.value) isFormValid.value = false;
  }
}

async function onSubmit() {
  if (!form.value.reportValidity()) return;

  busy.value = true;
  formError.value = {};

  const [error] = await profileModel.setPassword(password.value, newPassword.value);
  if (error) {
    if (error.status === 400) formError.value.newPassword = error.body.message;
    else if (error.status === 412) formError.value.password = error.body.message;
    else {
      formError.value.generic = error.status ? error.body.message : 'Internal error';
      console.error('Failed to set password', error);
    }

    busy.value = false;

    return;
  }

  emit('success');
  dialog.value.close();

  busy.value = false;
  formError.value = {};
}

defineExpose({
  async open() {
    newPassword.value = '';
    newPasswordRepeat.value = '';
    password.value = '';
    busy.value = false;
    formError.value = {};
    dialog.value.open();

    setTimeout(checkValidity, 100); // update state of the confirm button
  }
});

</script>

<template>
  <Dialog ref="dialog"
    :title="$t('profile.changePassword.title')"
    :confirm-label="$t('main.dialog.save')"
    :confirm-active="!busy && isFormValid"
    :confirm-busy="busy"
     confirm-style="primary"
    :reject-label="$t('main.dialog.cancel')"
    :reject-active="!busy"
     reject-style="secondary"
     @confirm="onSubmit"
  >
    <form @submit.prevent="onSubmit" autocomplete="off" ref="form"  @input="checkValidity()">
      <fieldset :disabled="busy">
        <input type="submit" style="display: none;">

        <div class="error-label" v-if="formError.generic">{{ formError.generic }}</div>

        <FormGroup :has-error="formError.newPassword">
          <label>{{ $t('profile.changePassword.newPassword') }}</label>
          <PasswordInput v-model="newPassword" required/>
          <div class="error-label" v-if="formError.newPassword">{{ formError.newPassword }}</div>
        </FormGroup>

        <FormGroup :has-error="newPasswordRepeat.length !== 0 && newPassword !== newPasswordRepeat">
          <label>{{ $t('profile.changePassword.newPasswordRepeat') }}</label>
          <PasswordInput v-model="newPasswordRepeat" required />
          <div class="error-label" v-if="newPasswordRepeat.length && newPassword !== newPasswordRepeat">{{ $t('profile.changePassword.errorPasswordsDontMatch') }}</div>
        </FormGroup>

        <FormGroup :has-error="formError.password">
          <label>{{ $t('profile.changePassword.currentPassword') }}</label>
          <PasswordInput v-model="password" required />
          <div class="error-label" v-if="formError.password">{{ formError.password }}</div>
        </FormGroup>
      </fieldset>
    </form>
  </Dialog>
</template>
