Files
nademitex_user/src/views/home/HomeView.vue
T

729 lines
18 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useCrmStore } from '@/stores/crm'
import { useFavoritesStore } from '@/stores/favorites'
import { fetchAgentProfile, fetchBanners, type AppBannerDto } from '@/services/agentApi'
import IconBell from '@/components/icons/IconBell.vue'
import IconChevronLeft from '@/components/icons/IconChevronLeft.vue'
import IconMedal from '@/components/icons/IconMedal.vue'
import IconHeart from '@/components/icons/IconHeart.vue'
import IconCatalog from '@/components/icons/IconCatalog.vue'
import IconDiamond from '@/components/icons/IconDiamond.vue'
import IconLayoutGrid from '@/components/icons/IconLayoutGrid.vue'
import IconProfile from '@/components/icons/IconProfile.vue'
import IconChevronBack from '@/components/icons/IconChevronBack.vue'
import { formatNumber } from '@/utils/format'
import type { CatalogProductCard } from '@/types/crm'
import type { Component } from 'vue'
import { useGoBack } from '@/composables/useGoBack'
import AuthThemeToggle from '@/components/ui/AuthThemeToggle.vue'
import PageSkeleton from '@/components/ui/PageSkeleton.vue'
import UserAvatar from '@/components/ui/UserAvatar.vue'
const router = useRouter()
const { goBack } = useGoBack('/')
const auth = useAuthStore()
const crm = useCrmStore()
const favorites = useFavoritesStore()
const loading = ref(true)
const error = ref<string | null>(null)
const newest = ref<CatalogProductCard[]>([])
const favError = ref<string | null>(null)
const agentLevel = ref<{
label: string
nextLabel: string
pointsRemaining: number
progressPercent: number
} | null>(null)
const customerActions: Array<{ id: string; label: string; to: string; icon: Component }> = [
{ id: 'qa-club', label: 'باشگاه مشتریان', to: '/profile', icon: IconProfile },
{ id: 'qa-cat', label: 'دسته بندی', to: '/categories', icon: IconLayoutGrid },
{ id: 'qa-new', label: 'محصولات جدید', to: '/products', icon: IconDiamond },
{ id: 'qa-swatch', label: 'درخواست کالیته', to: '/catalog-request', icon: IconCatalog },
]
const agentActions: Array<{ id: string; label: string; to: string; icon: Component }> = [
{ id: 'qa-customers', label: 'مشتریان من', to: '/customers', icon: IconProfile },
{ id: 'qa-reminders', label: 'یادآورها', to: '/reminders', icon: IconBell },
{ id: 'qa-cat', label: 'دسته بندی', to: '/categories', icon: IconLayoutGrid },
{ id: 'qa-swatch', label: 'درخواست کالیته', to: '/catalog-request', icon: IconCatalog },
]
const actions = computed(() => (auth.isAgent ? agentActions : customerActions))
const FALLBACK_BANNER: AppBannerDto = {
id: 'fallback',
slot: 'home_hero',
title: 'کالکشن پاییز و زمستان',
subtitle: 'جدیدترین پارچه‌ها',
ctaLabel: 'مشاهده کالکشن',
ctaLink: '/categories',
imageUrl: '',
}
const banners = ref<AppBannerDto[]>([FALLBACK_BANNER])
const bannerIndex = ref(0)
let bannerTimer: number | undefined
const banner = computed(() => banners.value[bannerIndex.value] ?? FALLBACK_BANNER)
function startBannerRotation() {
stopBannerRotation()
if (banners.value.length < 2) return
bannerTimer = window.setInterval(() => {
bannerIndex.value = (bannerIndex.value + 1) % banners.value.length
}, 5000)
}
function stopBannerRotation() {
if (bannerTimer) window.clearInterval(bannerTimer)
bannerTimer = undefined
}
function setBanner(index: number) {
bannerIndex.value = index
startBannerRotation()
}
const displayFirst = computed(() => auth.user?.firstName || 'کاربر')
const displayLast = computed(() => auth.user?.lastName || '')
const homeLevel = computed(() => {
if (agentLevel.value) return agentLevel.value
const u = auth.user
return {
label: u?.levelLabel || 'برنزی',
nextLabel: u?.nextLevelLabel || 'نقره‌ای',
pointsRemaining: u?.pointsToNext ?? 0,
progressPercent: u?.levelProgress ?? 0,
}
})
onUnmounted(stopBannerRotation)
onMounted(async () => {
loading.value = true
error.value = null
try {
const [products, homeBanners] = await Promise.all([
crm.loadBrowseProducts(),
fetchBanners('home_hero').catch(() => [] as AppBannerDto[]),
favorites.hydrate(),
])
if (homeBanners.length) banners.value = homeBanners
startBannerRotation()
newest.value = products.filter((p) => p.isNew).slice(0, 6)
if (!newest.value.length) newest.value = products.slice(0, 6)
if (auth.isAgent) {
try {
const profile = await fetchAgentProfile()
agentLevel.value = {
label: profile.levelLabel,
nextLabel: profile.nextLevelLabel,
pointsRemaining: profile.pointsToNext,
progressPercent: profile.levelProgress,
}
} catch {
// سطح از /auth/me؛ عدم دیپلوی agent API خانه را خراب نکند
}
}
} catch (e) {
error.value = e instanceof Error ? e.message : 'بارگذاری خانه ناموفق بود.'
} finally {
loading.value = false
}
})
function go(to: string) {
if (/^https?:\/\//i.test(to)) {
window.location.assign(to)
return
}
router.push(to)
}
function goBanner() {
go(banner.value.ctaLink.trim() || '/categories')
}
async function toggleFavorite(productId: string) {
favError.value = null
try {
await favorites.toggle(productId)
} catch (e) {
favError.value = e instanceof Error ? e.message : 'ذخیره علاقه‌مندی ناموفق بود.'
}
}
</script>
<template>
<div class="page home-page">
<header class="home-header">
<button type="button" class="icon-btn" aria-label="بازگشت" @click="goBack">
<IconChevronBack :size="20" />
</button>
<div class="home-header__user">
<UserAvatar
class="home-header__avatar"
:src="auth.user?.avatarUrl"
:initials="displayFirst.charAt(0)"
:size="44"
/>
<div>
<p class="home-header__hello">
سلام، {{ displayFirst }} {{ displayLast }}
</p>
<p class="home-header__welcome">خوش آمدید</p>
</div>
</div>
<div class="home-header__actions">
<AuthThemeToggle variant="inline" />
<button type="button" class="icon-btn" aria-label="اعلان‌ها" @click="go('/profile/notifications')">
<IconBell :size="20" />
</button>
</div>
</header>
<div class="page__scroll page__scroll--with-nav home-scroll">
<p v-if="error" class="home-error">{{ error }}</p>
<PageSkeleton v-else-if="loading" variant="home" />
<template v-else>
<section
class="hero-banner"
:aria-label="banner.title"
:style="banner.imageUrl ? { '--hero-image': `url(\"${banner.imageUrl}\")` } : undefined"
>
<div class="hero-banner__content">
<h2 class="hero-banner__title">
{{ banner.title }}
<span v-if="banner.subtitle">{{ banner.subtitle }}</span>
</h2>
<button
v-if="banner.ctaLabel"
type="button"
class="hero-banner__cta"
@click="goBanner"
>
{{ banner.ctaLabel }}
</button>
</div>
<div v-if="banners.length > 1" class="hero-banner__dots" role="tablist" aria-label="بنرها">
<button
v-for="(item, index) in banners"
:key="item.id"
type="button"
class="dot"
:class="{ 'dot--active': index === bannerIndex }"
:aria-label="item.title"
:aria-selected="index === bannerIndex"
@click="setBanner(index)"
/>
</div>
</section>
<section class="quick-grid" aria-label="دسترسی سریع">
<button
v-for="action in actions"
:key="action.id"
type="button"
class="quick-card"
@click="go(action.to)"
>
<span class="quick-card__icon" aria-hidden="true">
<component :is="action.icon" :size="20" />
</span>
<span class="quick-card__label">{{ action.label }}</span>
</button>
</section>
<section class="level-card" aria-label="سطح عضویت">
<div class="level-card__top">
<div class="level-card__identity">
<IconMedal :size="48" />
<div class="level-card__text">
<span class="level-card__eyebrow">سطح شما</span>
<strong class="level-card__name">{{ homeLevel.label }}</strong>
<span class="level-card__next">تا سطح {{ homeLevel.nextLabel }}</span>
</div>
</div>
<button type="button" class="level-card__details" @click="go('/profile')">
جزئیات سطح
</button>
</div>
<div class="level-card__progress">
<div
class="level-card__bar"
role="progressbar"
:aria-valuenow="homeLevel.progressPercent"
aria-valuemin="0"
aria-valuemax="100"
>
<span
class="level-card__bar-fill"
:style="{ width: `${homeLevel.progressPercent}%` }"
/>
</div>
<span class="level-card__points">
{{ formatNumber(homeLevel.pointsRemaining) }} امتیاز دیگر
</span>
</div>
</section>
<section class="section">
<div class="section__head">
<h3 class="section__title">جدیدترین محصولات</h3>
<button type="button" class="section__link" @click="go('/products')">
مشاهده همه
<IconChevronLeft :size="14" />
</button>
</div>
<div class="product-row">
<article
v-for="(item, index) in newest"
:key="item.id"
class="product-card"
@click="go(`/product?id=${item.id}`)"
>
<div
class="product-card__image"
:class="`product-card__image--${index % 3}`"
:style="item.imageUrl ? { backgroundImage: `url(${item.imageUrl})` } : undefined"
>
<span v-if="item.isNew" class="product-card__badge">جدید</span>
</div>
<div class="product-card__body">
<strong class="product-card__name">{{ item.name }}</strong>
<span class="product-card__sub">{{ item.subtitle }}</span>
<button
type="button"
class="product-card__fav"
:class="{ 'product-card__fav--on': favorites.isFavorite(item.id) }"
:aria-label="
favorites.isFavorite(item.id)
? 'حذف از علاقه‌مندی‌ها'
: 'افزودن به علاقه‌مندی‌ها'
"
:aria-pressed="favorites.isFavorite(item.id)"
@click.stop="toggleFavorite(item.id)"
>
<IconHeart
:size="16"
:filled="favorites.isFavorite(item.id)"
:color="favorites.isFavorite(item.id) ? '#0a353c' : '#b0b7c0'"
/>
</button>
</div>
</article>
</div>
</section>
</template>
</div>
</div>
</template>
<style scoped>
.home-page {
background: var(--color-bg);
}
.home-error {
margin: 0;
color: var(--color-text-muted);
font-size: 0.85rem;
}
.home-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: 12px var(--page-padding) 8px;
background: var(--color-surface);
color: var(--color-text);
}
.home-header__user {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
flex: 1;
}
.home-header__avatar {
background: var(--color-primary-soft);
color: var(--color-primary);
}
.home-header__hello {
margin: 0;
font-size: 0.95rem;
font-weight: 700;
}
.home-header__welcome {
margin: 2px 0 0;
font-size: 0.75rem;
color: var(--color-text-muted);
}
.home-header__actions {
display: flex;
align-items: center;
gap: 0;
color: inherit;
}
.icon-btn {
position: relative;
width: 40px;
height: 40px;
border: none;
border-radius: 12px;
background: transparent;
color: var(--color-text);
display: grid;
place-items: center;
cursor: pointer;
}
.home-scroll {
display: flex;
flex-direction: column;
gap: 16px;
padding: 12px var(--page-padding) 8px;
}
.home-scroll.page__scroll--with-nav {
padding-bottom: 8px;
}
.home-scroll > .section:last-child {
margin-top: auto;
}
.hero-banner {
--hero-image: #8a7a65;
position: relative;
min-height: 160px;
border-radius: var(--radius-lg);
background:
linear-gradient(120deg, rgba(var(--color-accent-navy-rgb), 0.72), rgba(var(--color-primary-rgb), 0.45)),
var(--hero-image);
background-size: cover;
background-position: center;
color: #fff;
padding: 20px;
overflow: hidden;
}
.hero-banner__title {
margin: 0 0 14px;
font-size: 1.05rem;
line-height: 1.7;
max-width: 16rem;
font-weight: 700;
}
.hero-banner__title span {
display: block;
font-weight: 500;
opacity: 0.95;
}
.hero-banner__cta {
border: none;
border-radius: 999px;
background: var(--color-accent-navy);
color: #fff;
padding: 10px 16px;
font-weight: 600;
cursor: pointer;
}
.hero-banner__dots {
position: absolute;
bottom: 12px;
inset-inline-start: 50%;
transform: translateX(-50%);
display: flex;
gap: 6px;
}
.dot {
width: 6px;
height: 6px;
border: none;
padding: 0;
border-radius: 999px;
background: rgba(255, 255, 255, 0.45);
cursor: pointer;
}
.dot--active {
width: 16px;
background: #fff;
}
.quick-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.quick-card {
border: none;
background: transparent;
border-radius: var(--radius-md);
padding: 12px 6px;
display: flex;
flex-direction: column;
align-items: center;
gap: 8px;
cursor: pointer;
box-shadow: none;
}
.quick-card__icon {
width: 40px;
height: 40px;
border-radius: 12px;
background: var(--color-primary-soft);
display: grid;
place-items: center;
color: var(--color-primary);
}
.quick-card__label {
font-size: 0.7rem;
font-weight: 600;
text-align: center;
line-height: 1.4;
}
.level-card {
background: var(--color-surface);
border-radius: 18px;
padding: 16px;
box-shadow: 0 2px 12px rgba(16, 24, 40, 0.06);
border: 1px solid rgba(230, 232, 230, 0.9);
}
.level-card__top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.level-card__identity {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.level-card__text {
display: flex;
flex-direction: column;
gap: 2px;
min-width: 0;
}
.level-card__eyebrow,
.level-card__next {
font-size: 0.75rem;
line-height: 1.4;
color: var(--color-text-muted);
font-weight: 500;
}
.level-card__name {
font-size: 1.15rem;
line-height: 1.35;
color: var(--color-accent-navy);
font-weight: 800;
}
.level-card__details {
flex-shrink: 0;
border: none;
border-radius: 999px;
background: #eef1f4;
color: var(--color-text-muted);
font-size: 0.75rem;
font-weight: 600;
padding: 8px 12px;
cursor: pointer;
white-space: nowrap;
}
.level-card__progress {
margin-top: 14px;
display: flex;
align-items: center;
gap: 10px;
}
.level-card__bar {
flex: 1;
min-width: 0;
height: 8px;
border-radius: 999px;
background: #e8ecf0;
overflow: hidden;
}
.level-card__bar-fill {
display: block;
height: 100%;
border-radius: 999px;
background: var(--color-accent-navy);
}
.level-card__points {
flex-shrink: 0;
font-size: 0.72rem;
color: var(--color-text-muted);
white-space: nowrap;
}
.section__head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.section__title {
margin: 0;
font-size: 1rem;
}
.section__link {
border: none;
background: transparent;
color: var(--color-info);
display: inline-flex;
align-items: center;
gap: 2px;
font-size: 0.8rem;
cursor: pointer;
}
.product-row {
display: flex;
gap: 12px;
overflow-x: auto;
padding-bottom: 0;
margin-inline: calc(-1 * var(--page-padding));
padding-inline: var(--page-padding);
scroll-snap-type: x mandatory;
-webkit-overflow-scrolling: touch;
}
.product-card {
flex: 0 0 148px;
display: flex;
flex-direction: column;
border: none;
background: var(--color-surface);
border-radius: 16px;
padding: 0;
text-align: start;
cursor: pointer;
overflow: hidden;
box-shadow: 0 2px 10px rgba(16, 24, 40, 0.05);
scroll-snap-align: start;
}
.product-card__image {
position: relative;
height: 128px;
background:
linear-gradient(160deg, rgba(255, 255, 255, 0.12), rgba(0, 0, 0, 0.18)),
linear-gradient(145deg, #c9b8a0 0%, #8f7a62 55%, #6d5a48 100%);
background-size: cover;
background-position: center;
}
.product-card__image--1 {
background:
linear-gradient(160deg, rgba(255, 255, 255, 0.1), rgba(0, 0, 0, 0.22)),
linear-gradient(145deg, #9aa4b2 0%, #5d6b7c 55%, #3f4a57 100%);
}
.product-card__image--2 {
background:
linear-gradient(160deg, rgba(255, 255, 255, 0.12), rgba(0, 0, 0, 0.2)),
linear-gradient(145deg, #d7c4a8 0%, #b08968 50%, #7a5a3c 100%);
}
.product-card__badge {
position: absolute;
top: 10px;
inset-inline-end: 10px;
background: var(--color-accent-navy);
color: #fff;
font-size: 0.68rem;
font-weight: 700;
line-height: 1;
padding: 5px 9px;
border-radius: 999px;
}
.product-card__body {
position: relative;
display: flex;
flex-direction: column;
align-items: stretch;
gap: 4px;
padding: 10px 12px 12px;
padding-inline-end: 34px;
min-height: 56px;
}
.product-card__name {
display: block;
width: 100%;
margin: 0;
font-size: 13px;
font-weight: 700;
line-height: 1.4;
color: #1a1a1a;
text-align: start;
}
.product-card__sub {
display: block;
width: 100%;
margin: 0;
font-size: 11px;
font-weight: 400;
line-height: 1.4;
color: #8b939e;
text-align: start;
}
.product-card__fav {
position: absolute;
bottom: 10px;
inset-inline-end: 8px;
width: 22px;
height: 22px;
border: none;
background: transparent;
padding: 0;
display: grid;
place-items: center;
cursor: pointer;
color: #b0b7c0;
}
.product-card__fav--on {
color: var(--color-primary);
}
</style>