<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 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';
import ClaimSummaryDialog from './Card/ClaimSummaryDialog.vue';
import CustomerDescriptorProxy from 'src/pages/Customer/Card/CustomerDescriptorProxy.vue';
import WorkerDescriptorProxy from '../Worker/Card/WorkerDescriptorProxy.vue';

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

const STATE_COLOR = {
    pending: 'positive',
    managed: 'warning',
    resolved: 'negative',
};
function stateColor(code) {
    return STATE_COLOR[code];
}
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="claimStateFk"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        :id="row.id"
                        :key="row.id"
                        :title="row.clientName"
                        @click="navigate(row.id)"
                        v-for="row of rows"
                    >
                        <template #list-items>
                            <VnLv label="ID" :value="row.id" />
                            <VnLv :label="t('claim.list.customer')" @click.stop>
                                <template #value>
                                    <span class="link">
                                        {{ row.clientName }}
                                        <CustomerDescriptorProxy :id="row.clientFk" />
                                    </span>
                                </template>
                            </VnLv>
                            <VnLv :label="t('claim.list.assignedTo')" @click.stop>
                                <template #value>
                                    <span class="link">
                                        {{ row.workerName }}
                                        <WorkerDescriptorProxy :id="row.workerFk" />
                                    </span>
                                </template>
                            </VnLv>
                            <VnLv
                                :label="t('claim.list.created')"
                                :value="toDate(row.created)"
                            />
                            <VnLv :label="t('claim.list.state')">
                                <template #value>
                                    <QBadge :color="stateColor(row.stateCode)" dense>
                                        {{ row.stateDescription }}
                                    </QBadge>
                                </template>
                            </VnLv>
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openCard')"
                                @click.stop="navigate(row.id)"
                                class="bg-vn-dark"
                                outline
                            />
                            <QBtn
                                :label="t('components.smartCard.viewDescription')"
                                @click.stop
                                class="bg-vn-dark"
                                outline
                                style="margin-top: 15px"
                            >
                                <CustomerDescriptorProxy :id="row.clientFk" />
                            </QBtn>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </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>
