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

299 lines
9.2 KiB
Vue
Raw Normal View History

<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';
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';
import { PlusOutlined, ReloadOutlined } from '@/icons/ant-bridge';
import { CrmSkeleton, CrmSkeletonList } from '@/components/skeleton';
import { PageHeader } from '@/components/page';
import { ControlOutlined } from '@/icons/ant-bridge';
const { Text: ATypographyText } = Typography;
const loading = ref(false);
const saving = ref(false);
const error = ref('');
const fields = ref<CustomFieldDefinitionDto[]>([]);
const filter = ref<string>('all');
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: 'معامله',
note: 'یادداشت',
};
const typeLabels: Record<CustomFieldType, string> = {
text: 'متن',
number: 'عدد',
date: 'تاریخ',
boolean: 'بله/خیر',
select: 'لیست انتخابی',
};
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,
];
const visible = computed(() =>
filter.value === 'all'
? fields.value
: fields.value.filter((f) => f.entityType === filter.value),
);
function entityTagColor(entity: CustomFieldEntity) {
if (entity === 'deal') return 'warning';
if (entity === 'company') return 'processing';
return 'blue';
}
function entityLabelOf(field: CustomFieldDefinitionDto) {
return entityLabels[field.entityType];
}
function typeLabelOf(field: CustomFieldDefinitionDto) {
return typeLabels[field.fieldType];
}
function fieldDescription(field: CustomFieldDefinitionDto) {
const parts = [`کلید سیستمی: ${field.key}`];
if (field.options?.length) parts.push(`گزینه‌ها: ${field.options.join('، ')}`);
return parts.join(' · ');
}
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>
<a-flex vertical :gap="16">
<PageHeader
title="فیلدهای سفارشی"
:icon="ControlOutlined"
/>
<a-alert v-if="error" type="error" show-icon :message="error" closable @close="error = ''" />
<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"
placeholder="مثلاً فناوری، تولید، خدمات"
/>
</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>
<a-divider orientation="right">فهرست فیلدها</a-divider>
<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>
<CrmSkeleton :loading="loading">
<CrmSkeletonList :rows="6" :avatar="false">
<a-empty
v-if="!loading && !visible.length"
description="فیلدی تعریف نشده است. هیچ فیلد سفارشی برای این موجودیت وجود ندارد."
/>
<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">
{{ entityLabelOf(item) }}
</a-tag>
<a-tag :bordered="false">{{ typeLabelOf(item) }}</a-tag>
<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>
</CrmSkeletonList>
</CrmSkeleton>
</a-card>
</a-flex>
</template>