<script setup>
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useQuasar } from 'quasar';
import Paginate from 'src/components/PaginateData.vue';
import WorkerSummaryDialog from './Card/WorkerSummaryDialog.vue';

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

const filter = {
    include: [
        {
            relation: 'user',
            scope: {
                fields: ['email', 'name', 'nickname'],
            },
        },
        {
            relation: 'department',
            scope: {
                include: {
                    relation: 'department',
                    scope: {
                        fields: ['name'],
                    },
                },
            },
        },
    ],
};

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

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

<template>
    <q-page class="q-pa-md">
        <paginate url="/Workers" :filter="filter" sort-by="id DESC" auto-load>
            <template #body="{ rows }">
                <q-card class="card" v-for="row in 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)">
                            <q-item-label class="text-h6">{{ row.user.nickname }}</q-item-label>
                            <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('worker.list.name') }}</q-item-label>
                                        <q-item-label>{{ row.user.name }}</q-item-label>
                                    </q-item-section>
                                </q-item>
                                <q-item class="q-pa-none">
                                    <q-item-section>
                                        <q-item-label caption>{{ t('worker.list.email') }}</q-item-label>
                                        <q-item-label>{{ row.user.email }}</q-item-label>
                                    </q-item-section>
                                </q-item>
                                <q-item class="q-pa-none">
                                    <q-item-section>
                                        <q-item-label caption>{{ t('worker.list.department') }}</q-item-label>
                                        <q-item-label>{{ row.department.department.name }}</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 flat round color="orange" icon="preview" @click="viewSummary(row.id)">
                                <q-tooltip>{{ t('components.smartCard.openSummary') }}</q-tooltip>
                            </q-btn>
                            <q-btn flat round color="grey-7" icon="schedule" @click="navigate(row.id)">
                                <q-tooltip>{{ t('worker.list.schedule') }}</q-tooltip>
                            </q-btn>
                        </q-card-actions>
                    </q-item>
                </q-card>
            </template>
        </paginate>
    </q-page>
</template>
