Vue.component('resend-ticket-dialog', {
  props: ['transaction','shown'],
  inject: ['_f','_get','validate','base_uri','session_path','handleError'],
  data: () => ({
    sending: false,
    resendEmail: null,
    badmailList: [],
  }),
  computed: {
    isInWhiteLabel() {
      return localStorage.getItem('isInWhiteLabel')
    },
    whiteLabel() {
      return {
        white_label_name: localStorage.getItem('brandName'),
        white_label_color: localStorage.getItem('brandColor'),
        white_label_mode: localStorage.getItem('brandDark'),
        white_label_logo: localStorage.getItem('brandLogo')
      }
    }
  },
  mounted () {
		this.resendEmail = this.transaction.user_email;
  },
  methods: {
    resendTicket(transaction) {
      if (this.$refs['resend-ticket'].validate()) {
        swal({
          title: this.$t('ResendTicket.title', {email:this.resendEmail}),
          icon: 'info',
          buttons: {
            cancel: this.$t('general.cancel'),
            confirm: this.$t('general.confirm'),
          },
          dangerMode: true,
        }).then((confirm) => {
          if (confirm) {
            this.sending = true;
            axios.post("<?=$apiEndpoint;?>"+'event_transactions/resend', {
              token: transaction.token,
              email: this.resendEmail
            }).then(response => {
              this.sending = false;
              this.$emit('close');
              swal(this.$t('ResendTicket.success_title'),this.$t('ResendTicket.success_subtitle', {email:this.resendEmail}),"success");
            })
            .catch(err => {
              this.sending = false;
              this.handleError(err);
            });
          }
        });
      } else {
        this.$root.showToast({text: this.$t('form.invalid'), zindex:301}, "warning");
      }
    },
  },
  template:
`<v-dialog v-model="shown" persistent max-width="500">
  <v-card>
    <v-form ref="resend-ticket">
      <v-card-title class="title primary--text">
        <v-icon color="primary" class="mr-2">email</v-icon>{{$t('page.MyTickets.sendTicket')}}
      </v-card-title>
      <v-divider></v-divider>
      <v-card-text class="pt-2 pb-0">
        <v-alert class="mb-2 text-left font-weight-medium" transition="fade-transition" type="warning" :value="badmailList.filter(email=>(resendEmail != null && resendEmail.includes(email))).length > 0">
          {{$t('warning.badmail.ymail')}}
        </v-alert>
        <v-text-field
          type="email"
          :label="$t('ResendTicket.receive_email')"
          :rules="[
            v=>(!!v) || $t('validation.required',{field: $t('ResendTicket.receive_email')}),
            v => validate('email', v) || $t('validation.email', {field:$t('input.email.label')}),
            v => validate('email_characters', v) || $t('validation.email_characters', {field:$t('input.email.label')}),
          ]"
          hide-details="auto"
          v-model="resendEmail"
          @keypress.enter.prevent="resendTicket(transaction)"
        ></v-text-field>
      </v-card-text>
      <v-card-actions>
        <v-row no-gutters justify="center">
          <v-col>
            <v-btn
              block
              dark
              text
              color="grey"
              @click="$emit('close')"
              :disabled="sending"
            >{{$t('general.back')}}</v-btn>
          </v-col>
          <v-col>
            <v-btn
              block
              color="primary"
              :loading="sending"
              @click="resendTicket(transaction)"
            >{{$t('general.send')}}</v-btn>
          </v-col>
        </v-row>
      </v-card-actions>
    </v-form>
  </v-card>
</v-dialog>`
});