<template>
    <div class="row p-r-10">
        <div class="col p-r-5">
            <div class="row m-b-20">
                <div class="col-7">
                    <div class="row m-b-15">
                        <div class="col-12">
                            <div class="row m-b-5">
                                <div class="col p-r-0">
                                    <div class="btn btn-xs btn-outline-primary btn-block text-left b-rad-none p-t-0 p-b-0 p-l-15 p-r-15" @click="selectedSupplier.status = !selectedSupplier.status">
                                        <div class="row">
                                            <div class="col p-t-5 p-b-5 fs-9">
                                                {{selectedSupplier.name}} <template v-if="selectedSupplier.reference">- {{selectedSupplier.reference}}</template>
                                            </div>
                                            <div class="col-auto b-l b-success">
                                                <div class="row h-100 align-items-center">
                                                    <div class="col">
                                                        <i class="fa" :class="[{'fa-angle-down': !selectedSupplier.status}, {'fa-angle-up': selectedSupplier.status}]"></i>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="relative w-100">
                                        <div class="absolute w-100 b-l b-b b-r b-primary" :class="[{'hide': !selectedSupplier.status}]" style="top: 100%; right: 0; z-index: 1;">
                                            <div class="row text-left no-margin bg-white">
                                                <div class="col no-padding">
                                                    <div class="row no-margin" v-for="supplier in suppliers"  :key="supplier.id" :data="supplier">
                                                        <div class="col b-b b-grey p-t-10 p-b-10 pointer hover-t-10 p-b-10" :class="[{'bg-primary-light': selectedSupplier.id === supplier.id}, {'text-white': selectedSupplier.id === supplier.id}, {'hover-primary': selectedSupplier.id !== supplier.id}]" @click="updateSupplier(supplier)">
                                                            <div class="row align-items-center justify-content-center">
                                                                <div class="col">
                                                                    <div class="font-heading fs-10">{{supplier.name}} - {{supplier.reference}}</div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col" v-if="section === 'paymentInProgressBillGroupList'">
                            <list-component :key="key" section="paymentInProgressBillGroupListSection" :endpoint="route('api.transaction.group.bill.list')" :options="{per_page: 20, status: 0, ...(selectedSupplier.id && {issuer_in: [selectedSupplier.id]}), order_by: {column: 'id', DESC: true}}">
                                <template #list="{data}">
                                    <bill-group-component :data="data" :section="section" :selectedBillGroup="selectedBillGroup" @select="selectBillGroup($event)"></bill-group-component>
                                </template>
                            </list-component>
                        </div>
                        <div class="col" v-if="section === 'paymentVerificationBillGroupList'">
                            <list-component :key="key" section="paymentVerificationBillGroupListSection" :endpoint="route('api.transaction.group.bill.list')" :options="{per_page: 20, has_pending_verify_transaction: true, ...(selectedSupplier.id && {issuer_in: [selectedSupplier.id]}), order_by: {column: 'id', DESC: true}}">
                                <template #list="{data}">
                                    <bill-group-component :data="data" :section="section" :selectedBillGroup="selectedBillGroup" @select="selectBillGroup($event)"></bill-group-component>
                                </template>
                            </list-component>
                        </div>
                        <div class="col" v-if="section === 'paidBillGroupList'">
                            <list-component :key="key" section="paidBillGroupListSection" :endpoint="route('api.transaction.group.bill.list')" :options="{per_page: 20, status_in: [2, 3], ...(selectedSupplier.id && {issuer_in: [selectedSupplier.id]}), order_by: {column: 'id', DESC: true}}">
                                <template #list="{data}">
                                    <bill-group-component :data="data" :section="section" :selectedBillGroup="selectedBillGroup" @select="selectBillGroup($event)"></bill-group-component>
                                </template>
                            </list-component>
                        </div>
                    </div>
                </div>
                <div class="col-12 col-md">
                    <bill-group-payment-summary-component :selectedBillGroup="selectedBillGroup" :refreshList="updateList" :section="section"></bill-group-payment-summary-component>
                </div>
            </div>
        </div>
    </div>
</template>

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

const props = defineProps<{
    section: string;
}>();

const uiStore = useUiStore();
const { submit } = useRequest();

const suppliers = ref<any[]>([]);
const selectedSupplier = ref({
    id: '',
    name: 'Select Supplier',
    reference: '',
    status: false
});
const selectedBillGroup = ref({
    id: ''
});
const key = ref(1);

const pendingQueue = computed(() => uiStore.isShowing(props.section));

const updateList = () => {
    selectedSupplier.value.status = false;
    selectedBillGroup.value = { id: '' };
    key.value++;
};

const successHandler = (response: any) => {
    suppliers.value = response.payload.data;
};

const updateSupplier = (supplier: any) => {
    if (supplier.id !== selectedSupplier.value.id) {
        selectedSupplier.value = { ...supplier, status: false };
        updateList();
    } else {
        selectedSupplier.value.status = false;
    }
};

const selectBillGroup = (billGroup: any) => {
    selectedBillGroup.value = billGroup;
};

watch(pendingQueue, () => {
    updateList();
});

onMounted(() => {
    const filters = JSON.stringify({ 'business_type': 3, 'status_in': [1, 2, 0] });
    submit(route('api.company.list') + '?filters=' + filters, 'get', 'transactionGroupsListPaymentSection', false, false, undefined, { successHandler });
});
</script>
