2026-08-29 21:06:30 +03:30
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { computed, onMounted, ref } from 'vue';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import { api } from '@/lib/api';
|
|
|
|
|
import type {
|
|
|
|
|
CreateCustomFieldRequest,
|
|
|
|
|
CustomFieldDefinitionDto,
|
|
|
|
|
CustomFieldEntity,
|
|
|
|
|
CustomFieldType,
|
|
|
|
|
} from '@mento/shared';
|
2026-11-04 10:01:11 +03:30
|
|
|
import {
|
|
|
|
|
Alert as AAlert,
|
|
|
|
|
Button as AButton,
|
|
|
|
|
Card as ACard,
|
|
|
|
|
Checkbox as ACheckbox,
|
|
|
|
|
Col as ACol,
|
|
|
|
|
Divider as ADivider,
|
|
|
|
|
Empty as AEmpty,
|
|
|
|
|
Flex as AFlex,
|
|
|
|
|
Form as AForm,
|
|
|
|
|
FormItem as AFormItem,
|
|
|
|
|
Input as AInput,
|
|
|
|
|
List as AList,
|
|
|
|
|
ListItem as AListItem,
|
|
|
|
|
ListItemMeta as AListItemMeta,
|
|
|
|
|
Row as ARow,
|
|
|
|
|
Select as ASelect,
|
|
|
|
|
Space as ASpace,
|
|
|
|
|
Tag as ATag,
|
|
|
|
|
Typography,
|
|
|
|
|
} from 'ant-design-vue';
|
2026-11-05 14:46:35 +03:30
|
|
|
import { PlusOutlined, ReloadOutlined } from '@/icons/ant-bridge';
|
|
|
|
|
import { CrmSkeleton, CrmSkeletonList } from '@/components/skeleton';
|
2026-11-04 10:01:11 +03:30
|
|
|
|
|
|
|
|
const { Title: ATypographyTitle, Text: ATypographyText } = Typography;
|
2026-08-29 21:06:30 +03:30
|
|
|
|
|
|
|
|
const loading = ref(false);
|
|
|
|
|
const saving = ref(false);
|
|
|
|
|
const error = ref('');
|
|
|
|
|
const fields = ref<CustomFieldDefinitionDto[]>([]);
|
2026-09-12 01:33:07 +03:30
|
|
|
const filter = ref<string>('all');
|
2026-08-29 21:06:30 +03:30
|
|
|
|
|
|
|
|
const label = ref('');
|
|
|
|
|
const entityType = ref<CustomFieldEntity>('person');
|
|
|
|
|
const fieldType = ref<CustomFieldType>('text');
|
|
|
|
|
const optionsRaw = ref('');
|
|
|
|
|
const required = ref(false);
|
|
|
|
|
|
|
|
|
|
const entityLabels: Record<CustomFieldEntity, string> = {
|
|
|
|
|
person: 'شخص',
|
|
|
|
|
company: 'شرکت',
|
|
|
|
|
deal: 'معامله',
|
2026-11-01 15:38:05 +03:30
|
|
|
note: 'یادداشت',
|
2026-08-29 21:06:30 +03:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const typeLabels: Record<CustomFieldType, string> = {
|
|
|
|
|
text: 'متن',
|
|
|
|
|
number: 'عدد',
|
|
|
|
|
date: 'تاریخ',
|
|
|
|
|
boolean: 'بله/خیر',
|
|
|
|
|
select: 'لیست انتخابی',
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
const entityOptions = [
|
|
|
|
|
{ value: 'person', label: 'شخص' },
|
|
|
|
|
{ value: 'company', label: 'شرکت' },
|
|
|
|
|
{ value: 'deal', label: 'معامله' },
|
|
|
|
|
{ value: 'note', label: 'یادداشت' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const typeOptions = [
|
|
|
|
|
{ value: 'text', label: 'متن' },
|
|
|
|
|
{ value: 'number', label: 'عدد' },
|
|
|
|
|
{ value: 'date', label: 'تاریخ' },
|
|
|
|
|
{ value: 'boolean', label: 'بله/خیر' },
|
|
|
|
|
{ value: 'select', label: 'لیست انتخابی' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const filterOptions = [
|
|
|
|
|
{ value: 'all', label: 'همه موجودیتها' },
|
|
|
|
|
...entityOptions,
|
|
|
|
|
];
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
const visible = computed(() =>
|
|
|
|
|
filter.value === 'all'
|
|
|
|
|
? fields.value
|
|
|
|
|
: fields.value.filter((f) => f.entityType === filter.value),
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
function entityTagColor(entity: CustomFieldEntity) {
|
|
|
|
|
if (entity === 'deal') return 'warning';
|
|
|
|
|
if (entity === 'company') return 'processing';
|
|
|
|
|
return 'blue';
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-04 10:02:51 +03:30
|
|
|
function entityLabelOf(field: CustomFieldDefinitionDto) {
|
|
|
|
|
return entityLabels[field.entityType];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function typeLabelOf(field: CustomFieldDefinitionDto) {
|
|
|
|
|
return typeLabels[field.fieldType];
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
function fieldDescription(field: CustomFieldDefinitionDto) {
|
|
|
|
|
const parts = [`کلید سیستمی: ${field.key}`];
|
|
|
|
|
if (field.options?.length) parts.push(`گزینهها: ${field.options.join('، ')}`);
|
|
|
|
|
return parts.join(' · ');
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
async function load() {
|
|
|
|
|
loading.value = true;
|
|
|
|
|
error.value = '';
|
|
|
|
|
try {
|
|
|
|
|
fields.value = (await api.get<CustomFieldDefinitionDto[]>('/custom-fields')).data;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (axios.isAxiosError(e) && !e.response) {
|
|
|
|
|
error.value = 'اتصال به API برقرار نشد.';
|
|
|
|
|
} else {
|
|
|
|
|
error.value = 'بارگذاری فیلدها ناموفق بود.';
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
loading.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createField() {
|
|
|
|
|
if (!label.value.trim()) {
|
|
|
|
|
error.value = 'عنوان فیلد را وارد کنید.';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
saving.value = true;
|
|
|
|
|
error.value = '';
|
|
|
|
|
try {
|
|
|
|
|
const payload: CreateCustomFieldRequest = {
|
|
|
|
|
entityType: entityType.value,
|
|
|
|
|
label: label.value.trim(),
|
|
|
|
|
fieldType: fieldType.value,
|
|
|
|
|
required: required.value,
|
|
|
|
|
options:
|
|
|
|
|
fieldType.value === 'select'
|
|
|
|
|
? optionsRaw.value
|
|
|
|
|
.split(/[,،\n]/)
|
|
|
|
|
.map((o) => o.trim())
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
: undefined,
|
|
|
|
|
};
|
|
|
|
|
await api.post('/custom-fields', payload);
|
|
|
|
|
label.value = '';
|
|
|
|
|
optionsRaw.value = '';
|
|
|
|
|
required.value = false;
|
|
|
|
|
await load();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (axios.isAxiosError(e)) {
|
|
|
|
|
const msg = (e.response?.data as { message?: string | string[] })?.message;
|
|
|
|
|
error.value = Array.isArray(msg)
|
|
|
|
|
? msg.join('، ')
|
|
|
|
|
: (msg ?? 'ثبت فیلد ناموفق بود');
|
|
|
|
|
} else {
|
|
|
|
|
error.value = 'ثبت فیلد ناموفق بود';
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
saving.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function toggleActive(field: CustomFieldDefinitionDto) {
|
|
|
|
|
await api.patch(`/custom-fields/${field.id}`, { isActive: !field.isActive });
|
|
|
|
|
await load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function removeField(field: CustomFieldDefinitionDto) {
|
|
|
|
|
if (!confirm(`فیلد «${field.label}» حذف شود؟`)) return;
|
|
|
|
|
await api.delete(`/custom-fields/${field.id}`);
|
|
|
|
|
await load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMounted(load);
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-flex vertical :gap="16">
|
|
|
|
|
<a-typography-title :level="3" :style="{ margin: 0 }">فیلدهای سفارشی</a-typography-title>
|
2026-08-29 21:06:30 +03:30
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-alert v-if="error" type="error" show-icon :message="error" closable @close="error = ''" />
|
2026-09-12 01:33:07 +03:30
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-card :bordered="false">
|
|
|
|
|
<a-form layout="vertical" @finish="createField">
|
|
|
|
|
<a-divider orientation="right">فیلد جدید</a-divider>
|
|
|
|
|
<a-row :gutter="[16, 0]">
|
|
|
|
|
<a-col :xs="24" :md="8">
|
|
|
|
|
<a-form-item label="عنوان" required>
|
|
|
|
|
<a-input v-model:value="label" placeholder="مثلاً صنعت" />
|
|
|
|
|
</a-form-item>
|
|
|
|
|
</a-col>
|
|
|
|
|
<a-col :xs="24" :md="8">
|
|
|
|
|
<a-form-item label="روی چه چیزی؟">
|
|
|
|
|
<a-select
|
|
|
|
|
v-model:value="entityType"
|
|
|
|
|
:options="entityOptions"
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
/>
|
|
|
|
|
</a-form-item>
|
|
|
|
|
</a-col>
|
|
|
|
|
<a-col :xs="24" :md="8">
|
|
|
|
|
<a-form-item label="نوع فیلد">
|
|
|
|
|
<a-select
|
|
|
|
|
v-model:value="fieldType"
|
|
|
|
|
:options="typeOptions"
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
/>
|
|
|
|
|
</a-form-item>
|
|
|
|
|
</a-col>
|
|
|
|
|
<a-col v-if="fieldType === 'select'" :span="24">
|
|
|
|
|
<a-form-item label="گزینهها (با کاما جدا کنید)">
|
|
|
|
|
<a-input
|
|
|
|
|
v-model:value="optionsRaw"
|
2026-09-12 01:33:07 +03:30
|
|
|
placeholder="مثلاً فناوری، تولید، خدمات"
|
|
|
|
|
/>
|
2026-11-04 10:01:11 +03:30
|
|
|
</a-form-item>
|
|
|
|
|
</a-col>
|
|
|
|
|
<a-col :span="24">
|
|
|
|
|
<a-form-item>
|
|
|
|
|
<a-checkbox v-model:checked="required">فیلد اجباری است</a-checkbox>
|
|
|
|
|
</a-form-item>
|
|
|
|
|
</a-col>
|
|
|
|
|
<a-col :span="24">
|
|
|
|
|
<a-button type="primary" html-type="submit" :loading="saving" :disabled="saving">
|
|
|
|
|
<template #icon><PlusOutlined /></template>
|
|
|
|
|
{{ saving ? 'در حال ثبت…' : 'افزودن فیلد' }}
|
|
|
|
|
</a-button>
|
|
|
|
|
</a-col>
|
|
|
|
|
</a-row>
|
|
|
|
|
</a-form>
|
2026-09-12 01:33:07 +03:30
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-divider orientation="right">فهرست فیلدها</a-divider>
|
2026-08-29 21:06:30 +03:30
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-row :gutter="[16, 16]" :style="{ marginBottom: '16px' }">
|
|
|
|
|
<a-col :xs="24" :md="12">
|
|
|
|
|
<a-select
|
|
|
|
|
v-model:value="filter"
|
|
|
|
|
:options="filterOptions"
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
/>
|
|
|
|
|
</a-col>
|
|
|
|
|
<a-col :xs="24" :md="8">
|
|
|
|
|
<a-button @click="load">
|
|
|
|
|
<template #icon><ReloadOutlined /></template>
|
|
|
|
|
بروزرسانی
|
|
|
|
|
</a-button>
|
|
|
|
|
</a-col>
|
|
|
|
|
</a-row>
|
2026-08-29 21:06:30 +03:30
|
|
|
|
2026-11-05 14:46:35 +03:30
|
|
|
<CrmSkeleton :loading="loading">
|
|
|
|
|
<CrmSkeletonList :rows="6" :avatar="false">
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-empty
|
|
|
|
|
v-if="!loading && !visible.length"
|
|
|
|
|
description="فیلدی تعریف نشده است. هیچ فیلد سفارشی برای این موجودیت وجود ندارد."
|
|
|
|
|
/>
|
2026-08-29 21:06:30 +03:30
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-list v-else-if="visible.length" item-layout="horizontal" :data-source="visible">
|
|
|
|
|
<template #renderItem="{ item }">
|
|
|
|
|
<a-list-item>
|
|
|
|
|
<template #actions>
|
|
|
|
|
<a-space wrap>
|
|
|
|
|
<a-button @click="toggleActive(item)">
|
|
|
|
|
{{ item.isActive ? 'غیرفعالسازی' : 'فعالسازی' }}
|
|
|
|
|
</a-button>
|
|
|
|
|
<a-button danger @click="removeField(item)">حذف</a-button>
|
|
|
|
|
</a-space>
|
|
|
|
|
</template>
|
|
|
|
|
<a-list-item-meta :description="fieldDescription(item)">
|
|
|
|
|
<template #title>
|
|
|
|
|
<a-space wrap>
|
|
|
|
|
<a-typography-text strong>{{ item.label }}</a-typography-text>
|
|
|
|
|
<a-tag :color="entityTagColor(item.entityType)" :bordered="false">
|
2026-11-04 10:02:51 +03:30
|
|
|
{{ entityLabelOf(item) }}
|
2026-11-04 10:01:11 +03:30
|
|
|
</a-tag>
|
2026-11-04 10:02:51 +03:30
|
|
|
<a-tag :bordered="false">{{ typeLabelOf(item) }}</a-tag>
|
2026-11-04 10:01:11 +03:30
|
|
|
<a-tag v-if="item.required" color="error" :bordered="false">اجباری</a-tag>
|
|
|
|
|
<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>
|
2026-11-05 14:46:35 +03:30
|
|
|
</CrmSkeletonList>
|
|
|
|
|
</CrmSkeleton>
|
2026-11-04 10:01:11 +03:30
|
|
|
</a-card>
|
|
|
|
|
</a-flex>
|
2026-08-29 21:06:30 +03:30
|
|
|
</template>
|