Files
CRM_Montzar/apps/web/src/views/AutomationsSettingsView.vue
T
MahdiandCursor 092767b6f2 Align older CRM pages with the Mento visual language.
Restyle list, form, settings, and secondary modules to match contacts/activities patterns with teal chrome, soft panels, and mobile layouts — without changing already-redesigned screens.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-05 02:29:41 +03:30

410 lines
9.3 KiB
Vue

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import axios from 'axios';
import { api } from '@/lib/api';
import type {
AutomationAction,
AutomationRuleDto,
AutomationTrigger,
CreateAutomationRuleRequest,
} from '@mento/shared';
const AUTOMATION_TRIGGER_LABELS: Record<AutomationTrigger, string> = {
deal_won: 'معامله برنده شد',
ticket_resolved: 'تیکت حل شد',
};
const AUTOMATION_ACTION_LABELS: Record<AutomationAction, string> = {
create_activity: 'ساخت فعالیت',
create_notification: 'ارسال اعلان',
add_loyalty_points: 'افزودن امتیاز باشگاه',
};
const loading = ref(false);
const saving = ref(false);
const error = ref('');
const ok = ref('');
const items = ref<AutomationRuleDto[]>([]);
const editingId = ref<string | null>(null);
const name = ref('');
const trigger = ref<AutomationTrigger>('deal_won');
const action = ref<AutomationAction>('create_activity');
const isActive = ref(true);
const title = ref('');
const body = ref('');
const points = ref(100);
const triggers = Object.keys(AUTOMATION_TRIGGER_LABELS) as AutomationTrigger[];
const actions = Object.keys(AUTOMATION_ACTION_LABELS) as AutomationAction[];
function resetForm() {
editingId.value = null;
name.value = '';
trigger.value = 'deal_won';
action.value = 'create_activity';
isActive.value = true;
title.value = '';
body.value = '';
points.value = 100;
}
function startEdit(row: AutomationRuleDto) {
editingId.value = row.id;
name.value = row.name;
trigger.value = row.trigger;
action.value = row.action;
isActive.value = row.isActive;
title.value = row.config.title ?? '';
body.value = row.config.body ?? '';
points.value = row.config.points ?? 100;
ok.value = '';
}
async function load() {
loading.value = true;
error.value = '';
try {
const { data } = await api.get<AutomationRuleDto[]>('/automations');
items.value = data;
} catch (e) {
if (axios.isAxiosError(e) && !e.response) {
error.value = 'اتصال به API برقرار نشد.';
} else {
error.value = 'بارگذاری اتوماسیون ناموفق بود.';
}
} finally {
loading.value = false;
}
}
async function save() {
if (name.value.trim().length < 2) {
error.value = 'نام قانون را وارد کنید.';
return;
}
saving.value = true;
error.value = '';
ok.value = '';
try {
const payload: CreateAutomationRuleRequest = {
name: name.value.trim(),
trigger: trigger.value,
action: action.value,
isActive: isActive.value,
config: {
title: title.value.trim() || undefined,
body: body.value.trim() || undefined,
points:
action.value === 'add_loyalty_points'
? Math.max(1, Math.floor(points.value))
: undefined,
},
};
if (editingId.value) {
await api.patch(`/automations/${editingId.value}`, payload);
ok.value = 'قانون به‌روز شد.';
} else {
await api.post('/automations', payload);
ok.value = 'قانون ساخته شد.';
}
resetForm();
await load();
} catch {
error.value = 'ذخیره قانون ناموفق بود.';
} finally {
saving.value = false;
}
}
async function removeRule(id: string) {
if (!confirm('قانون حذف شود؟')) return;
try {
await api.delete(`/automations/${id}`);
await load();
} catch {
error.value = 'حذف ناموفق بود.';
}
}
async function toggleActive(row: AutomationRuleDto) {
try {
await api.patch(`/automations/${row.id}`, { isActive: !row.isActive });
await load();
} catch {
error.value = 'تغییر وضعیت ناموفق بود.';
}
}
onMounted(load);
</script>
<template>
<section class="page">
<header class="page-head">
<div class="head-copy">
<h1>اگر / آنگاه</h1>
<p class="lead">
بعد از بردن معامله یا حل شدن تیکت، فعالیت، اعلان یا امتیاز باشگاه بسازید.
</p>
</div>
<button class="btn ghost" type="button" :disabled="loading" @click="load">
بروزرسانی
</button>
</header>
<p v-if="error" class="error">{{ error }}</p>
<p v-if="ok" class="ok">{{ ok }}</p>
<form class="panel form" @submit.prevent="save">
<h2>{{ editingId ? 'ویرایش قانون' : 'قانون جدید' }}</h2>
<label>
نام
<input v-model="name" type="text" required minlength="2" maxlength="120" />
</label>
<div class="grid2">
<label>
اگر (محرک)
<select v-model="trigger">
<option v-for="t in triggers" :key="t" :value="t">
{{ AUTOMATION_TRIGGER_LABELS[t] }}
</option>
</select>
</label>
<label>
آنگاه (عمل)
<select v-model="action">
<option v-for="a in actions" :key="a" :value="a">
{{ AUTOMATION_ACTION_LABELS[a] }}
</option>
</select>
</label>
</div>
<label>
عنوان / متن عمل
<input v-model="title" type="text" maxlength="200" placeholder="اختیاری" />
</label>
<label>
توضیح
<textarea v-model="body" rows="3" maxlength="1000" placeholder="اختیاری" />
</label>
<label v-if="action === 'add_loyalty_points'">
امتیاز
<input v-model.number="points" type="number" min="1" />
</label>
<label class="check">
<input v-model="isActive" type="checkbox" />
فعال
</label>
<div class="actions">
<button class="btn primary" type="submit" :disabled="saving">
{{ saving ? 'در حال ذخیره…' : editingId ? 'به‌روزرسانی' : 'ساخت' }}
</button>
<button
v-if="editingId"
class="btn ghost"
type="button"
@click="resetForm"
>
انصراف
</button>
</div>
</form>
<section class="panel">
<h2>قوانین</h2>
<ul class="list">
<li v-for="row in items" :key="row.id">
<div>
<strong>{{ row.name }}</strong>
<p class="muted">
اگر {{ AUTOMATION_TRIGGER_LABELS[row.trigger] }} →
{{ AUTOMATION_ACTION_LABELS[row.action] }}
<span v-if="!row.isActive"> · غیرفعال</span>
</p>
</div>
<div class="row-actions">
<button class="link" type="button" @click="toggleActive(row)">
{{ row.isActive ? 'غیرفعال' : 'فعال' }}
</button>
<button class="link" type="button" @click="startEdit(row)">ویرایش</button>
<button class="link danger" type="button" @click="removeRule(row.id)">
حذف
</button>
</div>
</li>
</ul>
<p v-if="!items.length && !loading" class="muted">هنوز قانونی نیست.</p>
</section>
</section>
</template>
<style scoped>
.page {
display: grid;
gap: 1rem;
max-width: 860px;
min-width: 0;
overflow-x: clip;
}
.page-head {
display: flex;
justify-content: space-between;
gap: 1rem;
flex-wrap: wrap;
align-items: flex-start;
}
.head-copy {
min-width: 0;
}
h1 {
margin: 0 0 0.3rem;
font-size: clamp(1.45rem, 2.5vw, 1.85rem);
font-weight: 800;
}
.lead,
.muted {
margin: 0;
color: #64748b;
}
.panel {
border: 1px solid #e2e8f0;
border-radius: 18px;
padding: 1.1rem;
background: #fff;
box-shadow: 0 8px 22px rgba(15, 23, 42, 0.04);
display: grid;
gap: 0.75rem;
}
h2 {
margin: 0;
font-size: 1.05rem;
font-weight: 700;
color: #0f172a;
}
.form label {
display: grid;
gap: 0.35rem;
font-size: 0.9rem;
color: #64748b;
}
input,
select,
textarea {
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 0.6rem 0.75rem;
font: inherit;
color: #0f172a;
background: #fff;
}
.grid2 {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.65rem;
}
.check {
display: flex !important;
align-items: center;
gap: 0.45rem;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 42px;
padding: 0.55rem 1rem;
border-radius: 12px;
font-weight: 700;
border: 1px solid transparent;
cursor: pointer;
font: inherit;
}
.btn.primary {
background: #14b8a6;
color: #fff;
box-shadow: 0 8px 18px rgba(20, 184, 166, 0.24);
}
.btn.ghost {
background: #fff;
color: #0f766e;
border-color: #99f6e4;
box-shadow: none;
}
.actions,
.row-actions {
display: flex;
gap: 0.55rem;
flex-wrap: wrap;
}
.list {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.7rem;
}
.list li {
display: flex;
justify-content: space-between;
gap: 0.75rem;
flex-wrap: wrap;
border-bottom: 1px solid #e2e8f0;
padding-bottom: 0.65rem;
}
.link {
background: none;
border: none;
color: #0f766e;
cursor: pointer;
font: inherit;
padding: 0;
font-weight: 600;
}
.danger {
color: #b91c1c;
}
.error {
margin: 0;
color: #b91c1c;
}
.ok {
margin: 0;
color: #0f766e;
font-weight: 600;
}
@media (max-width: 700px) {
.grid2 {
grid-template-columns: 1fr;
}
.actions,
.row-actions,
.list li {
flex-direction: column;
align-items: stretch;
}
}
</style>