Files
tehran_tala/apps/admin-panel/src/views/promotion/autopromotion.vue
T

79 lines
2.6 KiB
Vue

<template>
<!--
Ported from legacy henza_admin/src/views/promotion/autopromotion.vue —
the one-shot welcome discount for new customers. State toggle + percentage.
-->
<a-form layout="vertical" class="page-card" @submit.prevent="handleSubmit">
<a-spin :spinning="loading">
<a-card title="تخفیف اتوماتیک یکبار مصرف ویژه مشتریان جدید" class="mb-base">
<div class="grid-3">
<a-form-item label="وضعیت تخفیف اتوماتیک">
<a-switch v-model:checked="state" size="default" />
</a-form-item>
<a-form-item
label="درصد تخفیف اتوماتیک روی محصولات"
:validate-status="err.discount_percent ? 'error' : ''"
:help="err.discount_percent"
>
<a-input-number
v-model:value="form.discount_percent"
dir="ltr"
:min="0"
:max="100"
:formatter="(v: number | string) => `${v}%`"
:parser="(v: string) => v.replace('%', '')"
style="width: 100%"
/>
</a-form-item>
</div>
</a-card>
</a-spin>
<a-button type="primary" class="mr-4 form-submit" @click="handleSubmit">ثبت اطلاعات</a-button>
</a-form>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { message } from 'ant-design-vue';
import api from '../../api/client';
const loading = ref(false);
const state = ref(false);
const form = reactive<Record<string, any>>({ discount_percent: undefined });
const err = reactive<Record<string, string>>({});
async function handleSubmit() {
Object.keys(err).forEach((k) => delete err[k]);
if (form.discount_percent === undefined || form.discount_percent === null) {
err.discount_percent = 'وارد کردن درصد تخفیف اتوماتیک الزامی است';
return;
}
try {
loading.value = true;
await api.post('autopromotion', {
state: state.value,
discount_percent: form.discount_percent,
});
message.success('اطلاعات با موفقیت به ثبت رسید.');
} catch (e: any) {
message.error(e?.response?.data?.message ?? 'خطا در ثبت اطلاعات.');
} finally {
loading.value = false;
}
}
onMounted(async () => {
try {
const { data } = await api.get('autopromotion');
state.value = Boolean(data.state);
form.discount_percent = data.discount_percent ?? undefined;
} catch (e) {
console.log(e);
}
});
</script>
<!-- .grid-3 now comes from the global utilities.less (dedup) -->