Initial reforms

This commit is contained in:
2026-09-19 17:23:11 +03:30
parent ab5ce96529
commit 8be9280721
57 changed files with 476 additions and 67 deletions
+117 -23
View File
@@ -8,6 +8,7 @@ import ActiveSwitchCell from '@/components/ui/ActiveSwitchCell.vue'
import AdminIcon from '@/components/icons/AdminIcon.vue'
import AdminSelect from '@/components/ui/AdminSelect.vue'
import { brandsApi, customersApi, levelsApi, type Brand, type CustomerLevel } from '@/services/api'
import CustomerSuggestionsPanel from './CustomerSuggestionsPanel.vue'
import { ApiError } from '@/services/http'
import { useToast } from '@/composables/useToast'
import { CUSTOMER_TYPE_LABELS } from '@/utils/adminLabels'
@@ -19,6 +20,7 @@ const levels = ref<CustomerLevel[]>([])
const agents = ref<Customer[]>([])
const brands = ref<Brand[]>([])
const searchQuery = ref('')
const roleTab = ref<'customer' | 'agent'>('customer')
const loading = ref(true)
const modalMode = ref<'add' | 'edit' | null>(null)
const editingId = ref<string | null>(null)
@@ -49,6 +51,15 @@ const appRoleOptions = [
{ value: 'agent', label: 'واسطه' },
]
const APP_ROLE_LABELS: Record<'customer' | 'agent', string> = {
customer: 'مشتری',
agent: 'واسطه',
}
function appRoleLabel(role?: 'customer' | 'agent' | null) {
return APP_ROLE_LABELS[role === 'agent' ? 'agent' : 'customer']
}
const levelOptions = computed(() => [
{ value: '', label: 'بدون سطح' },
...levels.value.map((level) => ({ value: level.id, label: level.label })),
@@ -109,14 +120,27 @@ function toggleBrand(brandId: string, checked: boolean) {
async function loadCustomers() {
loading.value = true
try {
customerList.value = await customersApi.list()
customerList.value = await customersApi.list(undefined, roleTab.value)
} catch (err) {
toast.error(err instanceof ApiError ? err.message : 'بارگذاری مشتریان انجام نشد.')
toast.error(
err instanceof ApiError
? err.message
: roleTab.value === 'agent'
? 'بارگذاری واسطه‌ها انجام نشد.'
: 'بارگذاری مشتریان انجام نشد.',
)
} finally {
loading.value = false
}
}
function setRoleTab(tab: 'customer' | 'agent') {
if (roleTab.value === tab) return
roleTab.value = tab
searchQuery.value = ''
void loadCustomers()
}
async function loadLookups() {
try {
const [levelsData, agentsData, brandsData] = await Promise.all([
@@ -141,6 +165,7 @@ function openAddModal() {
modalMode.value = 'add'
editingId.value = null
Object.assign(form, emptyForm())
form.appRole = roleTab.value
formError.value = ''
}
@@ -219,10 +244,33 @@ function clearSearch() {
<div class="customers-page">
<div class="admin-page-header">
<div>
<h1 class="admin-page-header__title">مشتریان</h1>
<h1 class="admin-page-header__title">مشتریان و واسطه‌ها</h1>
</div>
<button type="button" class="admin-btn admin-btn--primary" @click="openAddModal">
افزودن مشتری
{{ roleTab === 'agent' ? 'افزودن واسطه' : 'افزودن مشتری' }}
</button>
</div>
<div class="customers-page__tabs" role="tablist" aria-label="نوع کاربر">
<button
type="button"
role="tab"
class="customers-page__tab"
:class="{ 'is-active': roleTab === 'customer' }"
:aria-selected="roleTab === 'customer'"
@click="setRoleTab('customer')"
>
مشتریان
</button>
<button
type="button"
role="tab"
class="customers-page__tab"
:class="{ 'is-active': roleTab === 'agent' }"
:aria-selected="roleTab === 'agent'"
@click="setRoleTab('agent')"
>
واسطه‌ها
</button>
</div>
@@ -241,18 +289,26 @@ function clearSearch() {
v-else-if="!filteredCustomers.length"
class="admin-card"
icon="customers"
:title="searchQuery.trim() ? 'نتیجه‌ای یافت نشد' : 'مشتریان ثبت شده'"
:title="
searchQuery.trim()
? 'نتیجه‌ای یافت نشد'
: roleTab === 'agent'
? 'واسطه‌ای ثبت نشده'
: 'مشتریان ثبت شده'
"
:description="
searchQuery.trim()
? 'عبارت جستجو را تغییر دهید یا فیلترها را پاک کنید.'
: 'برای شروع، اولین مشتری را ثبت کنید.'
: roleTab === 'agent'
? 'برای شروع، اولین واسطه را ثبت کنید.'
: 'برای شروع، اولین مشتری را ثبت کنید.'
"
>
<button v-if="searchQuery.trim()" type="button" class="admin-btn admin-btn--outline admin-btn--sm" @click="clearSearch">
پاک کردن جستجو
</button>
<button v-else type="button" class="admin-btn admin-btn--primary admin-btn--sm" @click="openAddModal">
افزودن مشتری
{{ roleTab === 'agent' ? 'افزودن واسطه' : 'افزودن مشتری' }}
</button>
</BaseEmptyState>
@@ -261,13 +317,14 @@ function clearSearch() {
<table class="admin-table">
<thead>
<tr>
<th>نام مشتری</th>
<th>{{ roleTab === 'agent' ? 'نام واسطه' : 'نام مشتری' }}</th>
<th>تلفن</th>
<th>شناسه ملی</th>
<th>نوع</th>
<th>سطح</th>
<th>واسطه</th>
<th>شرکت</th>
<th>نقش</th>
<th v-if="roleTab === 'agent'">کد واسطه</th>
<th v-if="roleTab === 'customer'">نوع</th>
<th v-if="roleTab === 'customer'">سطح</th>
<th v-if="roleTab === 'customer'">واسطه</th>
<th v-if="roleTab === 'customer'">شرکت</th>
<th class="numeric">تعداد سفارش</th>
<th class="numeric">مجموع متراژ</th>
<th class="status">وضعیت</th>
@@ -279,11 +336,12 @@ function clearSearch() {
<tr v-for="customer in filteredCustomers" :key="customer.id">
<td>{{ displayCell(customer.name) }}</td>
<td>{{ displayCell(customer.phone) }}</td>
<td>{{ displayCell(customer.nationalId) }}</td>
<td>{{ displayCell(CUSTOMER_TYPE_LABELS[customer.customerType]) }}</td>
<td>{{ displayCell(customer.levelLabel) }}</td>
<td>{{ displayCell(customer.assignedAgentName) }}</td>
<td>{{ displayCell(customer.companyName) }}</td>
<td>{{ appRoleLabel(customer.appRole) }}</td>
<td v-if="roleTab === 'agent'">{{ displayCell(customer.agentCode) }}</td>
<td v-if="roleTab === 'customer'">{{ displayCell(CUSTOMER_TYPE_LABELS[customer.customerType]) }}</td>
<td v-if="roleTab === 'customer'">{{ displayCell(customer.levelLabel) }}</td>
<td v-if="roleTab === 'customer'">{{ displayCell(customer.assignedAgentName) }}</td>
<td v-if="roleTab === 'customer'">{{ displayCell(customer.companyName) }}</td>
<td class="numeric">{{ displayCell(customer.orderCount) }}</td>
<td class="numeric">{{ displayMeterage(customer.totalMeterage) }}</td>
<td class="status">
@@ -326,10 +384,11 @@ function clearSearch() {
</div>
<div class="admin-card-item__meta">
<span>{{ displayCell(customer.phone) }}</span>
<span>{{ displayCell(CUSTOMER_TYPE_LABELS[customer.customerType]) }}</span>
<span>{{ displayCell(customer.levelLabel) }}</span>
<span>{{ displayCell(customer.assignedAgentName) }}</span>
<span>{{ displayCell(customer.companyName) }}</span>
<span v-if="roleTab === 'agent'">کد: {{ displayCell(customer.agentCode) }}</span>
<span v-if="roleTab === 'customer'">{{ displayCell(CUSTOMER_TYPE_LABELS[customer.customerType]) }}</span>
<span v-if="roleTab === 'customer'">{{ displayCell(customer.levelLabel) }}</span>
<span v-if="roleTab === 'customer'">واسطه: {{ displayCell(customer.assignedAgentName) }}</span>
<span v-if="roleTab === 'customer'">{{ displayCell(customer.companyName) }}</span>
<span>
{{ displayCell(customer.orderCount) }} سفارش · {{ displayMeterage(customer.totalMeterage) }}
</span>
@@ -441,13 +500,24 @@ function clearSearch() {
<div class="admin-form__field">
<BaseSwitch v-model="form.isActive" label="وضعیت" />
</div>
<CustomerSuggestionsPanel
v-if="modalMode === 'edit' && editingId && form.appRole === 'customer'"
:customer-id="editingId"
/>
</form>
<template #footer>
<button type="button" class="admin-btn admin-btn--outline" @click="closeModal">انصراف</button>
<button type="button" class="admin-btn admin-btn--primary" :disabled="!isFormValid || saving" @click="submitCustomer">
<span v-if="saving" class="admin-btn__spinner" aria-hidden="true" />
{{ modalMode === 'edit' ? 'ذخیره تغییرات' : 'ثبت مشتری' }}
{{
modalMode === 'edit'
? 'ذخیره تغییرات'
: form.appRole === 'agent'
? 'ثبت واسطه'
: 'ثبت مشتری'
}}
</button>
</template>
</BaseModal>
@@ -455,6 +525,30 @@ function clearSearch() {
</template>
<style scoped>
.customers-page__tabs {
display: flex;
gap: 8px;
margin-bottom: 16px;
}
.customers-page__tab {
border: 1px solid var(--color-border);
border-radius: 999px;
background: var(--color-surface);
color: var(--color-text-muted);
padding: 8px 16px;
font: inherit;
font-size: 0.875rem;
font-weight: 600;
cursor: pointer;
}
.customers-page__tab.is-active {
border-color: var(--color-primary);
background: var(--color-primary-soft);
color: var(--color-primary);
}
.customers-page__search {
display: flex;
align-items: center;