setting pushed

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-24 09:48:27 +03:30
co-authored by Cursor
parent a75c4d915d
commit 6bbd8704b8
17 changed files with 2338 additions and 14 deletions
@@ -0,0 +1,736 @@
<template>
<a-form
ref="formRef"
:model="form"
layout="vertical"
autocomplete="off"
class="auto-editor"
>
<div class="auto-editor__grid">
<div class="auto-editor__col">
<a-form-item>
<template #label>
<AdminLabelWithHelp label="مخاطب" :help="automationAudienceHelp" />
</template>
<a-radio-group
:value="audienceMode"
option-type="button"
button-style="solid"
@update:value="setAudienceMode"
>
<a-radio-button value="all">همه کاربران</a-radio-button>
<a-radio-button value="filter">با شرط</a-radio-button>
</a-radio-group>
</a-form-item>
<template v-if="audienceMode === 'filter'">
<div class="auto-editor__conds">
<div
v-for="(row, idx) in form.conditions"
:key="idx"
class="auto-editor__cond"
>
<div v-if="idx > 0" class="auto-editor__join">
<a-radio-group
:value="row.cr || 'and'"
option-type="button"
button-style="solid"
size="small"
@update:value="(v) => (row.cr = v)"
>
<a-radio-button value="and">و</a-radio-button>
<a-radio-button value="or">یا</a-radio-button>
</a-radio-group>
</div>
<div class="auto-editor__cond-row">
<BasicSelect
class="auto-editor__cond-select"
:value="row.field"
:items="fieldItems"
key-name="key"
value-name="name"
placeholder="نوع شرط"
:has-tooltip="false"
:dropdown-match-select-width="false"
:dropdown-style="wideDropdownStyle"
@update:value="(v) => onFieldChange(row, v)"
/>
<template v-for="inp in inputsOf(row.field)" :key="inp.key">
<BasicSelect
v-if="inp.type === 'operator'"
class="auto-editor__cond-select"
:value="row.op || 'eq'"
:items="catalog?.operators || []"
key-name="key"
value-name="name"
:placeholder="inp.label"
:has-tooltip="false"
@update:value="(v) => (row.op = v)"
/>
<BasicSelect
v-else-if="inp.type === 'select'"
class="auto-editor__cond-select"
:value="row.value"
:items="inp.options || []"
key-name="key"
value-name="name"
:placeholder="inp.label"
:has-tooltip="false"
@update:value="(v) => (row.value = v)"
/>
<BasicInput
v-else
class="auto-editor__cond-input"
:value="(row.value as any)"
type="number"
:place-holder="inp.label"
@update:value="(v) => (row.value = v)"
/>
</template>
<BaseButton danger size="small" @click="removeCondition(idx)">حذف</BaseButton>
</div>
</div>
</div>
<BaseButton size="small" type="dashed" @click="addCondition">افزودن شرط</BaseButton>
</template>
<div class="auto-editor__hint">مخاطب تقریبی: {{ previewText }}</div>
</div>
<div class="auto-editor__col">
<div class="auto-editor__pair">
<a-form-item name="name" :rules="inputRules()">
<template #label>
<AdminLabelWithHelp label="نام" :help="automationNameHelp" />
</template>
<BasicInput v-model:value="form.name" placeholder="نام اتوماسیون" />
</a-form-item>
<a-form-item name="title" :rules="inputRules()">
<template #label>
<AdminLabelWithHelp label="عنوان پیام" :help="automationTitleHelp" />
</template>
<BasicInput v-model:value="form.title" placeholder="عنوان پیامک / اعلان" />
</a-form-item>
</div>
<a-form-item name="body" :rules="inputRules()" class="auto-editor__body">
<template #label>
<AdminLabelWithHelp label="متن" :help="automationBodyHelp" />
</template>
<a-textarea
ref="bodyAreaRef"
v-model:value="form.body"
class="auto-editor__textarea"
:rows="4"
placeholder="متن پیام را بنویسید…"
@blur="rememberBodyCursor"
@click="rememberBodyCursor"
@keyup="rememberBodyCursor"
@select="rememberBodyCursor"
/>
<div class="auto-editor__vars">
<button
v-for="v in bodyVars"
:key="v.token"
type="button"
class="auto-editor__var-chip"
:title="v.help"
@mousedown.prevent="insertBodyToken(v.token)"
>
<span class="auto-editor__var-token">%{{ v.token }}</span>
<span>{{ v.label }}</span>
</button>
</div>
</a-form-item>
<a-form-item name="trigger_type" :rules="inputRules()">
<template #label>
<AdminLabelWithHelp label="نوع اجرا" :help="automationTriggerTypeHelp" />
</template>
<a-radio-group
v-model:value="form.trigger_type"
option-type="button"
button-style="solid"
>
<a-radio-button
v-for="item in catalog?.triggers || []"
:key="String(item.key)"
:value="item.key"
>
{{ item.name }}
</a-radio-button>
</a-radio-group>
</a-form-item>
<a-form-item v-if="form.trigger_type === 'schedule'">
<template #label>
<AdminLabelWithHelp label="هر چند روز یک‌بار" :help="automationEveryDaysHelp" />
</template>
<BasicInput
v-model:value="form.trigger_config.every_days"
type="number"
placeholder="مثلاً ۲۰"
/>
</a-form-item>
<template v-if="form.trigger_type === 'event'">
<div class="auto-editor__pair">
<a-form-item>
<template #label>
<AdminLabelWithHelp label="رویداد" :help="automationEventHelp" />
</template>
<BasicSelect
v-model:value="form.trigger_config.event"
:items="catalog?.events || []"
key-name="key"
value-name="name"
placeholder="انتخاب رویداد"
:has-tooltip="false"
/>
</a-form-item>
<a-form-item>
<template #label>
<AdminLabelWithHelp label="زمان ارسال" :help="automationEventTimingHelp" />
</template>
<a-radio-group
:value="eventTiming"
option-type="button"
button-style="solid"
@update:value="setEventTiming"
>
<a-radio-button value="immediate">سر ضرب</a-radio-button>
<a-radio-button value="delayed">با تأخیر</a-radio-button>
</a-radio-group>
</a-form-item>
</div>
<a-form-item v-if="eventTiming === 'delayed'">
<template #label>
<AdminLabelWithHelp label="تأخیر (ساعت)" :help="automationDelayHoursHelp" />
</template>
<BasicInput
v-model:value="form.trigger_config.delay_hours"
type="number"
placeholder="۲۴"
/>
</a-form-item>
</template>
<template v-if="form.trigger_type === 'price'">
<div class="auto-editor__pair">
<a-form-item>
<template #label>
<AdminLabelWithHelp label="بازه" :help="automationPriceWindowHelp" />
</template>
<a-radio-group
v-model:value="form.trigger_config.price_window"
option-type="button"
button-style="solid"
>
<a-radio-button value="calendar_day">در یک روز</a-radio-button>
<a-radio-button value="from_last">از آخرین ارسال</a-radio-button>
</a-radio-group>
</a-form-item>
<a-form-item>
<template #label>
<AdminLabelWithHelp label="جهت" :help="automationPriceDirectionHelp" />
</template>
<a-radio-group
v-model:value="form.trigger_config.direction"
option-type="button"
button-style="solid"
>
<a-radio-button value="up">بالا</a-radio-button>
<a-radio-button value="down">پایین</a-radio-button>
<a-radio-button value="both">هر دو</a-radio-button>
</a-radio-group>
</a-form-item>
</div>
<div class="auto-editor__pair">
<a-form-item>
<template #label>
<AdminLabelWithHelp label="رقم تغییر (تومان)" :help="automationPriceStepHelp" />
</template>
<BasicInput
v-model:value="form.trigger_config.step_toman"
type="price"
addon-before="تومان"
placeholder="۱۰۰۰۰۰"
/>
</a-form-item>
<a-form-item>
<template #label>
<AdminLabelWithHelp label="محصول" :help="automationPriceProductHelp" />
</template>
<BasicSelect
v-model:value="form.trigger_config.product_id"
:items="productItems"
key-name="id"
value-name="name"
placeholder="خالی = قیمت تابلو"
allow-clear
:has-tooltip="false"
/>
</a-form-item>
</div>
</template>
<div class="auto-editor__pair">
<a-form-item v-if="form.trigger_type !== 'schedule'">
<template #label>
<AdminLabelWithHelp
label="فاصلهٔ تکرار برای هر نفر (ساعت)"
:help="automationCooldownHelp"
/>
</template>
<BasicInput v-model:value="form.cooldown_hours" type="number" placeholder="۲۴" />
</a-form-item>
<a-form-item>
<template #label>
<AdminLabelWithHelp label="حداکثر ارسال به هر نفر" :help="automationMaxSendsHelp" />
</template>
<BasicInput v-model:value="form.max_sends" type="number" placeholder="خالی = نامحدود" />
</a-form-item>
</div>
<div class="auto-editor__pair">
<a-form-item>
<template #label>
<AdminLabelWithHelp label="پیامک" :help="automationSmsHelp" />
</template>
<div class="auto-editor__channel">
<a-switch :checked="hasSms" @change="(v) => toggleChannel('sms', !!v)" />
<BasicSelect
v-if="hasSms && showChannelHours"
class="auto-editor__hour-select"
:value="form.sms_send_hour"
:items="catalog?.hours || []"
key-name="key"
value-name="name"
placeholder="ساعت"
:has-tooltip="false"
@update:value="(v) => (form.sms_send_hour = v)"
/>
</div>
</a-form-item>
<a-form-item>
<template #label>
<AdminLabelWithHelp label="اعلان داخل اپ" :help="automationInAppHelp" />
</template>
<div class="auto-editor__channel">
<a-switch :checked="hasInApp" @change="(v) => toggleChannel('in_app', !!v)" />
<BasicSelect
v-if="hasInApp && showChannelHours"
class="auto-editor__hour-select"
:value="form.in_app_send_hour"
:items="catalog?.hours || []"
key-name="key"
value-name="name"
placeholder="ساعت"
:has-tooltip="false"
@update:value="(v) => (form.in_app_send_hour = v)"
/>
</div>
</a-form-item>
</div>
<div class="auto-editor__pair">
<a-form-item>
<template #label>
<AdminLabelWithHelp label="شروع از" :help="automationStartsAtHelp" />
</template>
<BaseDatePicker
v-model:value="form.starts_at"
show-time
format="YYYY-MM-DD HH:mm:ss"
class="w-full"
/>
</a-form-item>
<a-form-item>
<template #label>
<AdminLabelWithHelp label="پایان در" :help="automationEndsAtHelp" />
</template>
<BaseDatePicker
v-model:value="form.ends_at"
show-time
format="YYYY-MM-DD HH:mm:ss"
class="w-full"
/>
</a-form-item>
</div>
<a-form-item>
<template #label>
<AdminLabelWithHelp label="وضعیت" :help="automationStatusHelp" />
</template>
<a-switch
v-model:checked="form.is_active"
checked-children="فعال"
un-checked-children="غیرفعال"
/>
</a-form-item>
</div>
</div>
</a-form>
</template>
<script setup lang="ts">
import { computed, nextTick, onMounted, ref, watch } from 'vue'
import type { FormInstance } from 'ant-design-vue'
import { cloneDeep, debounce, isEmpty } from 'lodash'
import { inputRules } from '@/utils/inputRules'
import { getAutomationCatalog, previewAutomationAudience } from '@/api/automations'
import BaseDatePicker from '@core/components/BaseDatePicker'
import AdminLabelWithHelp from '@/components/AdminLabelWithHelp.vue'
import {
automationAudienceHelp,
automationBodyHelp,
automationCooldownHelp,
automationDelayHoursHelp,
automationEndsAtHelp,
automationEventHelp,
automationEventTimingHelp,
automationEveryDaysHelp,
automationInAppHelp,
automationMaxSendsHelp,
automationNameHelp,
automationPriceDirectionHelp,
automationPriceProductHelp,
automationPriceStepHelp,
automationPriceWindowHelp,
automationSmsHelp,
automationStartsAtHelp,
automationStatusHelp,
automationTitleHelp,
automationTriggerTypeHelp,
} from '@/utils/adminHelpCopy'
import type {
Automation,
AutomationCatalog,
AutomationChannel,
AutomationCondition,
AutomationField,
} from '@/model/automations'
const props = defineProps<{
form: Automation
}>()
const formRef = ref<FormInstance>()
const catalog = ref<AutomationCatalog | null>(null)
const previewCount = ref<number | null>(null)
const bodyAreaRef = ref<{ $el?: HTMLElement } | null>(null)
let bodyCursorStart = 0
let bodyCursorEnd = 0
const bodyVars = [
{ token: 'نام', label: 'نام کاربر', help: 'نام کامل مشتری' },
{ token: 'نام_کوچک', label: 'نام کوچک', help: 'نام کوچک مشتری' },
{ token: 'موبایل', label: 'موبایل', help: 'شماره موبایل مشتری' },
]
function getBodyTextarea(): HTMLTextAreaElement | null {
const root = bodyAreaRef.value?.$el
if (!root) return null
if (root instanceof HTMLTextAreaElement) return root
return root.querySelector?.('textarea') ?? null
}
function rememberBodyCursor() {
const ta = getBodyTextarea()
if (!ta) return
bodyCursorStart = ta.selectionStart ?? 0
bodyCursorEnd = ta.selectionEnd ?? bodyCursorStart
}
function insertBodyToken(token: string) {
rememberBodyCursor()
const insert = `%${token}`
const text = props.form.body ?? ''
const start = bodyCursorStart
const end = bodyCursorEnd
props.form.body = text.slice(0, start) + insert + text.slice(end)
const nextPos = start + insert.length
bodyCursorStart = nextPos
bodyCursorEnd = nextPos
nextTick(() => {
const ta = getBodyTextarea()
if (!ta) return
ta.focus()
ta.setSelectionRange(nextPos, nextPos)
})
}
const fieldItems = computed(() =>
(catalog.value?.fields || []).map((f) => ({
key: f.key,
name: `${f.group} › ${f.name}`,
}))
)
const productItems = computed(() => catalog.value?.products || [])
const wideDropdownStyle = {
minWidth: '22rem',
maxWidth: 'min(40rem, 94vw)',
}
const eventTiming = computed<'immediate' | 'delayed'>(() =>
Number(props.form.trigger_config?.delay_hours ?? 0) > 0 ? 'delayed' : 'immediate'
)
const audienceMode = computed<'all' | 'filter'>(() =>
props.form.audience_mode === 'filter' || (props.form.conditions || []).some((r) => r.field)
? 'filter'
: 'all'
)
const showChannelHours = computed(() => {
if (props.form.trigger_type === 'event') return eventTiming.value === 'delayed'
if (props.form.trigger_type === 'price') {
return (props.form.trigger_config?.price_window || 'calendar_day') === 'from_last'
}
return true
})
const previewText = computed(() => {
if (previewCount.value == null) return '—'
return `${previewCount.value.toLocaleString('fa-IR')} نفر`
})
const hasSms = computed(() => (props.form.channels || []).includes('sms'))
const hasInApp = computed(() => (props.form.channels || []).includes('in_app'))
function ensureTriggerConfig() {
if (!props.form.trigger_config) {
props.form.trigger_config = {
delay_hours: 0,
every_days: 1,
direction: 'both',
price_window: 'calendar_day',
}
}
}
function setAudienceMode(mode: 'all' | 'filter') {
props.form.audience_mode = mode
if (mode === 'all') {
props.form.conditions = []
} else if (!props.form.conditions?.length) {
addCondition()
}
}
function setEventTiming(mode: 'immediate' | 'delayed') {
ensureTriggerConfig()
if (mode === 'immediate') {
props.form.trigger_config!.delay_hours = 0
} else if (!(Number(props.form.trigger_config!.delay_hours) > 0)) {
props.form.trigger_config!.delay_hours = 24
}
}
function fieldByKey(key?: string): AutomationField | undefined {
return (catalog.value?.fields || []).find((f) => f.key === key)
}
function inputsOf(field?: string) {
return fieldByKey(field)?.inputs || []
}
function onFieldChange(row: AutomationCondition, key: string) {
row.field = key
row.op = inputsOf(key).some((i) => i.type === 'operator') ? 'eq' : null
row.value = null
}
function addCondition() {
if (!props.form.conditions) props.form.conditions = []
props.form.audience_mode = 'filter'
props.form.conditions.push({
cr: 'and',
field: '',
op: null,
value: null,
})
}
function removeCondition(idx: number) {
props.form.conditions?.splice(idx, 1)
if (!props.form.conditions?.length) {
props.form.audience_mode = 'all'
}
}
function toggleChannel(ch: AutomationChannel, on: boolean) {
const cur = props.form.channels || []
if (on) {
if (!cur.includes(ch)) props.form.channels = [...cur, ch]
} else {
props.form.channels = cur.filter((c) => c !== ch)
}
}
const runPreview = debounce(async () => {
try {
const cond =
audienceMode.value === 'all'
? []
: (props.form.conditions || []).filter((r) => !isEmpty(r.field))
const data = await previewAutomationAudience(cloneDeep(cond))
previewCount.value = data?.count ?? 0
} catch {
previewCount.value = null
}
}, 400)
watch(
() => cloneDeep(props.form.conditions),
() => runPreview(),
{ deep: true }
)
watch(audienceMode, () => runPreview())
onMounted(async () => {
catalog.value = await getAutomationCatalog()
ensureTriggerConfig()
if (props.form.trigger_config!.every_days == null) {
props.form.trigger_config!.every_days = 1
}
if (!props.form.trigger_config!.price_window) {
props.form.trigger_config!.price_window = 'calendar_day'
}
if (!props.form.audience_mode) {
props.form.audience_mode = (props.form.conditions || []).some((r) => r.field)
? 'filter'
: 'all'
}
runPreview()
})
defineExpose({
validateFields: () => formRef.value?.validate(),
resetFields: () => formRef.value?.resetFields(),
})
</script>
<style scoped>
.auto-editor {
@apply flex flex-col;
}
.auto-editor__grid {
direction: ltr;
@apply grid grid-cols-1 items-start gap-x-8 gap-y-6 lg:grid-cols-2;
}
.auto-editor__col {
direction: rtl;
@apply flex min-w-0 flex-col gap-6;
}
.auto-editor__pair {
@apply grid grid-cols-1 gap-x-4 gap-y-6 sm:grid-cols-2;
}
.auto-editor__body :deep(textarea),
.auto-editor__textarea {
min-height: 4.5rem;
max-height: 7rem;
background: var(--admin-bg-elevated) !important;
border-color: var(--admin-border) !important;
color: var(--admin-text) !important;
}
.auto-editor__vars {
@apply mt-2 flex flex-wrap gap-2;
}
.auto-editor__var-chip {
@apply inline-flex items-center gap-1.5 rounded-lg border border-solid px-2.5 py-1 text-xs;
border-color: var(--admin-border);
background: var(--admin-bg-elevated);
color: var(--admin-text);
cursor: pointer;
}
.auto-editor__var-chip:hover {
border-color: var(--admin-border-strong, var(--admin-border));
}
.auto-editor__var-token {
@apply font-bold;
color: var(--admin-text-secondary);
}
.auto-editor :deep(.ant-form-item) {
margin-bottom: 0;
}
.auto-editor :deep(.ant-form-item-label) {
padding-bottom: 6px;
}
.auto-editor :deep(.ant-form-item-label > label) {
color: var(--admin-text);
height: auto;
}
.auto-editor__conds {
@apply mb-3 flex flex-col gap-3;
}
.auto-editor__cond {
@apply flex flex-col gap-2;
}
.auto-editor__join {
@apply flex;
}
.auto-editor__cond-row {
@apply flex flex-wrap items-center gap-2;
}
.auto-editor__cond-select,
.auto-editor__cond-input {
width: min(100%, 22rem);
min-width: 12rem;
flex: 0 1 22rem;
}
.auto-editor__channel {
@apply flex items-center gap-3;
}
.auto-editor__hour-select {
min-width: 7rem;
flex: 1;
}
.auto-editor__hour-select :deep(.ant-select-selector) {
display: flex !important;
align-items: center !important;
height: 32px !important;
}
.auto-editor__hour-select :deep(.ant-select-selection-search),
.auto-editor__hour-select :deep(.ant-select-selection-item),
.auto-editor__hour-select :deep(.ant-select-selection-placeholder) {
display: flex !important;
align-items: center !important;
line-height: 30px !important;
margin-top: 0 !important;
margin-bottom: 0 !important;
}
.auto-editor__hour-select :deep(.ant-select-selection-item > div) {
line-height: 30px;
white-space: nowrap;
padding: 0;
}
.auto-editor__hint {
@apply mt-3 text-xs;
color: var(--admin-text-secondary);
}
</style>
@@ -0,0 +1,121 @@
<template>
<div class="h-full min-h-0 min-w-0 max-w-full overflow-hidden">
<BasicShowContext title="اتوماسیون">
<template #extra>
<BaseButton type="primary" size="small" icon="vuesax-linear:add" @click="emits('add')">
افزودن
</BaseButton>
</template>
<div v-if="isMobile" class="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto p-1">
<div
v-for="item in mobileItems"
:key="item.id"
class="flex flex-col gap-2 rounded-xl border border-solid border-gray-100 bg-white p-3 dark:border-slate-700 dark:bg-slate-800/40"
>
<div class="flex items-start justify-between gap-2">
<div class="min-w-0">
<div class="truncate font-bold text-slate-800 dark:text-slate-100">{{ item.name }}</div>
<div class="text-xs text-gray-500">{{ item.code }}</div>
</div>
<a-switch :checked="item.is_active" size="small" @change="emits('toggle', item)" />
</div>
<div class="line-clamp-2 text-sm text-slate-600 dark:text-slate-300">{{ item.body }}</div>
<div class="flex items-center justify-between text-xs text-gray-500">
<span>ارسال {{ Number(item.sent_count ?? 0) }} · ناموفق {{ Number(item.failed_count ?? 0) }}</span>
<div class="flex gap-3">
<Icon
icon="vuesax-linear:edit"
color="#4572E9"
class="cursor-pointer"
@click="emits('edit', item)"
/>
<Icon
icon="vuesax-linear:trash"
color="#C53030"
class="cursor-pointer"
@click="emits('delete', item.id)"
/>
</div>
</div>
</div>
<div v-if="!mobileLoading && mobileItems.length === 0" class="py-8 text-center text-gray-500">
اتوماسیونی نیست
</div>
</div>
<BaseTable v-else class="h-full min-h-0 min-w-0" v-bind="tblProps" @register="submit" ref="tbl">
<template #is_active="{ record }">
<a-switch :checked="record.is_active" size="small" @change="emits('toggle', record)" />
</template>
<template #action="{ record }">
<div class="flex justify-center gap-2">
<a-tooltip placement="bottom">
<template #title>ویرایش</template>
<Icon
icon="vuesax-linear:edit"
color="#4572E9"
class="cursor-pointer"
@click="emits('edit', record)"
/>
</a-tooltip>
<a-tooltip placement="bottom">
<template #title>حذف</template>
<Icon
icon="vuesax-linear:trash"
color="#C53030"
class="cursor-pointer"
@click="emits('delete', record.id)"
/>
</a-tooltip>
</div>
</template>
</BaseTable>
</BasicShowContext>
</div>
</template>
<script lang="tsx" setup>
import { onMounted, ref } from 'vue'
import type { TableInput } from '@core/type/baseTable'
import useBaseTable from '@core/components/BaseTableOld/hooks/useBaseTable'
import Icon from '@core/components/BaseIcon/src/Icon.vue'
import { isMobile } from '@core/utils/is'
import { cols } from '../hooks/automationsColumns'
import { getAutomationsTable } from '@/api/automations'
import type { Automation } from '@/model/automations'
import { useMobileTableList } from '@/components/mobile/composables/useMobileTableList'
const [submit, reload] = useBaseTable()
const emits = defineEmits<{
add: []
edit: [record: Automation]
toggle: [record: Automation]
delete: [id: number]
}>()
const tblProps = {
columns: cols,
api: (params: TableInput) => getAutomationsTable(params),
}
const {
loading: mobileLoading,
items: mobileItems,
reload: reloadMobile,
} = useMobileTableList<Automation>({
fetchApi: getAutomationsTable,
})
onMounted(() => {
if (isMobile.value) reloadMobile()
})
function fetch() {
if (isMobile.value) reloadMobile()
else reload()
}
defineExpose({ fetch })
</script>
@@ -0,0 +1,93 @@
import type { BaseTableColumn } from '@core/type/baseTable'
const triggerLabel: Record<string, string> = {
schedule: 'زمان‌دار',
event: 'رویداد',
price: 'حرکت قیمت',
}
function channelText(record: any): string {
const ch = Array.isArray(record.channels) ? record.channels : []
const parts: string[] = []
if (ch.includes('sms')) parts.push('پیامک')
if (ch.includes('in_app')) parts.push('اعلان')
return parts.join(' / ') || '—'
}
function previewBody(text: unknown): string {
const s = String(text || '').replace(/\s+/g, ' ').trim()
if (!s) return '—'
return s.length > 48 ? `${s.slice(0, 48)}…` : s
}
export const cols: BaseTableColumn[] = [
{
title: 'ردیف',
dataIndex: 'id',
key: 'id',
customRender: (record: any) => record.index + 1,
},
{
title: 'نام',
dataIndex: 'name',
key: 'name',
type: 'Text',
},
{
title: 'کد',
dataIndex: 'code',
key: 'code',
},
{
title: 'عنوان',
dataIndex: 'title',
key: 'title',
type: 'Text',
},
{
title: 'متن',
dataIndex: 'body',
key: 'body',
customRender: ({ record }) => previewBody(record.body),
},
{
title: 'نوع',
dataIndex: 'trigger_type',
key: 'trigger_type',
customRender: ({ record }) => triggerLabel[record.trigger_type] || record.trigger_type,
},
{
title: 'کانال',
dataIndex: 'channels',
key: 'channels',
customRender: ({ record }) => channelText(record),
},
{
title: 'فعال',
dataIndex: 'is_active',
key: 'is_active',
slot: 'is_active',
width: 90,
},
{
title: 'ارسال شده',
dataIndex: 'sent_count',
key: 'sent_count',
customRender: ({ record }) => Number(record.sent_count ?? 0),
},
{
title: 'ناموفق',
dataIndex: 'failed_count',
key: 'failed_count',
customRender: ({ record }) => Number(record.failed_count ?? 0),
},
{
title: 'عملیات',
dataIndex: 'action',
key: 'action',
slot: 'action',
fixed: 'right',
filterable: false,
width: 120,
},
]
+132
View File
@@ -0,0 +1,132 @@
<template>
<div class="flex h-full w-full">
<tbl
ref="tableRef"
class="h-full w-full"
@add="openAdd"
@edit="openEdit"
@toggle="toggleActive"
@delete="deleteItem"
/>
<BasicActionSheet
v-model:visible="editorVisible"
:title="sheetTitle"
modal-width="80rem"
drawer-height="80vh"
:loading="loading"
:show-button="true"
:show-cancel="true"
:show-ok="true"
ok-text="ذخیره"
cancel-text="انصراف"
@ok="submitForm"
@close="closeEditor"
@cancel="closeEditor"
>
<div v-if="isMobile" class="mb-3 text-base font-bold">{{ sheetTitle }}</div>
<AutomationEditorFormBody ref="editorRef" :form="form" />
</BasicActionSheet>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { message } from 'ant-design-vue'
import { cloneDeep } from 'lodash'
import tbl from './components/table.vue'
import AutomationEditorFormBody from './components/AutomationEditorFormBody.vue'
import { emptyAutomation, type Automation } from '@/model/automations'
import {
createAutomation,
deleteAutomation,
toggleAutomationActive,
updateAutomation,
} from '@/api/automations'
import { confirmDeleteItem } from '@/utils/confirmModal'
import { isMobile } from '@core/utils/is'
const tableRef = ref<null | { fetch: () => void }>(null)
const editorRef = ref<{
validateFields?: () => Promise<unknown>
resetFields?: () => void
}>()
const loading = ref(false)
const editorVisible = ref(false)
const editId = ref<number | undefined>()
const form = ref<Automation>(emptyAutomation())
const sheetTitle = computed(() => (editId.value ? 'ویرایش اتوماسیون' : 'افزودن اتوماسیون'))
function refreshTable() {
tableRef.value?.fetch()
}
function closeEditor() {
editorVisible.value = false
}
function openAdd() {
editId.value = undefined
form.value = emptyAutomation()
editorRef.value?.resetFields?.()
editorVisible.value = true
}
function openEdit(record: Automation) {
editId.value = record.id
const conditions = record.conditions || []
form.value = cloneDeep({
...emptyAutomation(),
...record,
audience_mode: conditions.length ? 'filter' : 'all',
trigger_config: {
delay_hours: 0,
every_days: 1,
direction: 'both',
price_window: 'calendar_day',
...(record.trigger_config || {}),
},
conditions,
channels: record.channels || [],
})
editorVisible.value = true
}
async function submitForm() {
try {
await editorRef.value?.validateFields?.()
loading.value = true
const payload = cloneDeep(form.value)
if (editId.value) {
await updateAutomation(editId.value, payload)
} else {
await createAutomation(payload)
}
message.success('اتوماسیون ذخیره شد')
editorVisible.value = false
refreshTable()
} catch (e) {
console.error(e)
} finally {
loading.value = false
}
}
async function toggleActive(record: Automation) {
if (!record.id) return
try {
await toggleAutomationActive(record.id)
message.success(record.is_active ? 'غیرفعال شد' : 'فعال شد')
refreshTable()
} catch (e) {
console.error(e)
message.error('تغییر وضعیت انجام نشد')
}
}
function deleteItem(id: number) {
confirmDeleteItem('اتوماسیون', id, deleteAutomation, refreshTable)
}
</script>