<script setup>
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useQuasar } from 'quasar';
import { useStateStore } from 'stores/useStateStore';
import VnPaginate from 'src/components/ui/VnPaginate.vue';
import WorkerSummaryDialog from './Card/WorkerSummaryDialog.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import WorkerFilter from './WorkerFilter.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import CardList from 'src/components/ui/CardList.vue';

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

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

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

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="WorkerList"
                :label="t('Search worker')"
                :info="t('You can search by worker id or name')"
            />
        </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">
            <WorkerFilter data-key="WorkerList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="card-list">
            <VnPaginate
                data-key="WorkerList"
                url="Workers/filter"
                order="id DESC"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        @click="navigate(row.id)"
                        :title="row.nickname"
                    >
                        <template #list-items>
                            <VnLv label="ID" :value="row.id" />
                            <VnLv :label="t('worker.list.name')" :value="row.userName" />
                            <VnLv :label="t('worker.list.email')" :value="row.email" />
                            <VnLv
                                :label="t('worker.list.department')"
                                :value="row.department"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                flat
                                icon="arrow_circle_right"
                                @click.stop="navigate(row.id)"
                            >
                                <QTooltip>
                                    {{ t('components.smartCard.openCard') }}
                                </QTooltip>
                            </QBtn>
                            <QBtn flat icon="preview" @click.stop="viewSummary(row.id)">
                                <QTooltip>
                                    {{ t('components.smartCard.openSummary') }}
                                </QTooltip>
                            </QBtn>
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
    </QPage>
</template>

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

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