<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
// import { useI18n } from 'vue-i18n';
import axios from 'axios';
import { useState } from 'src/composables/useState';
import { toDate } from 'src/filters/index';

const route = useRoute();
// const { t } = useI18n();
const state = useState();

onMounted(async () => {
    await fetch();
});
const claim = ref({});

async function fetch() {
    const entityId = route.params.id;
    const filter = {
        include: [
            { relation: 'client' },
            { relation: 'claimState' },
            {
                relation: 'claimState',
            },
            {
                relation: 'worker',
                scope: {
                    include: { relation: 'user' },
                },
            },
        ],
    };
    const options = { params: { filter } };
    const { data } = await axios.get(`Claims/${entityId}`, options);

    if (data) claim.value = data;
}

function stateColor(code) {
    if (code === 'pending') return 'green';
    if (code === 'managed') return 'orange';
    if (code === 'resolved') return 'red';
}
</script>
<template>
    <q-drawer v-model="state.drawer.value" show-if-above :width="256" :breakpoint="500">
        <q-scroll-area class="fit text-grey-8 descriptor">
            <div class="header bg-orange q-pa-sm">
                <router-link :to="{ path: '/claim/list' }">
                    <q-btn round flat dense size="md" icon="view_list" color="white">
                        <q-tooltip>Claim list</q-tooltip>
                    </q-btn>
                </router-link>
                <router-link :to="{ path: '/claim/list' }">
                    <q-btn round flat dense size="md" icon="launch" color="white">
                        <q-tooltip>Claim preview</q-tooltip>
                    </q-btn>
                </router-link>

                <q-btn round flat dense size="md" icon="more_vert" color="white">
                    <q-tooltip>More options</q-tooltip>
                    <q-menu>
                        <q-list>
                            <q-item clickable v-ripple>Option 1</q-item>
                            <q-item clickable v-ripple>Option 2</q-item>
                        </q-list>
                    </q-menu>
                </q-btn>
            </div>

            <div v-if="claim.client" class="q-py-sm">
                <h5>{{ claim.client.name }}</h5>
                <q-list>
                    <q-item>
                        <q-item-section>
                            <q-item-label caption>Claim ID</q-item-label>
                            <q-item-label>#{{ claim.id }}</q-item-label>
                        </q-item-section>
                        <q-item-section>
                            <q-item-label caption>Created</q-item-label>
                            <q-item-label>{{ toDate(claim.created) }}</q-item-label>
                        </q-item-section>
                    </q-item>
                    <q-item>
                        <q-item-section>
                            <q-item-label caption>Assigned</q-item-label>
                            <q-item-label>{{ claim.worker.user.name }}</q-item-label>
                        </q-item-section>
                        <q-item-section>
                            <q-item-label caption>State</q-item-label>
                            <q-item-label>
                                <q-chip :color="stateColor(claim.claimState.code)" dense>
                                    {{ claim.claimState.description }}
                                </q-chip>
                            </q-item-label>
                        </q-item-section>
                    </q-item>
                    <q-item>
                        <q-item-section>
                            <q-item-label caption>Ticket ID</q-item-label>
                            <q-item-label>{{ claim.ticketFk }}</q-item-label>
                        </q-item-section>
                    </q-item>
                </q-list>
                <q-card-actions>
                    <q-btn
                        size="md"
                        icon="vn:client"
                        color="primary"
                        :to="{ name: 'CustomerCard', params: { id: claim.clientFk } }"
                    >
                        <q-tooltip>Client preview</q-tooltip>
                    </q-btn>
                    <q-btn
                        size="md"
                        icon="vn:ticket"
                        color="primary"
                        :to="{ name: 'TicketCard', params: { id: claim.ticketFk } }"
                    >
                        <q-tooltip>Claimed ticket</q-tooltip>
                    </q-btn>
                </q-card-actions>
            </div>
            <!-- Skeleton -->
            <div id="descriptor-skeleton" v-if="!claim.client">
                <div class="col q-pl-sm q-pa-sm">
                    <q-skeleton type="text" square height="45px" />
                    <q-skeleton type="text" square height="18px" />
                    <q-skeleton type="text" square height="18px" />
                    <q-skeleton type="text" square height="18px" />
                </div>

                <q-card-actions>
                    <q-skeleton size="40px" />
                    <q-skeleton size="40px" />
                    <q-skeleton size="40px" />
                    <q-skeleton size="40px" />
                    <q-skeleton size="40px" />
                </q-card-actions>
            </div>

            <q-separator />

            <q-list>
                <q-item :to="{ name: 'ClaimBasicData' }" clickable v-ripple active-class="text-orange">
                    <q-item-section avatar>
                        <q-icon name="person" />
                    </q-item-section>
                    <q-item-section>Basic data</q-item-section>
                </q-item>
            </q-list>
        </q-scroll-area>
    </q-drawer>
    <q-page-container>
        <router-view></router-view>
    </q-page-container>
</template>

<style lang="scss" scoped>
.descriptor {
    h5 {
        margin: 0 15px;
    }

    .header {
        display: flex;
        justify-content: space-between;
    }

    .q-card__actions {
        justify-content: center;
    }

    #descriptor-skeleton .q-card__actions {
        justify-content: space-around;
    }
}
</style>
