<!--
 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-flex class="d-flex flex-column border-box-sizing pb-8 pt-4">
    <v-card class="flex-grow-1 d-flex" flat>
      <v-card-title class="title">
        <span class="title">{{ $t('deeds.provisioning.management.emailSettings') }}</span>
        <v-btn
          :loading="loadingSettings"
          :title="$t('deeds.provisioning.management.emailSettingsButtonTitle')"
          icon
          class="my-auto ms-6"
          v-bind="attrs"
          v-on="on"
          @click="openManageEmailDrawer">
          <v-icon>fas fa-cog</v-icon>
        </v-btn>
      </v-card-title>
    </v-card>
    <v-card class="flex-grow-1 d-flex flex-column" flat>
      <v-card-title>
        <span class="title">{{ $t('deeds.provisioning.management.emailTemplates') }}</span>
        <v-btn
          :title="$t('deeds.provisioning.management.emailTemplatesAddButtonTitle')"
          icon
          class="my-auto ms-6"
          v-bind="attrs"
          v-on="on"
          @click="openAddEmailTemplateDrawer">
          <v-icon>fas fa-plus</v-icon>
        </v-btn>
      </v-card-title>
      <v-card-subtitle class="subtitle">{{ $t('deeds.provisioning.management.emailTemplatesSubTitle') }}</v-card-subtitle>
      <v-card-text v-if="hasTemplates || !initialized">
        <v-progress-liner
          v-if="loadingTemplates || !initialized"
          color="primary"
          indeterminate />
        <v-list
          v-if="initialized"
          class="ms-0 ms-md-4 pa-0">
          <deed-provisioning-management-settings-email-template-item
            v-for="emailTemplate in emailTemplates"
            :key="emailTemplate.title"
            :email-template="emailTemplate" />
        </v-list>
      </v-card-text>
      <v-card-text v-else class="d-flex align-center justify-center py-12">
        <v-alert
          type="info"
          elevation="0"
          class="position-static"
          outlined>
          {{ $t('deeds.provisioning.management.emailTemplatesEmpty') }}
        </v-alert>
      </v-card-text>
    </v-card>
    <deed-provisioning-management-settings-email-drawer
      ref="emailSettingsDrawer"
      :settings="settings" />
    <deed-provisioning-management-settings-email-template-drawer
      ref="emailTemplateFormDrawer" />
    <deed-provisioning-management-settings-note-tree-drawer
      ref="emailTemplateNoteTree" />
  </v-flex>
</template>
<script>
export default {
  props: {
    emailTemplates: {
      type: Array,
      default: () => null,
    },
    settings: {
      type: Object,
      default: null,
    },
  },
  data: () => ({
    loadingTemplates: false,
  }),
  computed: {
    initialized() {
      return this.emailTemplates !== null;
    },
    hasTemplates() {
      return !!this.emailTemplates?.length;
    },
  },
  watch: {
    emailTemplates() {
      this.loadingTemplates = false;
    },
    settings() {
      this.loadingSettings = false;
    },
  },
  methods: {
    openManageEmailDrawer() {
      this.$refs.emailSettingsDrawer.open();
    },
    openAddEmailTemplateDrawer() {
      this.$refs.emailTemplateFormDrawer.open();
    },
  }
};
</script>