<template>
  <div>
    <v-row justify="space-around">
      <v-card>
        <v-card-title class="text-center">
          Anzahl Freigetränke
        </v-card-title>
        <v-card-text class="text-h2 text-center">
          {{free_drink_list_history_job_without_canceled.length}}
        </v-card-text>
      </v-card>
      <v-card>
        <v-card-title class="text-center">
          Summe Freigetränke
        </v-card-title>
        <v-card-text class="text-h2 text-center">
          {{ (sum/100).toFixed(2)}} €
        </v-card-text>
      </v-card>
    </v-row>
    <v-data-table :headers="header" :items="table"/>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
name: "FreeDrinkListJob",
data() {
  return {
    header: [
      {
        text: 'Datum',
        value: 'date'
      },
      {
        text: 'Label Freigetränk',
        value: 'label'
      },
      {
        text: 'Freigetränk',
        value: 'name'
      },
      {
        text: 'Anzahl',
        value: 'count'
      },
      {
        text: 'Preis pro Getränk',
        value: 'pricepro'
      },
      {
        text: 'Preis gesamt',
        value: 'sum'
      }
    ]
  }
},
computed: {
  ...mapGetters('freeDrinkList', ['free_drink_list_history_job_without_canceled', 'free_drink_list_config']),
  sum() {
    let sum = 0
    this.free_drink_list_history_job_without_canceled.forEach(item => {
      sum += item.free_drink_config.price
    })
    return sum
  },
  table() {
    if (this.free_drink_list_history_job_without_canceled.length == 0) {
      return []
    }
    let retVal = []
    const date_month = this.free_drink_list_history_job_without_canceled[0].timestamp
    let days = new Date(date_month.getFullYear(), date_month.getMonth() + 1, 0).getDate()
    for (let day = 1; day <= days; day++) {
      let from = new Date(date_month.getFullYear(), date_month.getMonth(), day)
      let to = new Date(date_month.getFullYear(), date_month.getMonth(), day + 1)
      let history_of_date = this.free_drink_list_history_job_without_canceled.filter(item => {
        return item.timestamp >= from && item.timestamp <= to && !item.canceled
      })
      this.free_drink_list_config.forEach(drink_config => {
        let history_of_config = history_of_date.filter(item => {
          return item.free_drink_config_id == drink_config.id
        })
        if (history_of_config.length > 0) {
          retVal.push({
            date: `${from.getDate()}.${from.getMonth() + 1}.${from.getFullYear()}`,
            label: drink_config.label,
            name: drink_config.drink.name,
            count: history_of_config.length,
            pricepro: (drink_config.price / 100).toFixed(2),
            sum: (drink_config.price / 100 * history_of_config.length).toFixed(2)
          })
        }
      })
    }
    return retVal
  }
}
}
</script>

<style scoped>

</style>