<template>
  <div>
    <slot/>
  </div>
</template>

<script>
import {user_is_above_town, user_is_admin} from "@/utils/user";

export default {
  name: 'ManagerFramework',
  props: {
    num: {
      type: Number,
      default: 0,
    },
    index: {
      type: Number,
      default: 0,
    },
  },
  computed: {
    isAboveTown() {
      return user_is_above_town();
    },
    allowSwitch() {
      return user_is_admin();
    },
  },
  data() {
    return {
      currentIndex: user_is_above_town() ? 0 : 1,
      func: null,
    };
  },
  methods: {
    switch(evt) {
      if(!this.allowSwitch)
        return;
      if(evt.shiftKey && evt.altKey)
      {
        if(evt.keyCode === 39)
        {
          this.currentIndex = (this.currentIndex + 1) % this.num;
          this.$emit('update:index', this.currentIndex);
        }
        else if(evt.keyCode === 37)
        {
          this.currentIndex = (this.currentIndex + this.num - 1) % this.num;
          this.$emit('update:index', this.currentIndex);
        }
        else if(evt.keyCode >= 49 && evt.keyCode <= 57)
        {
          if(evt.keyCode - 49 < this.num)
          {
            this.currentIndex = (evt.keyCode - 49) % this.num;
            this.$emit('update:index', this.currentIndex);
          }
        }
        //console.log(evt);
      }
    }
  },
  created() {
    this.$emit('update:index', this.currentIndex);
  },
  mounted() {
    if(this.allowSwitch)
    {
      addEventListener('keyup', this.switch);
      this.func = this.switch;
    }
  },
  beforeDestroy() {
    if(this.func)
      removeEventListener('keyup', this.func);
  }
}
</script>
