<template>
  <q-card style="text-align: center">
    <q-card-section>
      <div class="text-h6">Gerücht: {{ balance.toFixed(2) }} €</div>
    </q-card-section>
  </q-card>
</template>

<script lang="ts">
import { computed, defineComponent, onBeforeMount } from 'vue';
import { StateInterfaceBalance } from 'src/plugins/balance/store/balance';
import { Store } from 'vuex';

export default defineComponent({
  name: 'BalanceWidget',
  setup(_, { root }) {
    onBeforeMount(() => {
      store.dispatch('balance/getBalance').catch((err) => {
        console.warn(err);
      });
    });

    const store = <Store<StateInterfaceBalance>>root.$store;

    const balance = computed(
      () =>
        store.state.balance.balances.get((<FG.User>store.state.user.currentUser).userid)?.balance ||
        NaN
    );

    return { balance };
  },
});
</script>
