<template>
  <BContainer class="container-fluid">
    <BRow align-h="center" class="change-password">
      <BCol class="change-password">
        <form class="form"  autocomplete="off" @submit.prevent="submitUser">
          <h1 class="title">Cambiar contraseña</h1>
          <FormInput
            v-model="form.old_password"
            type="text"
            label-text="Contraseña actual"
            required
            @input="form.old_password = $event"
          />
          <FormInput
            v-model="form.password"
            type="password"
            label-text="Nueva contraseña"
            required
            @input="form.password = $event"
          />
          <FormInput
            v-model="form.password2"
            type="password"
            label-text="Nueva contraseña"
            required
            @input="form.password2 = $event"
          />
          <small v-if="error" class="error">{{ error }}</small>
          <small v-if="success" class="success">{{ success }}</small>
          <div class="submit-btn">
            <SubmitButton text="Actualizar" image-url="" />
          </div>
        </form>
      </BCol>
    </BRow>
  </BContainer>
</template>

<script>
import { mapState } from 'pinia'
import { useAuthStore } from '@/stores/auth'
export default {
  setup() {
    definePageMeta({
      layout: 'editar',
      middleware: 'auth',
      auth: { authority: 'SHOP_USER' },
    })
  },
  data() {
    return {
      form: {
        old_password: null,
        password: null,
        password2: null,
      },
      error: null,
      success: null,
    }
  },
  computed: {
    ...mapState(useAuthStore, ['id', 'access']),
  },
  methods: {
    async submitUser() {
      this.error = null
      this.success = null
      if (this.form.password === this.form.password2) {
        try {
          const config = useRuntimeConfig()
          const userId = this.id
          const access = this.access
          await $fetch(`/user/change_password/${userId}/`, {
            baseURL: config.public.baseURL,
            method: 'PUT',
            body: this.form,
            headers: {
              Authorization: `Bearer ${access}`
            }
          })
          this.success = 'Contraseña cambiada correctamente'
        } catch (error) {
          console.error('Error cambiando la contraseña:', error)
          this.error = 'La contraseña actual no es correcta'
        }
      } else {
        this.error = 'La nueva contraseña no coincide'
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.container-fluid {
  margin: 5rem 0 5rem 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient($color-consumo-base-light, $color-bg-light);
  border-radius: 1rem;
  padding: 2rem;
  gap: 3rem;
  color: $color-primary;

  @include mobile {
    margin-top: 7rem;
  }
}

.change-password {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.title {
  color: $color-primary;
  font-size: $h2;
  margin-bottom: 40px;
  text-align: center;
  width: 100%;
  @include mobile {
    width: 80%;
    margin-top: 70px;
  }
  @include desktop {
    width: 40%;
  }
}
.form {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 40px;
  label {
    text-align: left;
    color: $color-primary;
    font-weight: $bold;
    font-size: $xs;
  }
}
.error {
  color: crimson;
}
.success {
  color: green;
}
</style>
