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

190 lines
6.2 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
import { RouterLink, useRouter } from 'vue-router';
import axios from 'axios';
import { api } from '@/lib/api';
import type { LeadFormDto } from '@mento/shared';
import { toFaDigits } from '@/lib/jalali';
import {
Alert as AAlert,
Button as AButton,
Card as ACard,
Empty as AEmpty,
Flex as AFlex,
List as AList,
ListItem as AListItem,
ListItemMeta as AListItemMeta,
Space as ASpace,
Spin as ASpin,
Tag as ATag,
Typography,
} from 'ant-design-vue';
import {
CopyOutlined,
DeleteOutlined,
EditOutlined,
EyeOutlined,
PlusOutlined,
} from '@ant-design/icons-vue';
const { Title: ATypographyTitle, Text: ATypographyText } = Typography;
const router = useRouter();
const loading = ref(false);
const saving = ref(false);
const error = ref('');
const forms = ref<LeadFormDto[]>([]);
async function load() {
loading.value = true;
error.value = '';
try {
const { data } = await api.get<LeadFormDto[]>('/lead-forms');
forms.value = data;
} catch (e) {
error.value = axios.isAxiosError(e) && !e.response
? 'اتصال به API برقرار نشد.'
: 'بارگذاری فرم‌ها ناموفق بود.';
} finally {
loading.value = false;
}
}
function nextFormName() {
const taken = new Set(forms.value.map((f) => f.name.trim()));
let n = forms.value.length + 1;
while (taken.has(`فرم جدید ${toFaDigits(n)}`)) n += 1;
return `فرم جدید ${toFaDigits(n)}`;
}
async function createForm() {
saving.value = true;
error.value = '';
try {
const { data } = await api.post<LeadFormDto>('/lead-forms', {
name: nextFormName(),
source: 'وب‌سایت',
createLead: true,
createPerson: false,
createDeal: false,
duplicateMode: 'update',
});
await router.push({ name: 'lead-form-builder', params: { id: data.id }, query: { new: '1' } });
} catch {
error.value = 'ساخت فرم ناموفق بود.';
} finally {
saving.value = false;
}
}
async function removeForm(form: LeadFormDto) {
if (!confirm(`فرم «${form.name}» حذف شود؟`)) return;
await api.delete(`/lead-forms/${form.id}`);
await load();
}
function publicUrl(form: LeadFormDto) {
const base = String(import.meta.env.VITE_LEAD_FORM_PUBLIC_BASE || '').replace(/\/$/, '')
|| window.location.origin;
return `${base}${form.publicPath}`;
}
async function copyPublishLink(form: LeadFormDto) {
await navigator.clipboard.writeText(publicUrl(form));
}
function formMeta(form: LeadFormDto) {
const parts = [
`${toFaDigits(form.fields.length)} فیلد سفارشی`,
`${toFaDigits(form.submitCount ?? form.submissionCount ?? 0)} ارسال`,
`${toFaDigits(form.leadCreateCount ?? 0)} لید`,
];
if (form.source) parts.push(`منبع: ${form.source}`);
return parts.join(' · ');
}
const countLabel = computed(() => toFaDigits(forms.value.length));
const listEmpty = computed(() => !loading.value && !forms.value.length);
onMounted(load);
</script>
<template>
<a-flex vertical :gap="16">
<a-flex justify="space-between" align="flex-start" wrap="wrap" :gap="12">
<a-typography-title :level="3" :style="{ margin: 0 }">فرم‌ساز وب</a-typography-title>
<a-button type="primary" :loading="saving" @click="createForm">
<template #icon><PlusOutlined /></template>
فرم جدید
</a-button>
</a-flex>
<a-alert v-if="error" type="error" show-icon :message="error" />
<a-spin :spinning="loading">
<a-card v-if="listEmpty && !error" :bordered="false">
<a-empty
description="هنوز فرمی نساخته‌اید. با ساخت اولین فرم، می‌توانید در وب‌سایت یا شبکه‌های اجتماعی سرنخ و مشتری جذب کنید."
>
<a-button type="primary" :loading="saving" @click="createForm">
<template #icon><PlusOutlined /></template>
ساخت اولین فرم
</a-button>
</a-empty>
</a-card>
<a-card v-else-if="!error && forms.length" :bordered="false">
<a-flex vertical :gap="12">
<a-typography-text type="secondary">
{{ countLabel }} فرم ساخته شده
</a-typography-text>
<a-list item-layout="horizontal" :data-source="forms">
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-space wrap>
<RouterLink
:to="{ name: 'lead-form-builder', params: { id: item.id } }"
custom
v-slot="{ href, navigate }"
>
<a-button :href="href" @click="navigate">
<template #icon><EditOutlined /></template>
ویرایش فیلدها
</a-button>
</RouterLink>
<a-button @click="copyPublishLink(item)">
<template #icon><CopyOutlined /></template>
کپی لینک انتشار
</a-button>
<a-button :href="item.publicPath" target="_blank" rel="noopener">
<template #icon><EyeOutlined /></template>
پیش‌نمایش
</a-button>
<a-button danger @click="removeForm(item)">
<template #icon><DeleteOutlined /></template>
حذف
</a-button>
</a-space>
</template>
<a-list-item-meta :description="formMeta(item)">
<template #title>
<a-space :size="8" align="center" wrap>
<a-typography-text strong>{{ item.name }}</a-typography-text>
<a-tag :color="item.isActive ? 'success' : 'default'" :bordered="false">
{{ item.isActive ? 'فعال' : 'غیرفعال' }}
</a-tag>
</a-space>
</template>
</a-list-item-meta>
</a-list-item>
</template>
</a-list>
</a-flex>
</a-card>
</a-spin>
</a-flex>
</template>