<script setup>
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useQuasar } from 'quasar';
import { useStateStore } from 'stores/useStateStore';
import { toDate } from 'filters/index';
import VnPaginate from 'src/components/ui/VnPaginate.vue';
import ClaimSummaryDialog from './Card/ClaimSummaryDialog.vue';
import CustomerDescriptorProxy from 'pages/Customer/Card/CustomerDescriptorProxy.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import ClaimFilter from './ClaimFilter.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import CardList from 'src/components/ui/CardList.vue';

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

function stateColor(code) {
    if (code === 'pending') return 'green';
    if (code === 'managed') return 'orange';
    if (code === 'resolved') return 'red';
}

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

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

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="ClaimList"
                :label="t('Search claim')"
                :info="t('You can search by claim id or customer 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">
            <ClaimFilter data-key="ClaimList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="card-list">
            <VnPaginate
                data-key="ClaimList"
                url="Claims/filter"
                order="id DESC"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :title="row.clientName"
                        @click="navigate(row.id)"
                    >
                        <template #list-items>
                            <VnLv label="ID" :value="row.id" />
                            <VnLv
                                :label="t('claim.list.customer')"
                                :value="row.clientName"
                            />
                            <VnLv
                                :label="t('claim.list.assignedTo')"
                                :value="row.workerName"
                            />
                            <VnLv
                                :label="t('claim.list.created')"
                                :value="toDate(row.created)"
                            />
                            <VnLv :label="t('claim.list.state')">
                                <template #value>
                                    <QBadge
                                        :color="stateColor(row.stateCode)"
                                        class="q-ma-none"
                                        dense
                                    >
                                        {{ row.stateDescription }}
                                    </QBadge>
                                </template>
                            </VnLv>
                        </template>
                        <template #actions>
                            <QBtn
                                flat
                                icon="arrow_circle_right"
                                @click="navigate(row.id)"
                            >
                                <QTooltip>
                                    {{ t('components.smartCard.openCard') }}
                                </QTooltip>
                            </QBtn>
                            <QBtn flat icon="preview" @click="viewSummary(row.id)">
                                <QTooltip>
                                    {{ t('components.smartCard.openSummary') }}
                                </QTooltip>
                            </QBtn>
                            <QBtn flat icon="vn:client">
                                <QTooltip>
                                    {{ t('components.smartCard.viewDescription') }}
                                </QTooltip>

                                <CustomerDescriptorProxy :id="row.clientFk" />
                            </QBtn>
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
    </QPage>
</template>

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

<i18n>
es:
    Search claim: Buscar reclamación
    You can search by claim id or customer name: Puedes buscar por id de la reclamación o nombre del cliente
</i18n>
