<script setup>
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
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 CustomerDescriptorProxy from 'src/pages/Customer/Card/CustomerDescriptorProxy.vue';
import VnUserLink from 'src/components/ui/VnUserLink.vue';
import ClaimSummary from './Card/ClaimSummary.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import { getUrl } from 'src/composables/getUrl';

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

const STATE_COLOR = {
    pending: 'warning',
    managed: 'info',
    resolved: 'positive',
};
function getApiUrl() {
    return new URL(window.location).origin;
}
function stateColor(code) {
    return STATE_COLOR[code];
}
function navigate(event, id) {
    if (event.ctrlKey || event.metaKey)
        return window.open(`${getApiUrl()}/#/claim/${id}/summary`);
    router.push({ path: `/claim/${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="vn-card-list">
            <VnPaginate
                data-key="ClaimList"
                url="Claims/filter"
                :order="['priority ASC', 'created DESC']"
                auto-load
            >
                <template #body="{ rows }">
                    <CardList
                        :id="row.id"
                        :key="row.id"
                        :title="row.clientName"
                        @click="navigate($event, row.id)"
                        v-for="row of rows"
                    >
                        <template #list-items>
                            <VnLv :label="t('claim.list.customer')">
                                <template #value>
                                    <span class="link" @click.stop>
                                        {{ row.clientName }}
                                        <CustomerDescriptorProxy :id="row.clientFk" />
                                    </span>
                                </template>
                            </VnLv>
                            <VnLv :label="t('claim.list.assignedTo')">
                                <template #value>
                                    <span @click.stop>
                                        <VnUserLink
                                            :name="row.workerName"
                                            :worker-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('globals.description')"
                                @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, ClaimSummary)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
    </QPage>
</template>

<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>
