<template>
  <ds-form v-model="formData" :schema="formSchema" @submit="handleSubmit" @input="handleInput">
    <template>
      <ds-input
        id="oldPassword"
        model="oldPassword"
        type="password"
        :label="$t('settings.security.change-password.label-old-password')"
      />
      <ds-input
        id="newPassword"
        model="newPassword"
        type="password"
        :label="$t('settings.security.change-password.label-new-password')"
      />
      <ds-input
        id="confirmPassword"
        model="confirmPassword"
        type="password"
        :label="$t('settings.security.change-password.label-new-password-confirm')"
      />
      <password-strength :password="formData.newPassword" @change="handlePasswordStrength"/>
      <ds-space margin-top="base">
        <ds-button
          :loading="loading"
          :disabled="disabled"
          primary
        >{{ $t('settings.security.change-password.button') }}</ds-button>
      </ds-space>
    </template>
  </ds-form>
</template>

<script>
import gql from 'graphql-tag'
import PasswordStrength from './Strength'

export default {
  name: 'ChangePassword',
  components: {
    PasswordStrength
  },
  data() {
    return {
      formData: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      formSchema: {
        oldPassword: {
          required: true,
          message: this.$t(
            'settings.security.change-password.message-old-password-required'
          )
        },
        newPassword: {
          required: true,
          message: this.$t(
            'settings.security.change-password.message-new-password-required'
          )
        },
        confirmPassword: {
          required: true,
          message: this.$t(
            'settings.security.change-password.message-new-password-confirm-required'
          )
        }
      },
      loading: false,
      disabled: true
    }
  },
  methods: {
    async handleInput(data) {
      console.log('validation')
      console.log(data)
      if (data.newPassword && data.newPassword === data.confirmPassword) {
        this.disabled = false
      } else {
        this.disabled = true
      }
    },
    async handleSubmit(data) {
      this.loading = true
      const mutation = gql`
        mutation($oldPassword: String!, $newPassword: String!) {
          changePassword(oldPassword: $oldPassword, newPassword: $newPassword)
        }
      `
      const variables = this.formData

      try {
        const { data } = await this.$apollo.mutate({ mutation, variables })
        this.$store.commit('auth/SET_TOKEN', data.changePassword)
        this.$toast.success(
          this.$t('settings.security.change-password.success')
        )
      } catch (err) {
        this.$toast.error(err.message)
      } finally {
        this.loading = false
      }
    },
    handlePasswordStrength(e) {
      console.log(e.isSecure)
    }
  }
}
</script>
