<script setup>
import { useI18n } from 'vue-i18n';
import { onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';
import { useStateStore } from 'stores/useStateStore';
import { toCurrency, toDate } from 'src/filters';
import CardList from 'components/ui/CardList.vue';
import WorkerDescriptorProxy from 'pages/Worker/Card/WorkerDescriptorProxy.vue';
import CustomerDescriptorProxy from 'pages/Customer/Card/CustomerDescriptorProxy.vue';
import VnPaginate from 'components/ui/VnPaginate.vue';
import VnLv from 'components/ui/VnLv.vue';
import OrderSearchbar from 'pages/Order/Card/OrderSearchbar.vue';
import OrderFilter from 'pages/Order/Card/OrderFilter.vue';
import OrderSummary from 'pages/Order/Card/OrderSummary.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';

const stateStore = useStateStore();
const router = useRouter();
const { t } = useI18n();
const { viewSummary } = useSummaryDialog();

onMounted(() => (stateStore.rightDrawer = true));
onUnmounted(() => (stateStore.rightDrawer = false));

function navigate(id) {
    router.push({ path: `/order/${id}` });
}
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <OrderSearchbar />
        </Teleport>
        <Teleport to="#actions-append">
            <div class="row q-gutter-x-sm">
                <QBtn
                    flat
                    @click="stateStore.toggleRightDrawer()"
                    round
                    dense
                    icon="menu"
                >
                    <QTooltip bottom anchor="bottom right">
                        {{ t('globals.collapseMenu') }}
                    </QTooltip>
                </QBtn>
            </div>
        </Teleport>
    </template>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <OrderFilter data-key="OrderList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                data-key="OrderList"
                url="Orders/filter"
                :limit="20"
                :order="['landed DESC', 'clientFk', 'id DESC']"
                :user-params="{ showEmpty: false }"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :id="row.id"
                        :title="`${row?.clientName} (${row?.clientFk})`"
                        @click="navigate(row.id)"
                    >
                        <template #list-items>
                            <VnLv
                                :label="t('order.field.salesPersonFk')"
                                :title-label="t('order.field.salesPersonFk')"
                            >
                                <template #value>
                                    <span class="link" @click.stop>
                                        {{ row?.name || '-' }}
                                        <WorkerDescriptorProxy :id="row?.salesPersonFk" />
                                    </span>
                                </template>
                            </VnLv>
                            <VnLv
                                :label="t('order.field.clientFk')"
                                :title-label="t('order.field.clientFk')"
                            >
                                <template #value>
                                    <span class="link" @click.stop>
                                        {{ row?.clientName || '-' }}
                                        <CustomerDescriptorProxy :id="row?.clientFk" />
                                    </span>
                                </template>
                            </VnLv>
                            <VnLv
                                :label="t('order.field.isConfirmed')"
                                :value="row?.isConfirmed === 1"
                            />
                            <VnLv
                                :label="t('order.field.created')"
                                :value="toDate(row?.created)"
                            />
                            <VnLv :label="t('order.field.landed')">
                                <template #value>
                                    <QBadge color="positive" dense>
                                        {{ toDate(row?.landed) }}
                                    </QBadge>
                                </template>
                            </VnLv>
                            <VnLv
                                :label="t('order.field.hour')"
                                :value="row.hourTheoretical || row.hourEffective"
                            />
                            <VnLv
                                :label="t('order.field.agency')"
                                :value="row?.agencyName"
                            />
                            <VnLv
                                :label="t('order.field.total')"
                                :value="toCurrency(row?.total)"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, OrderSummary)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QPageSticky :offset="[20, 20]">
            <RouterLink :to="{ name: 'OrderCreate' }">
                <QBtn fab icon="add" color="primary" />
                <QTooltip>
                    {{ t('order.list.newOrder') }}
                </QTooltip>
            </RouterLink>
        </QPageSticky>
    </QPage>
</template>
