<!--
 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>
  <exo-drawer
    ref="previewMailDialog"
    drawer-width="100%">
    <template #title>
      {{ $t('deeds.provisioning.management.emailPreviewDialogTitle') }}
    </template>
    <template #content>
      <v-card-text>
        <v-row class="pa-0 ma-0">
          <v-col cols="1" class="px-0 py-1">{{ $t('deeds.provisioning.management.emailPreviewFrom') }}</v-col>
          <v-col class="px-0 py-1">{{ emailSender }}</v-col>
        </v-row>
        <v-row class="pa-0 ma-0">
          <v-col cols="1" class="px-0 py-1">{{ $t('deeds.provisioning.management.emailPreviewTo') }}</v-col>
          <v-col class="px-0 py-1">{{ emailReceiver }}</v-col>
        </v-row>
        <v-row v-if="emailCci" class="pa-0 ma-0">
          <v-col cols="1" class="px-0 py-1">{{ $t('deeds.provisioning.management.emailPreviewCci') }}</v-col>
          <v-col class="px-0 py-1">{{ emailCci }}</v-col>
        </v-row>
        <v-row class="pa-0 ma-0">
          <v-col cols="1" class="px-0 py-1">{{ $t('deeds.provisioning.management.emailPreviewSubject') }}</v-col>
          <v-col class="px-0 py-1">{{ emailSubjectText }}</v-col>
        </v-row>
      </v-card-text>
      <v-divider />
      <v-card
        :loading="loading"
        class="pa-4"
        flat>
        <div v-sanitized-html="emailContentHtml" class="mb-4"></div>
        <div v-sanitized-html="emailSignatureHtml"></div>
        <div>
          <img
            :src="emailSignatureLogoSrc" 
            alt="Logo"
            role="presentation"
            width="200px">
        </div>
      </v-card>
    </template>
    <template #footer>
      <div class="d-flex">
        <v-btn
          class="btn mx-auto"
          @click="close">
          {{ $t('deeds.close') }}
        </v-btn>
      </div>
    </template>
  </exo-drawer>
</template>
<script>
export default {
  props: {
    settings: {
      type: Object,
      default: null,
    },
  },
  data: () => ({
    deed: null,
    emailTemplate: null,
    notePage: null,
    loading: false,
  }),
  computed: {
    emailSender() {
      return `${this.settings?.emailSenderName} <${this.settings?.emailSenderEmail}>`;
    },
    emailReceiver() {
      return this.deed?.managerEmail;
    },
    emailCci() {
      return this.settings?.emailAdditionalReceivers || '';
    },
    emailSubjectText() {
      return this.emailTemplate?.mailSubject || '';
    },
    emailContentHtml() {
      return this.notePage?.content || '';
    },
    emailSignatureHtml() {
      return this.settings?.emailSenderSignatureText || '';
    },
    emailSignatureLogoSrc() {
      return '/deeds-tenant-provisioning/rest/api/provisioning/settings/logo';
    },
  },
  created() {
    this.$root.$on('open-tenant-management-mail-preview', this.open);
  },
  methods: {
    open(deed, emailTemplate) {
      this.deed = deed;
      this.emailTemplate = emailTemplate;
      this.notePage = emailTemplate?.notePage;
      this.$refs.previewMailDialog.open();
      this.refreshNotePage();
    },
    refreshNotePage() {
      if (this.notePage?.id) {
        this.loading = true;
        this.$notesService.getNoteById(this.notePage?.id)
          .then(data => this.notePage = data)
          .finally(() => this.loading = false);
      }
    },
    close() {
      this.$refs.previewMailDialog.close();
    },
  },
};
</script>
