Files
CRM_Montzar/apps/web/src/views/TicketsView.vue
T
2026-10-05 11:56:49 +03:30

713 lines
16 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue';
import TableColumnsMenu from '@/components/TableColumnsMenu.vue';
import { RouterLink, useRoute, useRouter } from 'vue-router';
import axios from 'axios';
import { api } from '@/lib/api';
import { toFaDigits } from '@/lib/jalali';
import {
UiButton,
UiPageHeader,
UiEmptyState,
} from '@/components/ui';
import UiIcon from '@/components/UiIcon.vue';
import type {
PaginatedResponse,
TicketDto,
TicketPriority,
TicketStatus,
} from '@mento/shared';
const route = useRoute();
const router = useRouter();
const loading = ref(false);
const error = ref('');
const data = ref<PaginatedResponse<TicketDto> | null>(null);
const q = ref(typeof route.query.q === 'string' ? route.query.q : '');
const status = ref<TicketStatus | 'all'>(
typeof route.query.status === 'string' &&
['open', 'in_progress', 'waiting', 'resolved', 'closed'].includes(route.query.status)
? (route.query.status as TicketStatus)
: 'all',
);
const slaOnly = ref(route.query.sla === 'overdue');
const filterOpen = ref(false);
const statusLabel: Record<TicketStatus, string> = {
open: 'باز',
in_progress: 'در حال رسیدگی',
waiting: 'در انتظار',
resolved: 'حل‌شده',
closed: 'بسته',
};
const priorityLabel: Record<TicketPriority, string> = {
low: 'کم',
normal: 'عادی',
high: 'بالا',
urgent: 'فوری',
};
const items = computed(() => data.value?.items ?? []);
const listEmpty = computed(() => !loading.value && !items.value.length);
const statusFilterLabel = computed(() =>
status.value === 'all' ? 'همه وضعیت‌ها' : statusLabel[status.value],
);
function formatSla(iso: string | null) {
if (!iso) return '';
try {
return new Intl.DateTimeFormat('fa-IR', {
dateStyle: 'short',
timeStyle: 'short',
}).format(new Date(iso));
} catch {
return iso;
}
}
async function load() {
loading.value = true;
error.value = '';
filterOpen.value = false;
try {
const { data: res } = await api.get<PaginatedResponse<TicketDto>>('/tickets', {
params: {
q: q.value || undefined,
status: status.value === 'all' ? undefined : status.value,
sla: slaOnly.value ? 'overdue' : undefined,
limit: 50,
},
});
data.value = res;
} catch (e) {
if (axios.isAxiosError(e) && !e.response) {
error.value = 'اتصال به API برقرار نشد.';
} else {
error.value = 'بارگذاری تیکت‌ها ناموفق بود.';
}
} finally {
loading.value = false;
}
}
function onSearch() {
void router.replace({
name: 'tickets',
query: {
q: q.value.trim() || undefined,
status: status.value === 'all' ? undefined : status.value,
sla: slaOnly.value ? 'overdue' : undefined,
},
});
}
function setStatus(next: TicketStatus | 'all') {
status.value = next;
filterOpen.value = false;
onSearch();
}
function toggleSla() {
slaOnly.value = !slaOnly.value;
onSearch();
}
watch(
() => [route.query.q, route.query.status, route.query.sla],
() => {
q.value = typeof route.query.q === 'string' ? route.query.q : '';
status.value =
typeof route.query.status === 'string' &&
['open', 'in_progress', 'waiting', 'resolved', 'closed'].includes(route.query.status)
? (route.query.status as TicketStatus)
: 'all';
slaOnly.value = route.query.sla === 'overdue';
void load();
},
);
onMounted(load);
</script>
<template>
<section class="page" @click="filterOpen = false">
<UiPageHeader
title="تیکت‌ها"
lead="درخواست‌های پشتیبانی را ثبت و پیگیری کنید."
>
<template #actions>
<RouterLink :to="{ name: 'ticket-new' }">
<UiButton variant="primary">
<UiIcon name="plus" :size="16" />
تیکت جدید
</UiButton>
</RouterLink>
</template>
</UiPageHeader>
<div class="tools">
<form class="search" @submit.prevent="onSearch">
<UiIcon name="search" :size="18" />
<input v-model="q" type="search" placeholder="جستجو در موضوع یا شماره…" />
</form>
<button
class="btn ghost sla-btn"
type="button"
:class="{ on: slaOnly }"
@click="toggleSla"
>
فقط SLA
</button>
<div class="filter-wrap" @click.stop>
<button class="btn ghost filter-btn" type="button" @click="filterOpen = !filterOpen">
<UiIcon name="filter" :size="16" />
<span class="btn-full">{{ statusFilterLabel }}</span>
<span class="btn-short">وضعیت</span>
<UiIcon name="chevron" :size="14" class="chev" />
</button>
<div v-if="filterOpen" class="popover">
<button type="button" :class="{ on: status === 'all' }" @click="setStatus('all')">
همه وضعیت‌ها
</button>
<button
v-for="(label, key) in statusLabel"
:key="key"
type="button"
:class="{ on: status === key }"
@click="setStatus(key as TicketStatus)"
>
{{ label }}
</button>
</div>
</div>
</div>
<p v-if="error" class="error">{{ error }}</p>
<p v-else-if="loading" class="muted">در حال بارگذاری…</p>
<UiEmptyState
v-else-if="listEmpty"
title="هنوز تیکتی ثبت نشده است"
description="اولین درخواست پشتیبانی را ثبت کنید تا پیگیری‌ها اینجا جمع شوند."
>
<template #action>
<RouterLink :to="{ name: 'ticket-new' }">
<UiButton variant="primary">
<UiIcon name="plus" :size="16" />
تیکت جدید
</UiButton>
</RouterLink>
</template>
</UiEmptyState>
<template v-else>
<div class="list-head">
<strong>{{ toFaDigits(items.length) }} تیکت</strong>
</div>
<div class="desktop-only">
<div class="tc-row">
<TableColumnsMenu table-key="tickets" align="end" />
</div>
</div>
<div class="table-wrap desktop-only">
<table v-table-cols="'tickets'" class="data-table">
<thead>
<tr>
<th>موضوع</th>
<th>شماره</th>
<th>وضعیت</th>
<th>اولویت</th>
<th>مشتری / مسئول</th>
<th>SLA</th>
</tr>
</thead>
<tbody>
<tr v-for="ticket in items" :key="ticket.id" :class="{ breached: ticket.slaBreached }">
<td>
<RouterLink class="title" :to="{ name: 'ticket-detail', params: { id: ticket.id } }">
{{ ticket.subject }}
</RouterLink>
</td>
<td class="muted">{{ ticket.number }}</td>
<td>
<span class="badge" :class="ticket.status">
{{ statusLabel[ticket.status as TicketStatus] || ticket.status }}
</span>
</td>
<td>
<span class="prio" :class="ticket.priority">
{{ priorityLabel[ticket.priority as TicketPriority] || ticket.priority }}
</span>
</td>
<td class="muted">
<span v-if="ticket.person">{{ ticket.person.fullName }}</span>
<span v-if="ticket.person && ticket.assignee"> · </span>
<span v-if="ticket.assignee">{{ ticket.assignee.fullName }}</span>
<span v-if="!ticket.person && !ticket.assignee">—</span>
</td>
<td>
<span v-if="ticket.slaBreached" class="sla bad">تأخیر SLA</span>
<span v-else-if="ticket.firstRespondedAt" class="sla ok">پاسخ داده شد</span>
<span v-else-if="ticket.slaDueAt" class="sla">مهلت {{ formatSla(ticket.slaDueAt) }}</span>
<span v-else class="muted">—</span>
</td>
</tr>
</tbody>
</table>
</div>
<div class="cards mobile-only">
<RouterLink
v-for="ticket in items"
:key="ticket.id"
class="m-card"
:class="{ breached: ticket.slaBreached }"
:to="{ name: 'ticket-detail', params: { id: ticket.id } }"
>
<div class="m-top">
<strong>{{ ticket.subject }}</strong>
<span class="badge" :class="ticket.status">
{{ statusLabel[ticket.status as TicketStatus] || ticket.status }}
</span>
</div>
<p class="muted">
{{ ticket.number }} ·
{{ priorityLabel[ticket.priority as TicketPriority] || ticket.priority }}
</p>
<p v-if="ticket.person || ticket.assignee" class="meta">
<span v-if="ticket.person">{{ ticket.person.fullName }}</span>
<span v-if="ticket.assignee"> · {{ ticket.assignee.fullName }}</span>
</p>
<div class="m-foot">
<span v-if="ticket.slaBreached" class="sla bad">تأخیر SLA</span>
<span v-else-if="ticket.firstRespondedAt" class="sla ok">پاسخ داده شد</span>
<span v-else-if="ticket.slaDueAt" class="sla">مهلت {{ formatSla(ticket.slaDueAt) }}</span>
</div>
</RouterLink>
</div>
</template>
</section>
</template>
<style scoped>
.page {
display: grid;
gap: 1rem;
min-width: 0;
overflow-x: clip;
}
.page-head {
display: flex;
justify-content: space-between;
gap: 1rem;
align-items: flex-start;
}
.head-copy {
min-width: 0;
}
h1 {
margin: 0 0 0.3rem;
font-size: clamp(1.45rem, 2.5vw, 1.85rem);
font-weight: 600;
}
.lead,
.muted,
.error,
.meta {
margin: 0;
}
.lead,
.muted,
.meta {
color: #64748b;
}
.error {
color: #b91c1c;
}
.add-btn {
width: var(--control-h);
height: var(--control-h);
border-radius: var(--radius);
background: #14b8a6;
color: #fff;
display: grid;
place-items: center;
text-decoration: none;
font-size: 1.6rem;
font-weight: 700;
box-shadow: 0 8px 18px rgba(20, 184, 166, 0.28);
flex-shrink: 0;
}
.tools {
display: flex;
gap: 0.55rem;
align-items: center;
}
.search {
flex: 1 1 200px;
min-width: 180px;
max-width: 400px;
height: var(--control-h);
min-height: var(--control-h);
display: flex;
align-items: center;
gap: 0.5rem;
background: #fff;
border: 1px solid var(--line);
border-radius: var(--radius);
padding: 0 0.75rem;
color: var(--muted);
box-sizing: border-box;
transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.search:focus-within {
border-color: var(--accent);
box-shadow: 0 0 0 2px var(--accent-soft);
}
.search input {
flex: 1;
min-width: 0;
border: 0;
outline: none;
background: none;
padding: 0;
font: inherit;
font-size: var(--fs-sm);
color: var(--ink);
}
.filter-wrap {
position: relative;
flex-shrink: 0;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.35rem;
height: var(--control-h);
min-height: var(--control-h);
padding: 0 0.85rem;
border-radius: var(--radius);
font-weight: 600;
font-size: var(--fs-sm);
border: 1px solid transparent;
cursor: pointer;
font: inherit;
text-decoration: none;
box-sizing: border-box;
transition: background var(--transition-fast), border-color var(--transition-fast);
}
.btn.primary {
background: var(--accent);
color: #fff;
}
.btn.ghost {
background: #fff;
color: var(--ink-secondary);
border-color: var(--line);
}
.btn.ghost:hover {
background: var(--surface-subtle);
border-color: var(--line-strong);
}
.btn.ghost.on {
background: var(--accent-subtle);
border-color: var(--accent);
color: var(--accent-deep);
}
.btn.wide {
width: min(100%, 320px);
}
.sla-btn {
flex-shrink: 0;
white-space: nowrap;
}
.filter-btn .chev {
transform: rotate(90deg);
}
.btn-short {
display: none;
}
.popover {
position: absolute;
top: calc(100% + 6px);
inset-inline-end: 0;
min-width: 11rem;
background: #fff;
border-radius: var(--radius);
box-shadow: 0 16px 40px rgba(15, 23, 42, 0.14);
padding: 0.35rem;
display: grid;
z-index: 8;
}
.popover button {
display: block;
width: 100%;
text-align: start;
border: 0;
background: none;
padding: 0.6rem 0.7rem;
border-radius: var(--radius);
font: inherit;
cursor: pointer;
color: #0f172a;
}
.popover button:hover,
.popover button.on {
background: #f0fdfa;
color: #0f766e;
}
.empty-card {
background: #fff;
border-radius: var(--radius);
box-shadow: 0 10px 28px rgba(15, 23, 42, 0.05);
padding: 2.4rem 1.2rem 2rem;
display: grid;
justify-items: center;
text-align: center;
gap: 0.7rem;
}
.art svg {
width: 148px;
height: auto;
display: block;
}
.empty-card h2 {
margin: 0.2rem 0 0;
font-size: 1.15rem;
}
.empty-card > p {
max-width: 22rem;
color: #94a3b8;
line-height: 1.8;
}
.list-head {
color: #0f766e;
}
.table-wrap {
background: #fff;
border-radius: var(--radius);
box-shadow: 0 8px 24px rgba(15, 23, 42, 0.05);
overflow: auto;
}
.data-table {
width: 100%;
border-collapse: collapse;
min-width: 720px;
}
.data-table th,
.data-table td {
padding: 0.9rem 1rem;
text-align: start;
border-bottom: 1px solid #f1f5f9;
}
.data-table th {
font-size: 0.78rem;
color: #94a3b8;
font-weight: 700;
background: #f8fafc;
}
.data-table tr.breached td {
background: color-mix(in srgb, #fef2f2 55%, #fff);
}
.title {
color: #0f172a;
font-weight: 700;
text-decoration: none;
}
.title:hover {
color: #0f766e;
}
.badge,
.prio {
display: inline-flex;
align-items: center;
padding: 0.25rem 0.55rem;
border-radius: 999px;
font-size: 0.75rem;
font-weight: 700;
background: #f1f5f9;
color: #475569;
white-space: nowrap;
}
.badge.open,
.prio.normal {
background: #ecfeff;
color: #0e7490;
}
.badge.in_progress {
background: #fff7ed;
color: #c2410c;
}
.badge.waiting {
background: #fefce8;
color: #a16207;
}
.badge.resolved,
.badge.closed {
background: #ecfdf5;
color: #047857;
}
.prio.low {
background: #f1f5f9;
color: #64748b;
}
.prio.high,
.prio.urgent {
background: #fef2f2;
color: #b91c1c;
}
.sla {
font-size: 0.8rem;
font-weight: 700;
color: #0f766e;
}
.sla.bad {
color: #b91c1c;
}
.sla.ok {
color: #047857;
}
.desktop-only {
display: block;
}
.mobile-only {
display: none;
}
.cards {
display: grid;
gap: 0.65rem;
}
.m-card {
background: #fff;
border-radius: var(--radius);
padding: 0.95rem 1rem;
box-shadow: 0 6px 18px rgba(15, 23, 42, 0.05);
text-decoration: none;
color: inherit;
display: grid;
gap: 0.35rem;
min-width: 0;
}
.m-card.breached {
box-shadow: 0 0 0 1px color-mix(in srgb, #b91c1c 25%, transparent),
0 6px 18px rgba(15, 23, 42, 0.05);
}
.m-top {
display: flex;
justify-content: space-between;
gap: 0.6rem;
align-items: flex-start;
}
.m-top strong {
min-width: 0;
overflow-wrap: anywhere;
}
.m-foot {
margin-top: 0.25rem;
}
@media (max-width: 900px) {
.page-head {
align-items: center;
}
.tools {
flex-wrap: wrap;
}
.search {
flex: 1 1 100%;
}
.btn-full {
display: none;
}
.btn-short {
display: inline;
}
.desktop-only {
display: none;
}
.mobile-only {
display: grid;
}
}
@media (max-width: 520px) {
.add-btn {
width: var(--control-h);
height: var(--control-h);
}
.sla-btn {
font-size: 0.85rem;
padding-inline: 0.7rem;
}
.m-card {
padding: 0.85rem;
}
}
</style>