<script setup>
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useStateStore } from 'stores/useStateStore';
import VnPaginate from 'src/components/ui/VnPaginate.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import CustomerFilter from './CustomerFilter.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import CardList from 'src/components/ui/CardList.vue';
import VnLinkPhone from 'src/components/ui/VnLinkPhone.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import CustomerSummary from './Card/CustomerSummary.vue';

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

function navigate(id) {
    router.push({ path: `/customer/${id}` });
}

const redirectToCreateView = () => {
    router.push({ name: 'CustomerCreate' });
};
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                :info="t('You can search by customer id or name')"
                :label="t('Search customer')"
                data-key="CustomerList"
            />
        </Teleport>
        <Teleport to="#actions-append">
            <div class="row q-gutter-x-sm">
                <QBtn
                    @click="stateStore.toggleRightDrawer()"
                    dense
                    flat
                    icon="menu"
                    round
                >
                    <QTooltip bottom anchor="bottom right">
                        {{ t('globals.collapseMenu') }}
                    </QTooltip>
                </QBtn>
            </div>
        </Teleport>
    </template>
    <QDrawer :width="256" show-if-above side="right" v-model="stateStore.rightDrawer">
        <QScrollArea class="fit text-grey-8">
            <CustomerFilter data-key="CustomerList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                auto-load
                data-key="CustomerList"
                order="id DESC"
                url="/Clients/filter"
            >
                <template #body="{ rows }">
                    <CardList
                        :id="row.id"
                        :key="row.id"
                        :title="row.name"
                        @click="navigate(row.id)"
                        v-for="row of rows"
                    >
                        <template #list-items>
                            <VnLv :label="t('customer.list.email')" :value="row.email" />
                            <VnLv :value="row.phone">
                                <template #label>
                                    {{ t('customer.list.phone') }}
                                    <VnLinkPhone :phone-number="row.phone" />
                                </template>
                            </VnLv>
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openCard')"
                                @click.stop="navigate(row.id)"
                                class="bg-vn-dark"
                                outline
                            />
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, CustomerSummary)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QPageSticky :offset="[20, 20]">
            <QBtn @click="redirectToCreateView()" color="primary" fab icon="add" />
            <QTooltip>
                {{ t('New client') }}
            </QTooltip>
        </QPageSticky>
    </QPage>
</template>

<i18n>
es:
    Search customer: Buscar cliente
    You can search by customer id or name: Puedes buscar por id o nombre del cliente
    New client: Nuevo cliente
</i18n>
