<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 { BalanceInterface, StateInterfaceBalance } from 'src/plugins/balance/store/balance';
import { Store, useStore } from 'vuex';
import { UserStateInterface } from 'src/plugins/user/store/user';

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

    //const store = <Store<StateInterfaceBalance>>root.$store;
    const store = useStore<Store<StateInterface>>();
    const balanceState = <BalanceInterface>store.state.balance;
    const userState = <UserStateInterface>store.state.user;

    const balance = computed(
      () => balanceState.balances.get(userState.currentUser.userid)?.balance || NaN
    );

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