<!--
 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-list-item two-line>
    <v-list-item-content>
      <v-list-item-title class="title">
        {{ emailTemplate.name }}
      </v-list-item-title>
      <v-list-item-subtitle class="subtitle-2 mt-2">
        {{ emailTemplate.description }}
      </v-list-item-subtitle>
    </v-list-item-content>
    <v-list-item-content>
      <v-list-item-title class="d-flex">
        <v-chip v-if="automaticStatusLabel">
          {{ automaticStatusLabel }}
        </v-chip>
      </v-list-item-title>
    </v-list-item-content>
    <v-list-item-content>
      <v-list-item-title class="d-flex">
        <v-chip
          v-if="notePage"
          :href="notePage.url"
          :title="notePage.title || notePage.name"
          target="_blank"
          rel="nofollow noreferrer noopener"
          class="primary--text my-2 mx-auto flex-grow-0"
          outlined>
          <v-icon
            class="ms-1 me-2"
            size="16"
            left>
            fas fa-clipboard
          </v-icon>
          <div class="text-truncate">{{ notePage.title || notePage.name }}</div>
        </v-chip>
      </v-list-item-title>
    </v-list-item-content>
    <v-list-item-action class="mx-0">
      <v-btn
        text
        icon
        @click="editEmailTemplate">
        <v-icon size="18">fas fa-edit</v-icon>
      </v-btn>
    </v-list-item-action>
    <v-list-item-action class="mx-0">
      <v-btn
        color="error"
        text
        icon
        @click="deleteEmailTemplate">
        <v-icon size="18">fas fa-trash</v-icon>
      </v-btn>
    </v-list-item-action>
  </v-list-item>
</template>
<script>
export default {
  props: {
    emailTemplate: {
      type: Object,
      default: () => null,
    },
  },
  data: () => ({
    sending: false,
  }),
  computed: {
    notePage() {
      return this.emailTemplate?.notePage;
    },
    automaticStatusLabel() {
      if (this.emailTemplate?.automaticStatus && this.emailTemplate?.automaticStatus !== 'NONE') {
        const statusChoice = this.$root.automaticStatuses.find(status => status.value === this.emailTemplate?.automaticStatus);
        return statusChoice?.text;
      }
      return null;
    },
  },
  methods: {
    editEmailTemplate() {
      this.$root.$emit('deed-email-template-edit', this.emailTemplate);
    },
    deleteEmailTemplate() {
      this.$root.$emit('deed-display-confirm', {
        title: 'deeds.provisioning.management.emailTemplateDeleteTitle',
        message: 'deeds.provisioning.management.emailTemplateDeleteDescription',
        ok: 'deeds.ok',
        cancel: 'deeds.cancel',
        callback: () => this.confirmDeleteEmailTemplate(),
      });
    },
    confirmDeleteEmailTemplate() {
      this.sending = true;
      return this.$tenantProvisioningEmailService.deleteEmailTemplate(this.emailTemplate.id)
        .then(() => {
          document.dispatchEvent(new CustomEvent('notification-alert', {detail: {
            type: 'success',
            message: this.$t('deeds.provisioning.management.emailTemplateDeleted'),
          }}));
        })
        .catch(() => {
          document.dispatchEvent(new CustomEvent('notification-alert', {detail: {
            type: 'error',
            message: this.$t('deeds.provisioning.management.emailTemplateDeleteError'),
          }}));
        })
        .finally(() => {
          this.$root.$emit('load-email-templates');
          this.sending = false;
        });
    },
  }
};
</script>