<script setup>
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useQuasar } from 'quasar';
import { useStateStore } from 'stores/useStateStore';
import Paginate from 'src/components/PaginateData.vue';
import CustomerSummaryDialog from './Card/CustomerSummaryDialog.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import CustomerFilter from './CustomerFilter.vue';

const stateStore = useStateStore();
const router = useRouter();
const quasar = useQuasar();
const { t } = useI18n();

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

function viewSummary(id) {
    quasar.dialog({
        component: CustomerSummaryDialog,
        componentProps: {
            id,
        },
    });
}
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="CustomerList"
                :label="t('Search customer')"
                :info="t('You can search by customer id or name')"
            />
        </Teleport>
        <Teleport to="#actions-append">
            <div class="row q-gutter-x-sm">
                <q-btn
                    flat
                    @click="stateStore.toggleRightDrawer()"
                    round
                    dense
                    icon="menu"
                >
                    <q-tooltip bottom anchor="bottom right">
                        {{ t('globals.collapseMenu') }}
                    </q-tooltip>
                </q-btn>
            </div>
        </Teleport>
    </template>
    <q-drawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <q-scroll-area class="fit text-grey-8">
            <CustomerFilter data-key="CustomerList" />
        </q-scroll-area>
    </q-drawer>
    <q-page class="column items-center q-pa-md">
        <div class="card-list">
            <paginate
                data-key="CustomerList"
                url="/Clients/filter"
                order="id DESC"
                auto-load
            >
                <template #body="{ rows }">
                    <q-card class="card q-mb-md" v-for="row of rows" :key="row.id">
                        <q-item
                            class="q-pa-none items-start cursor-pointer q-hoverable"
                            v-ripple
                            clickable
                        >
                            <q-item-section class="q-pa-md" @click="navigate(row.id)">
                                <div class="text-h6">{{ row.name }}</div>
                                <q-item-label caption>#{{ row.id }}</q-item-label>

                                <q-list>
                                    <q-item class="q-pa-none">
                                        <q-item-section>
                                            <q-item-label caption>
                                                {{ t('customer.list.email') }}
                                            </q-item-label>
                                            <q-item-label>{{ row.email }}</q-item-label>
                                        </q-item-section>
                                    </q-item>
                                    <q-item class="q-pa-none">
                                        <q-item-section>
                                            <q-item-label caption>
                                                {{ t('customer.list.phone') }}
                                            </q-item-label>
                                            <q-item-label>{{ row.phone }}</q-item-label>
                                        </q-item-section>
                                    </q-item>
                                </q-list>
                            </q-item-section>
                            <q-separator vertical />
                            <q-card-actions vertical class="justify-between">
                                <!-- <q-btn color="grey-7" round flat icon="more_vert">
                                <q-tooltip>{{ t('customer.list.moreOptions') }}</q-tooltip>
                                <q-menu cover auto-close>
                                    <q-list>
                                        <q-item clickable>
                                            <q-item-section avatar>
                                                <q-icon name="add" />
                                            </q-item-section>
                                            <q-item-section>Add a note</q-item-section>
                                        </q-item>
                                        <q-item clickable>
                                            <q-item-section avatar>
                                                <q-icon name="history" />
                                            </q-item-section>
                                            <q-item-section>Display customer history</q-item-section>
                                        </q-item>
                                    </q-list>
                                </q-menu>
                            </q-btn> -->

                                <q-btn
                                    flat
                                    round
                                    color="primary"
                                    icon="arrow_circle_right"
                                    @click="navigate(row.id)"
                                >
                                    <q-tooltip>
                                        {{ t('components.smartCard.openCard') }}
                                    </q-tooltip>
                                </q-btn>
                                <q-btn
                                    flat
                                    round
                                    color="grey-7"
                                    icon="preview"
                                    @click="viewSummary(row.id)"
                                >
                                    <q-tooltip>
                                        {{ t('components.smartCard.openSummary') }}
                                    </q-tooltip>
                                </q-btn>
                                <!-- <q-btn flat round color="grey-7" icon="vn:ticket">
                                <q-tooltip>{{ t('customer.list.customerOrders') }}</q-tooltip>
                            </q-btn> -->
                            </q-card-actions>
                        </q-item>
                    </q-card>
                </template>
            </paginate>
        </div>
    </q-page>
</template>

<style lang="scss" scoped>
.card-list {
    width: 100%;
    max-width: 60em;
}
</style>

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