Files
CRM_Montzar/apps/web/src/views/ReportsView.vue
T

440 lines
9.6 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import axios from 'axios';
import { api } from '@/lib/api';
import { formatMoney } from '@/lib/money';
import { formatLocalDateInput } from '@/lib/jalali';
import JalaliDateInput from '@/components/JalaliDateInput.vue';
import type { OrganizationDto, ReportsDashboardDto } from '@mento/shared';
const loading = ref(false);
const error = ref('');
const data = ref<ReportsDashboardDto | null>(null);
const org = ref<OrganizationDto | null>(null);
const from = ref('');
const to = ref('');
const typeLabel: Record<string, string> = {
call: 'تماس',
meeting: 'جلسه',
task: 'وظیفه',
note: 'یادداشت',
};
function defaultRange() {
const now = new Date();
const start = new Date(now.getFullYear(), now.getMonth(), 1);
from.value = formatLocalDateInput(start);
to.value = formatLocalDateInput(now);
}
function money(n: number) {
return formatMoney(n, data.value?.currency || org.value?.baseCurrency || 'IRR');
}
async function load() {
loading.value = true;
error.value = '';
try {
const [dash, orgRes] = await Promise.all([
api.get<ReportsDashboardDto>('/reports/dashboard', {
params: {
from: from.value || undefined,
to: to.value || undefined,
},
}),
api.get<OrganizationDto>('/organizations/current'),
]);
data.value = dash.data;
org.value = orgRes.data;
} catch (e) {
if (axios.isAxiosError(e) && !e.response) {
error.value = 'اتصال به API برقرار نشد.';
} else {
error.value = 'بارگذاری گزارش ناموفق بود.';
}
} finally {
loading.value = false;
}
}
onMounted(() => {
defaultRange();
void load();
});
</script>
<template>
<section class="page">
<header class="page-head">
<div class="head-copy">
<h1>گزارشات و داشبورد</h1>
<p class="lead">نمای یک‌صفحه‌ای فروش، فعالیت، تیم و پشتیبانی.</p>
</div>
</header>
<form class="toolbar" @submit.prevent="load">
<label class="field">
<span>از</span>
<JalaliDateInput v-model="from" />
</label>
<label class="field">
<span>تا</span>
<JalaliDateInput v-model="to" />
</label>
<button class="btn primary" type="submit" :disabled="loading">اعمال</button>
<a
v-if="org"
class="portal-link"
:href="`/p/${org.slug}`"
target="_blank"
rel="noopener"
>
پرتال مشتری (/p/{{ org.slug }})
</a>
</form>
<p v-if="error" class="error">{{ error }}</p>
<p v-else-if="loading" class="muted">در حال بارگذاری…</p>
<template v-else-if="data">
<div class="kpis">
<article>
<span>برد دوره</span>
<strong>{{ money(data.sales.wonAmount) }}</strong>
<small>{{ data.sales.wonCount }} معامله</small>
</article>
<article>
<span>کاریز باز</span>
<strong>{{ money(data.sales.openAmount) }}</strong>
<small>{{ data.sales.openCount }} معامله</small>
</article>
<article>
<span>نرخ برد</span>
<strong>{{ data.sales.winRate }}٪</strong>
<small>{{ data.sales.lostCount }} باخت</small>
</article>
<article class="accent">
<span>اشخاص جدید</span>
<strong>{{ data.peopleCreated }}</strong>
<small>در بازه</small>
</article>
</div>
<div class="grid">
<section class="panel">
<h2>قیف فروش (باز)</h2>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>مرحله</th>
<th>تعداد</th>
<th>مبلغ</th>
</tr>
</thead>
<tbody>
<tr v-for="s in data.funnel" :key="s.stageId">
<td>
{{ s.stageName }}
<span class="muted"> · {{ s.pipelineName }}</span>
</td>
<td>{{ s.count }}</td>
<td>{{ money(s.amount) }}</td>
</tr>
<tr v-if="!data.funnel.length">
<td colspan="3" class="muted">داده‌ای نیست</td>
</tr>
</tbody>
</table>
</div>
</section>
<section class="panel">
<h2>فعالیت‌ها</h2>
<ul class="stats">
<li>باز: {{ data.activities.open }}</li>
<li>انجام‌شده: {{ data.activities.done }}</li>
<li>عقب‌افتاده: {{ data.activities.overdue }}</li>
</ul>
<ul class="stats">
<li v-for="t in data.activities.byType" :key="t.type">
{{ typeLabel[t.type] || t.type }}: {{ t.count }}
</li>
</ul>
</section>
<section class="panel">
<h2>تیم</h2>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>کاربر</th>
<th>برد</th>
<th>فعالیت انجام‌شده</th>
</tr>
</thead>
<tbody>
<tr v-for="u in data.team" :key="u.userId">
<td>{{ u.fullName }}</td>
<td>{{ u.wonCount }} · {{ money(u.wonAmount) }}</td>
<td>{{ u.activitiesDone }}</td>
</tr>
<tr v-if="!data.team.length">
<td colspan="3" class="muted">داده‌ای نیست</td>
</tr>
</tbody>
</table>
</div>
</section>
<section class="panel">
<h2>پشتیبانی</h2>
<ul class="stats">
<li>تیکت باز: {{ data.tickets.open }}</li>
<li>حل‌شده: {{ data.tickets.resolved }}</li>
<li>بسته: {{ data.tickets.closed }}</li>
<li>تأخیر SLA: {{ data.tickets.slaBreached }}</li>
<li>
میانگین CSAT:
{{
data.tickets.avgCsat == null
? '—'
: `${data.tickets.avgCsat} (${data.tickets.csatCount})`
}}
</li>
</ul>
</section>
</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: 800;
}
h2 {
margin: 0 0 0.85rem;
font-size: 1.05rem;
color: #0f172a;
}
.lead,
.muted {
margin: 0;
color: #64748b;
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.65rem;
align-items: end;
background: #fff;
border-radius: 18px;
padding: 0.95rem 1rem;
box-shadow: 0 8px 24px rgba(15, 23, 42, 0.04);
}
.field {
display: grid;
gap: 0.3rem;
font-size: 0.85rem;
color: #64748b;
min-width: 0;
}
.field span {
font-weight: 700;
}
.toolbar :deep(.trigger) {
min-width: 11.5rem;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 42px;
padding: 0.55rem 1rem;
border-radius: 12px;
font-weight: 700;
border: 1px solid transparent;
cursor: pointer;
font: inherit;
}
.btn.primary {
background: #14b8a6;
color: #fff;
box-shadow: 0 8px 18px rgba(20, 184, 166, 0.24);
}
.btn.primary:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.portal-link {
margin-inline-start: auto;
color: #0f766e;
font-size: 0.9rem;
font-weight: 700;
text-decoration: none;
align-self: center;
}
.portal-link:hover {
color: #14b8a6;
}
.kpis {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 0.75rem;
}
.kpis article {
background: #fff;
border-radius: 18px;
padding: 1rem 1.05rem;
display: grid;
gap: 0.25rem;
box-shadow: 0 8px 24px rgba(15, 23, 42, 0.04);
min-width: 0;
}
.kpis article.accent {
background: linear-gradient(135deg, #f0fdfa 0%, #fff 100%);
border: 1px solid #99f6e4;
}
.kpis span,
.kpis small {
color: #64748b;
font-size: 0.85rem;
}
.kpis strong {
font-size: 1.15rem;
color: #0f172a;
}
.kpis .accent strong {
color: #0f766e;
}
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 0.85rem;
}
.panel {
background: #fff;
border-radius: 18px;
padding: 1rem 1.05rem;
box-shadow: 0 8px 24px rgba(15, 23, 42, 0.04);
min-width: 0;
}
.table-wrap {
overflow-x: auto;
min-width: 0;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 0.92rem;
min-width: 0;
}
th,
td {
text-align: start;
padding: 0.55rem 0.35rem;
border-bottom: 1px solid #f1f5f9;
}
th {
font-size: 0.78rem;
color: #94a3b8;
font-weight: 700;
}
.stats {
margin: 0;
padding: 0;
list-style: none;
display: grid;
gap: 0.4rem;
color: #334155;
}
.error {
margin: 0;
color: #b91c1c;
}
@media (max-width: 900px) {
.kpis {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.grid {
grid-template-columns: 1fr;
}
.portal-link {
margin-inline-start: 0;
width: 100%;
}
}
@media (max-width: 700px) {
.kpis {
grid-template-columns: 1fr;
}
.toolbar {
flex-direction: column;
align-items: stretch;
}
.field :deep(.trigger) {
width: 100%;
min-width: 0;
}
.btn.primary {
width: 100%;
}
}
</style>