<i18n>
{
  "en": {
    "statements": {
      "CommentDialog": {
        "dialogTitle": "Adding a comment to the status",
        "addComment": "Add",
        "cancelComment": "Cancel"
      }
    }
  },
  "ru": {
    "statements": {
      "CommentDialog": {
        "dialogTitle": "В следующих начислениях не выбран обязательный параметр:",
        "addComment": "Добавить",
        "cancelComment": "Отменить"
      }
    }
  }
}
</i18n>

<template>
  <div>
    <q-btn
      class="q-ml-sm"
      padding="6px"
      outline
      :icon="get(buttonSettings, 'props.icon')"
      :label="get(buttonSettings, 'props.label')"
      :color="get(buttonSettings, 'props.color')"
      :disable="
        disableAllButtons ||
        !isAllPaymentsEPGUChecked ||
        !isBillPurposeSuitableLength
      "
      @click="doAction"
    >
      <q-badge class="badge" color="white" floating>
        <q-icon
          v-if="isAllPaymentsEPGUChecked && isBillPurposeSuitableLength"
          name="check_circle_outline"
          color="positive"
          style="font-size: 16px; position: relative; left: -4px"
        />
        <q-icon
          v-else
          name="error_outline"
          color="negative"
          style="font-size: 16px; position: relative; left: -4px"
        />
      </q-badge>
    </q-btn>
    <q-tooltip
      max-width="12rem"
      v-if="
        payments.length &&
        (!isAllPaymentsEPGUChecked || !isBillPurposeSuitableLength)
      "
    >
      <template v-if="!isAllPaymentsEPGUChecked">
        В начислении(-ях): <br />
        {{ tooltipText }} <br />
        не выбран обязательный параметр <br /><br />
      </template>
      <template v-if="!isBillPurposeSuitableLength">
        Длина поля "Назначение платежа" более 210 символов, регистрация
        начисления невозможна
      </template>
    </q-tooltip>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Watch, Vue } from 'vue-property-decorator';
import { BaseCardCommon } from '@/interfaces';
import { Payment } from '@openapi/mz-payment-service'
import get from 'lodash/get';
import { cloneDeep } from 'lodash';

interface PaymentWithEPGU extends Payment {
  EPGUPaymentRequired: boolean;
}

@Component({
  methods: { get },
})
export default class ChargeApproveDialog extends Vue {
  @Prop() common: BaseCardCommon
  @Prop() model: any;
  @Prop() disableAllButtons: boolean;
  @Prop() buttonSettings: any;

  payments: PaymentWithEPGU[] = [];
  isShowDialog = false;
  loading = false;

  created(): void {
    this.setLocalpayments();
  }

  @Watch('model.payments')
  setLocalpayments(): void {
    this.payments = cloneDeep(this.model.payments);
  }

  get isAllPaymentsEPGUChecked(): boolean {
    if (!this.payments.length) return false;
    return this.payments?.every((el) => {
      if (typeof el.EPGUPaymentRequired === 'undefined') return false; 
      return true;
    });
  }

  get isBillPurposeSuitableLength(): boolean {
    if (!this.payments.length) return true;
    return this.payments?.every((payment) => {
      return (
        get(payment, 'charge.billFor', this.$t('ChargesCard.noData')).length <
        210
      );
    });
  }

  get tooltipText(): string {
    if (!this.payments.length) return '';
    const paymentsId = this.payments
      .filter((el) => typeof el.EPGUPaymentRequired === 'undefined')
      .map((el) => el.charge?.supplierBillId);
    return ` ${paymentsId.join(', ')}`;
  }

  onSubmit(): void {
    this.$emit('submit', { buttonId: 'registerPayments' });
    this.isShowDialog = false;
  }

  onCancel(): void {
    this.isShowDialog = false;
  }

  doAction(): void {
    if (!this.isAllPaymentsEPGUChecked) {
      this.isShowDialog = true;
    } else {
      this.onSubmit();
    }
  }
}
</script>
<style lang="sass">
.badge
  border-radius: 50%
  width: 18px
  height: 18px
  top: -8px
  right: -8px
</style>