<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 WorkerSummary from './Card/WorkerSummary.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';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';

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

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

const redirectToCreateView = () => {
    router.push({ name: 'WorkerCreate' });
};
</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="vn-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"
                        :id="row.id"
                        :title="row.nickname"
                        @click="navigate(row.id)"
                    >
                        <template #list-items>
                            <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
                                :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, WorkerSummary)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QPageSticky :offset="[20, 20]">
            <QBtn fab icon="add" color="primary" @click="redirectToCreateView()" />
            <QTooltip>
                {{ t('worker.list.newWorker') }}
            </QTooltip>
        </QPageSticky>
    </QPage>
</template>

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