<template>
  <b-card v-if="poste"
          :bg-variant="poste.variant"
          text-variant="white"
          class="text-center">
    <div class="card-text">
        <div class="icon">
          <font-awesome-icon :icon="poste.icon" class="fa-3x"/>
        </div>
        <div class="amount">
          <h3>{{ total() }}€</h3>
          {{ poste.name }}
        </div>
    </div>
  </b-card>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'box',
  props: [
    'postename',
    'rows'
  ],
  data () {
    return {
      default_poste: {
        name: 'Tout',
        variant: 'info',
        icon: 'file-invoice-dollar'
      }
    }
  },
  computed: {
    ...mapGetters('config', [
      'postes'
    ]),
    ...mapGetters('datas', [
      'tag_filter_rows'
    ]),
    poste () {
      if (this.postename) {
        return this.postes[this.postename]
      } else {
        return this.default_poste
      }
    }
  },
  methods: {
    filter_rows () {
      if (this.poste === this.default_poste) {
        return this.tag_filter_rows([])
      } else {
        return this.tag_filter_rows([this.poste.name])
      }
    },
    total () {
      return Math.round(
        -this.filter_rows()
          .map(x => parseFloat(x.Montant))
          .reduce((sum, x) => sum + x, 0)
      )
    },
    count () {
    }
  }
}

</script>

<style scoped>
.card-body {
  padding: 10px;
}
.card-text {
  display: flex;
}
.icon {
  flex: 40%;
  align-self: center;
}
.amount {
  flex: 50%;
  text-align: right;
  margin-left: 10px;
}
</style>
