2026-08-29 21:06:30 +03:30
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { onMounted, ref } from 'vue';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import { api } from '@/lib/api';
|
|
|
|
|
import { formatMoney } from '@/lib/money';
|
2026-09-05 02:13:39 +03:30
|
|
|
import MoneyInput from '@/components/MoneyInput.vue';
|
2026-08-29 21:06:30 +03:30
|
|
|
import type {
|
|
|
|
|
CreateProductRequest,
|
|
|
|
|
OrganizationDto,
|
|
|
|
|
ProductDto,
|
|
|
|
|
} from '@mento/shared';
|
|
|
|
|
|
|
|
|
|
const loading = ref(false);
|
|
|
|
|
const saving = ref(false);
|
|
|
|
|
const error = ref('');
|
|
|
|
|
const products = ref<ProductDto[]>([]);
|
|
|
|
|
const enabledCurrencies = ref<string[]>(['IRR']);
|
|
|
|
|
|
|
|
|
|
const name = ref('');
|
|
|
|
|
const sku = ref('');
|
|
|
|
|
const unitPrice = ref('');
|
|
|
|
|
const currency = ref('IRR');
|
|
|
|
|
const description = ref('');
|
|
|
|
|
|
|
|
|
|
async function load() {
|
|
|
|
|
loading.value = true;
|
|
|
|
|
error.value = '';
|
|
|
|
|
try {
|
|
|
|
|
const [productsRes, orgRes] = await Promise.all([
|
|
|
|
|
api.get<ProductDto[]>('/products'),
|
|
|
|
|
api.get<OrganizationDto>('/organizations/current'),
|
|
|
|
|
]);
|
|
|
|
|
products.value = productsRes.data;
|
|
|
|
|
enabledCurrencies.value = orgRes.data.enabledCurrencies?.length
|
|
|
|
|
? orgRes.data.enabledCurrencies
|
|
|
|
|
: [orgRes.data.baseCurrency || 'IRR'];
|
|
|
|
|
currency.value = orgRes.data.baseCurrency || 'IRR';
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (axios.isAxiosError(e) && !e.response) {
|
|
|
|
|
error.value = 'اتصال به API برقرار نشد.';
|
|
|
|
|
} else {
|
|
|
|
|
error.value = 'بارگذاری محصولات ناموفق بود.';
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
loading.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createProduct() {
|
|
|
|
|
if (!name.value.trim()) {
|
|
|
|
|
error.value = 'نام محصول را وارد کنید.';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
saving.value = true;
|
|
|
|
|
error.value = '';
|
|
|
|
|
try {
|
|
|
|
|
const payload: CreateProductRequest = {
|
|
|
|
|
name: name.value.trim(),
|
|
|
|
|
sku: sku.value || undefined,
|
|
|
|
|
description: description.value || undefined,
|
|
|
|
|
unitPrice: unitPrice.value === '' ? null : Number(unitPrice.value),
|
|
|
|
|
currency: currency.value || 'IRR',
|
|
|
|
|
};
|
|
|
|
|
await api.post('/products', payload);
|
|
|
|
|
name.value = '';
|
|
|
|
|
sku.value = '';
|
|
|
|
|
unitPrice.value = '';
|
|
|
|
|
description.value = '';
|
|
|
|
|
await load();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (axios.isAxiosError(e)) {
|
|
|
|
|
const msg = (e.response?.data as { message?: string | string[] })?.message;
|
|
|
|
|
error.value = Array.isArray(msg)
|
|
|
|
|
? msg.join('، ')
|
|
|
|
|
: (msg ?? 'ثبت محصول ناموفق بود');
|
|
|
|
|
} else {
|
|
|
|
|
error.value = 'ثبت محصول ناموفق بود';
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
saving.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function toggleActive(product: ProductDto) {
|
|
|
|
|
await api.patch(`/products/${product.id}`, { isActive: !product.isActive });
|
|
|
|
|
await load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function removeProduct(product: ProductDto) {
|
|
|
|
|
if (!confirm(`محصول «${product.name}» حذف شود؟`)) return;
|
|
|
|
|
await api.delete(`/products/${product.id}`);
|
|
|
|
|
await load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatPrice(value: number | null, curr: string) {
|
|
|
|
|
if (value == null) return '—';
|
|
|
|
|
return formatMoney(value, curr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMounted(load);
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<section class="page">
|
|
|
|
|
<header class="page-head">
|
2026-09-05 02:29:41 +03:30
|
|
|
<div class="head-copy">
|
2026-08-29 21:06:30 +03:30
|
|
|
<h1>محصولات و خدمات</h1>
|
2026-09-05 02:29:41 +03:30
|
|
|
<p class="lead">کاتالوگ محصول برای افزودن به معاملات.</p>
|
2026-08-29 21:06:30 +03:30
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
<form class="panel" @submit.prevent="createProduct">
|
2026-08-29 21:06:30 +03:30
|
|
|
<h2>محصول جدید</h2>
|
|
|
|
|
<div class="grid">
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="p-name">نام</label>
|
|
|
|
|
<input id="p-name" v-model="name" required placeholder="مثلاً لایسنس سازمانی" />
|
|
|
|
|
</div>
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="p-sku">کد / SKU</label>
|
|
|
|
|
<input id="p-sku" v-model="sku" placeholder="اختیاری" />
|
|
|
|
|
</div>
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="p-price">قیمت واحد</label>
|
2026-09-05 02:13:39 +03:30
|
|
|
<MoneyInput id="p-price" v-model="unitPrice" :decimals="0" />
|
2026-08-29 21:06:30 +03:30
|
|
|
</div>
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="p-currency">ارز</label>
|
|
|
|
|
<select id="p-currency" v-model="currency">
|
|
|
|
|
<option v-for="code in enabledCurrencies" :key="code" :value="code">
|
|
|
|
|
{{ code }}
|
|
|
|
|
</option>
|
|
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="field wide">
|
|
|
|
|
<label for="p-desc">توضیح</label>
|
|
|
|
|
<input id="p-desc" v-model="description" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-09-05 02:29:41 +03:30
|
|
|
<button class="btn primary" type="submit" :disabled="saving">
|
2026-08-29 21:06:30 +03:30
|
|
|
{{ saving ? 'در حال ثبت…' : 'افزودن محصول' }}
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
|
|
|
|
|
<p v-if="error" class="error">{{ error }}</p>
|
|
|
|
|
<p v-if="loading" class="muted">در حال بارگذاری…</p>
|
|
|
|
|
|
|
|
|
|
<div v-else class="list">
|
|
|
|
|
<article v-for="product in products" :key="product.id" class="row">
|
|
|
|
|
<div>
|
|
|
|
|
<strong>{{ product.name }}</strong>
|
|
|
|
|
<p class="meta">
|
|
|
|
|
{{ formatPrice(product.unitPrice, product.currency) }}
|
|
|
|
|
<span v-if="product.sku"> · {{ product.sku }}</span>
|
|
|
|
|
<span v-if="!product.isActive"> · غیرفعال</span>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="actions">
|
|
|
|
|
<button class="btn btn-ghost" type="button" @click="toggleActive(product)">
|
|
|
|
|
{{ product.isActive ? 'غیرفعال' : 'فعال' }}
|
|
|
|
|
</button>
|
|
|
|
|
<button class="btn btn-danger" type="button" @click="removeProduct(product)">
|
|
|
|
|
حذف
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</article>
|
|
|
|
|
<p v-if="!products.length" class="muted">هنوز محصولی ثبت نشده است.</p>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.page {
|
|
|
|
|
display: grid;
|
2026-09-05 02:29:41 +03:30
|
|
|
gap: 1rem;
|
2026-08-29 21:06:30 +03:30
|
|
|
max-width: 920px;
|
2026-09-05 02:29:41 +03:30
|
|
|
min-width: 0;
|
|
|
|
|
overflow-x: clip;
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.page-head {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
gap: 1rem;
|
|
|
|
|
align-items: flex-start;
|
|
|
|
|
flex-wrap: wrap;
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.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,
|
|
|
|
|
.meta {
|
2026-08-29 21:06:30 +03:30
|
|
|
margin: 0;
|
2026-09-05 02:29:41 +03:30
|
|
|
color: #64748b;
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.error {
|
|
|
|
|
margin: 0;
|
|
|
|
|
color: #b91c1c;
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.panel,
|
2026-08-29 21:06:30 +03:30
|
|
|
.row {
|
2026-09-05 02:29:41 +03:30
|
|
|
background: #fff;
|
|
|
|
|
border: 1px solid #e2e8f0;
|
|
|
|
|
border-radius: 18px;
|
|
|
|
|
box-shadow: 0 8px 22px rgba(15, 23, 42, 0.04);
|
2026-08-29 21:06:30 +03:30
|
|
|
padding: 1.1rem;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.panel {
|
2026-08-29 21:06:30 +03:30
|
|
|
display: grid;
|
|
|
|
|
gap: 0.9rem;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.panel h2 {
|
2026-08-29 21:06:30 +03:30
|
|
|
margin: 0;
|
|
|
|
|
font-size: 1.05rem;
|
2026-09-05 02:29:41 +03:30
|
|
|
font-weight: 700;
|
|
|
|
|
color: #0f172a;
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.grid {
|
|
|
|
|
display: grid;
|
|
|
|
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
|
|
|
gap: 0.85rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.wide {
|
|
|
|
|
grid-column: 1 / -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.field label {
|
|
|
|
|
color: #64748b;
|
|
|
|
|
font-size: 0.9rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input,
|
|
|
|
|
select {
|
|
|
|
|
border: 1px solid #e2e8f0;
|
2026-08-29 21:06:30 +03:30
|
|
|
border-radius: 12px;
|
|
|
|
|
padding: 0.8rem 0.95rem;
|
|
|
|
|
background: #fff;
|
|
|
|
|
font: inherit;
|
|
|
|
|
width: 100%;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 02:29:41 +03:30
|
|
|
.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: 1px solid #99f6e4;
|
|
|
|
|
box-shadow: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.btn-danger {
|
|
|
|
|
background: #fff;
|
|
|
|
|
color: #b91c1c;
|
|
|
|
|
border: 1px solid #fecaca;
|
|
|
|
|
box-shadow: none;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
.list {
|
|
|
|
|
display: grid;
|
|
|
|
|
gap: 0.7rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.row {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
gap: 1rem;
|
|
|
|
|
align-items: center;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.meta {
|
2026-09-05 02:29:41 +03:30
|
|
|
margin-top: 0.25rem;
|
2026-08-29 21:06:30 +03:30
|
|
|
font-size: 0.92rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.actions {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 0.45rem;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@media (max-width: 700px) {
|
|
|
|
|
.grid {
|
|
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.row {
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: stretch;
|
|
|
|
|
}
|
2026-09-05 02:29:41 +03:30
|
|
|
|
|
|
|
|
.actions {
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
}
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
</style>
|