<template>
  <div v-if="day">
    <v-card :color="color(day)" max-width="300px" min-width="300px">
      <v-card-title v-if="day.date" class="subtitle-1 font-weight-bold">
        {{ day.name }} den {{ day.date.getDate() }}.{{
          day.date.getMonth() + 1
        }}.{{ day.date.getFullYear() }}
      </v-card-title>
      <v-card-text>
        <v-autocomplete
          chips
          return-object
          multiple
          v-model="day.worker"
          :items="allUsers"
          item-text="fullName"
          label="Dienste"
          prepend-inner-icon="group_add"
          filled
          color="green"
        >
          <template v-slot:selection="data">
            <v-chip
              v-bind="data.attrs"
              :input-value="data.selected"
              close
              @click="data.select"
              @click:close="remove(data.item)"
            >
              {{ data.item.firstname }} {{ data.item.lastname }}
            </v-chip>
          </template>
          <template v-slot:item="data">
            <v-list-item-content>
              <v-list-item-title>
                {{ data.item.firstname }} {{ data.item.lastname }}
              </v-list-item-title>
            </v-list-item-content>
          </template>
        </v-autocomplete>
      </v-card-text>
    </v-card>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'Day',
  props: {
    day: Object
  },
  data() {
    return {}
  },
  created() {
    this.getUser({date: this.day.date.getTime()/1000})
  },
  methods: {
    ...mapActions({
      addUser: 'sm/addUser',
      getUser: 'sm/getUser',
      deleteUser: 'sm/deleteUser'
    }),
    // eslint-disable-next-line no-unused-vars
    remove(deletedUser) {
      const obj = this.day.worker.find(a => {
        return a.username === deletedUser.username
      })
      const index = this.day.worker.indexOf(obj)
      if (index >= 0) this.day.worker.splice(index, 1)
      this.deleteUser({startdatetime: this.day.date, date: this.day.date.getTime()/1000, user: deletedUser})
    },
    color: day => {
      if (day) {
        if (day.date.getDay() === 0 || day.date.getDay() === 1) {
          return 'grey lighten-4'
        } else {
          if (day.worker.length < 2) {
            return 'yellow'
          } else {
            return 'light-green'
          }
        }
      } else {
        return 'grey lighten-4'
      }
    }
  },
  computed: {
    ...mapGetters({
      allUsers: 'sm/allUsers'
    }),
    worker() {
      return this.day.worker
    }
  },
  watch: {
    worker(newValue, oldValue) {
      if (oldValue !== newValue) {
        let addedUser = null
        for (let user in newValue) {
          if (!oldValue.includes(newValue[user])) {
            addedUser = newValue[user]
            this.addUser({date: this.day.date.getTime()/1000, user: addedUser})
          }
        }
        let deletedUser = null
        for (let user in oldValue) {
          if (!newValue.includes(oldValue[user])) {
            deletedUser = oldValue[user]
            this.deleteUser({startdatetime: this.day.date, date: this.day.date.getTime()/1000, user: deletedUser})
          }
        }
      }
    }
  }
}
</script>

<style scoped></style>
