<template>
  <div>
    <q-tabs v-if="$q.screen.gt.sm" v-model="tab">
      <q-tab
        v-for="(tabindex, index) in tabs"
        :key="'tab' + index"
        :name="tabindex.name"
        :label="tabindex.label"
      />
    </q-tabs>
    <div v-else class="fit row justify-end">
      <q-btn flat round icon="mdi-menu" @click="showDrawer = !showDrawer" />
    </div>
    <q-drawer v-model="showDrawer" side="right" behavior="mobile" @click="showDrawer = !showDrawer">
      <q-list v-model="tab">
        <q-item
          v-for="(tabindex, index) in tabs"
          :key="'tab' + index"
          :active="tab == tabindex.name"
          clickable
          @click="tab = tabindex.name"
        >
          <q-item-label>{{ tabindex.label }}</q-item-label>
        </q-item>
      </q-list>
    </q-drawer>
    <q-page padding class="fit row justify-center content-start items-start q-gutter-sm">
      <q-tab-panels
        v-model="tab"
        style="background-color: transparent"
        class="q-ma-none q-pa-none fit row justify-center content-start items-start"
        animated
      >
        <q-tab-panel name="create">
          <EditEvent :date="date" />
        </q-tab-panel>
        <q-tab-panel name="eventtypes">
          <ManageTypes title="Veranstaltungstyp" type="EventType" />
        </q-tab-panel>
        <q-tab-panel name="jobtypes">
          <ManageTypes title="Dienstart" type="JobType" />
        </q-tab-panel>
      </q-tab-panels>
    </q-page>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref } from 'vue';
import ManageTypes from '../components/management/ManageTypes.vue';
import EditEvent from '../components/management/EditEvent.vue';
import { hasPermission } from '@flaschengeist/api';
import { PERMISSIONS } from '../permissions';
import { Screen } from 'quasar';

export default defineComponent({
  name: 'EventManagement',
  components: { EditEvent, ManageTypes },
  props: {
    date: {
      type: String,
      required: false,
      default: undefined
    }
  },
  setup() {
    const tabs = computed(() => [
      { name: 'create', label: 'Veranstaltungen' },
      ...(hasPermission(PERMISSIONS.JOB_TYPE) ? [{ name: 'jobtypes', label: 'Dienstarten' }] : []),
      ...(hasPermission(PERMISSIONS.EVENT_TYPE) ? [{ name: 'eventtypes', label: 'Veranstaltungsarten' }] : [])
    ]);

    const drawer = ref<boolean>(false);
    const tab = ref<string>('create');

    const showDrawer = computed({
      get: () => {
        return !Screen.gt.sm && drawer.value;
      },
      set: (val: boolean) => {
        drawer.value = val;
      },
    });


    return {
      showDrawer,
      tab,
      tabs,
    };
  },
});
</script>
