<template>
  <div class="mensa">
    <h3 id="date">{{date}}</h3>
      <div class="meal" v-for="meal in meals">
        <h3>{{meal.category}}</h3>
        <p>{{meal.title}}</p>
      </div>
  </div>
</template>

<script>
export default {
  name: 'mensa',
  data () {
    return {
      meals: [],
      date: '',
      days: [1, 2, 3, 4, 5, 6],
      dayOfWeek: {
        1: 'Montag',
        2: 'Dienstag',
        3: 'Mittwoch',
        4: 'Donnerstag',
        5: 'Freitag',
        6: 'Samstag'
      }
    }
  },
  watch: {
    '$route': function () {
      if (!this.$route.fullPath.endsWith('/')) {
        this.$router.replace(this.$route.fullPath + '/')
      }
      if (!this.$route.params.tag) {
        this.$router.push('' + (new Date().getDay() || 1))
      }
      this.loadMeals()
    }
  },
  methods: {
    loadMeals () {
      fetch(`/static/${this.$route.params.mensa}.${this.$route.params.tag}.json`)
      .then(res => res.json())
        .then(menu => {
          this.meals = menu.meals
          this.date = menu.date
        })
    }
  },
  created () {
    this.$router.replace(`/zentralmensa/${new Date().getDay() || 1}`)
    this.loadMeals()
  }
}
</script>
<style>
.meal{
  margin: 2em 0;
}

.meal>h3{
  margin-bottom:0em;
}

.meal>p{
  margin-top: 0.4em;
}

#date{
  margin-bottom: -1em;
}
.mensa{
  margin-bottom: 3em;
}
</style>
