<template>
    <div class="row m-b-10 parentContainer">
        <div class="col">
            <loading-component style="height: 200px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row align-items-center pointer p-b-5 b-b b-grey" v-show="!isLoading" @click="activate($event)">
                <div class="col-auto p-r-0">
                    <div class="b-grey b-a fs-10 btn-rounded icon-thumbnail icon-25 m-r-0" :class="[{'bg-primary': active}, {'bg-transparent': !active}]">
                        <i class="fa fa-check text-white fs-12 fa-fw"></i>
                    </div>
                </div>
                <div class="col">
                    <div class="row m-b-10">
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Date</div>
                            <div class="font-heading fs-10">
                                {{item.created_at}}
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Supplier</div>
                            <div class="font-heading fs-10">
                                {{item.issuer_name}}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Currency Rate</div>
                            <div class="font-heading fs-10">
                                {{item.currency_rate}}
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Currency Amount</div>
                            <div class="font-heading fs-10">
                                {{item.original_currency.short_code}} {{(Math.round((item.original_amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Transfer Fee</div>
                            <div class="font-heading fs-10">
                                {{item.original_currency.short_code}} {{(Math.round((item.transfer_fee + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
                            </div>
                        </div>
                        <div class="col text-right">
                            <div class="font-heading fs-10 muted all-caps">Amount</div>
                            <div class="font-heading fs-14 text-success bold">
                                {{item.currency.short_code}} {{((Math.round(( item.amount + Number.EPSILON) * 100) / 100)).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">reference</div>
                            <document-file-viewer-component v-if="item.documents.currency_order" :file="item.documents.currency_order.files[0]">
                                <template #button>
                                    <button class="btn btn-xs btn-default bg-master-lightest b-rad-none no-border">
                                        <i class="fa fa-eye"></i>
                                    </button>
                                </template>
                            </document-file-viewer-component>
                        </div>
                        <div class="col">
                            <div class="row">
                                <div class="col">
                                    <div class="font-heading fs-10 muted all-caps">PO Completion</div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-auto p-r-0" v-if="item.documents.purchase_order" >
                                    <document-file-viewer-component :file="item.documents.purchase_order.files[0]">
                                        <template #button>
                                            <button class="btn btn-xs btn-default bg-master-lightest b-rad-none no-border">
                                                <i class="fa fa-file-pdf-o"></i>
                                            </button>
                                        </template>
                                    </document-file-viewer-component>
                                </div>
                                <div class="col">
                                    <p class="font-heading fs-12 bold text-success pointer" @click="expand($event)"><span :class="[{'text-danger': item.complete_transactions.length !== item.transactions.length}]">{{item.complete_transactions.length}}</span>/{{item.transactions.length}}</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="row parentContainer">
                                <div class="col p-l-0">
                                    <button class="btn btn-xs btn-primary b-rad-none requestModal" data-type="editTransactionGroup">
                                        Edit Transfer Fee
                                    </button>
                                    <div class="do-not-activate">
                                        <modal-component small type="editTransactionGroup">
                                            <edit-transfer-fee-form-component :section="section" :transfer_fee="item.transfer_fee" :id="item.id" :supplier_id="item.issuer_id" :rate="item.currency_rate"></edit-transfer-fee-form-component>
                                        </modal-component>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row" v-if="expanded">
                        <div class="col">
                            <div class="row" v-for="transaction in item.transactions">
                                <div class="col">
                                    <a :href="route('booking.details', transaction)" target="_blank">
                                        <span :class="[{'text-success': item.complete_transactions.includes(transaction)}, {'text-danger': !item.complete_transactions.includes(transaction)}]">{{ transaction }}</span>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useUiStore } from '@/stores/ui';
import route from '@/general/functions/router';
import { useComponentHandler } from '@/composables/useComponentHandler';

const props = defineProps<{
    data: any;
    section: string;
    payments: any[];
    emptyPaymentList: () => void;
}>();

const emit = defineEmits<{
    (e: 'update:modelValue', value: any): void;
}>();

const uiStore = useUiStore();
const { item } = useComponentHandler(props);
const isLoading = computed(() => uiStore.isLoading(props.section));

const expanded = ref(false);

const active = computed(() => {
    return props.payments.some(payment => payment.id === item.value.id);
});

const activate = (event: Event) => {
    const target = event.target as HTMLElement;
    if ((target.tagName.toLowerCase() === 'button' && target.getAttribute('data-type') === 'editTransactionGroup') || Boolean(target.closest('.do-not-activate'))) {
        props.emptyPaymentList();
        return;
    }
    emit('update:modelValue', item.value);
};

const expand = (event: Event) => {
    expanded.value = !expanded.value;
    event.stopPropagation();
};
</script>
