Files
CRM_Montzar/apps/web/src/components/routines/EntityLinkPicker.vue
T

225 lines
5.7 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue';
import type { CompanyDto, LeadDto, PaginatedResponse, PersonDto } from '@mento/shared';
import UiIcon from '@/components/UiIcon.vue';
import { UiInput, UiSelect } from '@/components/ui';
import { api } from '@/lib/api';
import { ENTITY_TYPE_LABELS } from '@/lib/routines';
import { useAuthStore } from '@/stores/auth';
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 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>
<div class="ent-picker">
<div v-if="model" class="picked">
<span class="picked-type">{{ ENTITY_TYPE_LABELS[model.type] }}</span>
<strong>{{ model.label }}</strong>
<button type="button" aria-label="حذف اتصال" @click="model = null">
<UiIcon name="x" :size="14" />
</button>
</div>
<template v-else-if="typeOptions.length">
<div class="search-row">
<UiSelect v-if="typeOptions.length > 1" v-model="type" class="type-sel">
<option v-for="t in typeOptions" :key="t" :value="t">{{ ENTITY_TYPE_LABELS[t] }}</option>
</UiSelect>
<UiInput v-model="q" :placeholder="`جستجوی ${ENTITY_TYPE_LABELS[type]}…`" />
</div>
<ul v-if="results.length || loading || q.trim().length >= 2" class="results">
<li v-if="loading" class="muted">در حال جستجو…</li>
<li v-else-if="!results.length" class="muted">موردی یافت نشد</li>
<li v-for="r in results" :key="r.id">
<button type="button" @click="pick(r)">{{ r.label }}</button>
</li>
</ul>
</template>
<p v-else class="muted">ماژول لید/مشتری برای سازمان فعال نیست.</p>
</div>
</template>
<style scoped>
.ent-picker {
display: grid;
gap: var(--sp-2);
}
.search-row {
display: flex;
gap: var(--sp-2);
}
.type-sel {
width: 110px;
flex-shrink: 0;
}
.search-row > :last-child {
flex: 1 1 auto;
}
.results {
list-style: none;
margin: 0;
padding: 4px;
border: 1px solid var(--line);
border-radius: var(--radius);
max-height: 220px;
overflow-y: auto;
background: var(--surface);
}
.results li button {
width: 100%;
text-align: right;
border: 0;
background: transparent;
padding: 8px 10px;
border-radius: 8px;
cursor: pointer;
color: var(--ink);
}
.results li button:hover {
background: var(--accent-subtle);
color: var(--accent-deep);
}
.muted {
margin: 0;
padding: 8px 10px;
color: var(--muted);
font-size: var(--fs-sm);
}
.picked {
display: flex;
align-items: center;
gap: var(--sp-2);
padding: 6px 10px;
border-radius: var(--radius);
background: var(--accent-subtle);
border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
color: var(--accent-deep);
min-width: 0;
}
.picked strong {
flex: 1 1 auto;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.picked-type {
font-size: var(--fs-xs);
color: var(--muted);
}
.picked button {
border: 0;
background: transparent;
color: var(--muted);
cursor: pointer;
display: grid;
place-items: center;
width: 26px;
height: 26px;
border-radius: 6px;
}
.picked button:hover {
background: var(--surface);
color: var(--danger);
}
</style>