<!--
 This file is part of the Meeds project (https://meeds.io/).
 
 Copyright (C) 2020 - 2022 Meeds Association contact@meeds.io
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 3 of the License, or (at your option) any later version.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public License
 along with this program; if not, write to the Free Software Foundation,
 Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-->
<template>
  <v-app>
    <div class="d-flex flex-column white fill-height ma-8 px-2">
      <deed-provisioning-management-navbar class="flex-grow-0" />
      <deed-provisioning-management-page
        :email-templates="emailTemplates"
        :settings="settings"
        class="flex-grow-1" />
      <div v-if="selectedTask" id="TasksManagementPortlet">
        <task-drawer ref="taskDrawer" :task="selectedTask" />
      </div>
    </div>
  </v-app>
</template>
<script>
export default {
  data: () => ({
    selectedTask: null,
    emailTemplates: null,
    settings: null,
  }),
  watch: {
    selectedTask() {
      if (this.selectedTask) {
        this.$nextTick(() => {
          this.$refs.taskDrawer.open(this.selectedTask);
        });
      }
    }
  },
  created() {
    document.title = this.$t('deeds.provisioning.management.page.title');
    this.$root.$on('open-deed-task', this.openTask);
    this.$root.$on('load-settings', this.loadSettings);
    this.$root.$on('load-email-templates', this.loadEmailTemplates);
    this.loadSettings();
    this.loadEmailTemplates();
  },
  methods: {
    loadEmailTemplates() {
      return this.$tenantProvisioningEmailService.getEmailTemplates()
        .then(emailTemplates => this.emailTemplates = emailTemplates || []);
    },
    loadSettings() {
      return this.$tenantProvisioningSettingsService.getSettings()
        .then(settings => this.settings = settings);
    },
    openTask(task) {
      this.selectedTask = null;
      this.$nextTick().then(() => this.selectedTask = task);
    },
  },
};
</script>