Files
CRM_Montzar/apps/web/src/components/routines/EntityLinkPicker.vue
T
MahdiandCursor 4abab55a6b feat(web): migrate CRM screens to Ant Design Vue with central theme
Adopt ant-design-vue and a shared antdTheme for leads, pipeline, invoices, and shared form controls under strict single-page governance.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-18 19:39:59 +03:30

168 lines
4.9 KiB
Vue

<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue';
import type { CompanyDto, LeadDto, PaginatedResponse, PersonDto } from '@mento/shared';
import {
Button as AButton,
Flex as AFlex,
Input as AInput,
Select as ASelect,
Space as ASpace,
Tag as ATag,
Typography,
} from 'ant-design-vue';
import { CloseOutlined } from '@ant-design/icons-vue';
import { api } from '@/lib/api';
import { ENTITY_TYPE_LABELS } from '@/lib/routines';
import { useAuthStore } from '@/stores/auth';
const { Text: ATypographyText } = Typography;
export type PickedEntity = { type: 'lead' | 'person' | 'company'; id: string; label: string };
const props = withDefaults(
defineProps<{
/** محدود کردن نوع پرونده (مثلاً وقتی روتین «الزامی به لید» است) */
only?: 'lead' | 'customer' | null;
}>(),
{ only: null },
);
const model = defineModel<PickedEntity | null>({ default: null });
const auth = useAuthStore();
function moduleOn(key: string) {
const mods = auth.user?.enabledModules;
return !mods || mods.length === 0 || mods.includes(key as never);
}
const typeOptions = computed(() => {
const list: Array<PickedEntity['type']> = [];
if (props.only !== 'customer' && moduleOn('leads')) list.push('lead');
if (props.only !== 'lead' && moduleOn('customers')) list.push('person', 'company');
return list;
});
const selectOptions = computed(() =>
typeOptions.value.map((t) => ({ value: t, label: ENTITY_TYPE_LABELS[t] })),
);
const type = ref<PickedEntity['type']>(typeOptions.value[0] ?? 'person');
const q = ref('');
const results = ref<PickedEntity[]>([]);
const loading = ref(false);
let timer: ReturnType<typeof setTimeout> | null = null;
let seq = 0;
watch(typeOptions, (opts) => {
if (!opts.includes(type.value) && opts[0]) type.value = opts[0];
});
async function search() {
const term = q.value.trim();
const mine = ++seq;
if (term.length < 2) {
results.value = [];
loading.value = false;
return;
}
loading.value = true;
try {
const params = { q: term, page: 1, limit: 8 };
let next: PickedEntity[] = [];
if (type.value === 'lead') {
const { data } = await api.get<PaginatedResponse<LeadDto>>('/leads', { params });
next = data.items.map((l) => ({
type: 'lead',
id: l.id,
label: l.title || l.contactName || l.mobile,
}));
} else if (type.value === 'person') {
const { data } = await api.get<PaginatedResponse<PersonDto>>('/people', { params });
next = data.items.map((p) => ({
type: 'person',
id: p.id,
label: `${p.firstName} ${p.lastName}`.trim(),
}));
} else {
const { data } = await api.get<PaginatedResponse<CompanyDto>>('/companies', { params });
next = data.items.map((c) => ({ type: 'company', id: c.id, label: c.name }));
}
if (mine === seq) results.value = next;
} catch {
if (mine === seq) results.value = [];
} finally {
if (mine === seq) loading.value = false;
}
}
watch([q, type], () => {
if (timer) clearTimeout(timer);
timer = setTimeout(search, 280);
});
onBeforeUnmount(() => {
if (timer) clearTimeout(timer);
});
function pick(item: PickedEntity) {
model.value = item;
q.value = '';
results.value = [];
}
</script>
<template>
<a-flex vertical :gap="8" style="width: 100%">
<a-flex v-if="model" justify="space-between" align="center" :gap="8">
<a-space :size="8">
<a-tag :bordered="false">{{ ENTITY_TYPE_LABELS[model.type] }}</a-tag>
<a-typography-text strong>{{ model.label }}</a-typography-text>
</a-space>
<a-button type="text" aria-label="حذف اتصال" @click="model = null">
<template #icon><CloseOutlined /></template>
</a-button>
</a-flex>
<template v-else-if="typeOptions.length">
<a-flex :gap="8" wrap="wrap">
<a-select
v-if="typeOptions.length > 1"
v-model:value="type"
:options="selectOptions"
style="width: 120px"
/>
<a-input
v-model:value="q"
:placeholder="`جستجوی ${ENTITY_TYPE_LABELS[type]}…`"
style="flex: 1; min-width: 160px"
/>
</a-flex>
<a-flex
v-if="results.length || loading || q.trim().length >= 2"
vertical
:gap="4"
>
<a-typography-text v-if="loading" type="secondary">در حال جستجو…</a-typography-text>
<a-typography-text v-else-if="!results.length" type="secondary">
موردی یافت نشد
</a-typography-text>
<a-button
v-for="r in results"
:key="r.id"
type="text"
block
style="text-align: start"
@click="pick(r)"
>
{{ r.label }}
</a-button>
</a-flex>
</template>
<a-typography-text v-else type="secondary">
ماژول لید/مشتری برای سازمان فعال نیست.
</a-typography-text>
</a-flex>
</template>