import { computed, inject, onMounted, provide, ref, type InjectionKey } from 'vue' import { loading } from '@/utils' import { guideTypePassword, isValidPersian, passwordRules, requireNationalCode, } from '@/utils/inputRules' import type { Password, User } from '@/models/login' import router from '@/router' import type { FormInstance } from 'ant-design-vue' import { message } from 'ant-design-vue' import { updateMe, updatePass } from '@/api/me' import { useUserStore } from '@/store/userStore' import { useTrackingSystemsFeature } from '@/composables/useTrackingSystemsFeature' export interface UserMenuAction { id: string label: string icon: string onClick: () => void visible?: boolean } export interface UserPageContext { userStore: ReturnType userFormState: ReturnType> userPassForm: ReturnType> formRef: ReturnType> formPassRef: ReturnType> editPassVisible: ReturnType> editPasswordVisible: ReturnType> isLegal: ReturnType> shareButtonLabel: ReturnType> menuActions: ReturnType> loading: typeof loading guideTypePassword: typeof guideTypePassword isValidPersian: typeof isValidPersian requireNationalCode: typeof requireNationalCode passwordRules: typeof passwordRules copyCode: () => void shareCode: () => void ok: () => void submitPassForm: () => void } export const USER_PAGE_CONTEXT_KEY: InjectionKey = Symbol('userPageContext') export function useUserPage() { const userFormState = ref({ fname: '', lname: '', mobile: undefined, birth_date: '', city: '', n_code: undefined, }) const userPassForm = ref({ password: '' }) const formRef = ref() const formPassRef = ref() const userStore = useUserStore() const editPassVisible = ref(false) const editPasswordVisible = ref(false) const { trackingSystemsEnabled } = useTrackingSystemsFeature() const isLegal = computed(() => { const result = { title: '', color: '', icon: '' } switch (userFormState.value.is_legal) { case 0: result.title = 'احراز هویت نشده' result.color = 'reject' result.icon = 'vuesax-linear:close-circle' break case 1: case 2: result.title = 'احراز هویت شده' result.color = 'success' result.icon = 'vuesax-linear:verify' break } return result }) const shareButtonLabel = computed(() => 'اشتراک گذاری') const menuActions = computed(() => [ { id: 'password', label: 'تغییر رمز عبور', icon: 'vuesax-linear:lock', onClick: () => { editPasswordVisible.value = true }, visible: true, }, { id: 'notify', label: 'هشدار قیمت', icon: 'vuesax-linear:bill', onClick: () => router.push({ name: 'notifyTrigger' }), visible: true, }, { id: 'about', label: 'درباره ما', icon: 'vuesax-linear:info-circle', onClick: () => router.push({ name: 'about' }), visible: true, }, { id: 'recoverySites', label: 'سایت‌های ری‌گیری', icon: 'vuesax-linear:global', onClick: () => router.push({ name: 'recoverySites' }), visible: trackingSystemsEnabled.value, }, { id: 'rules', label: 'قوانین و مقررات', icon: 'vuesax-linear:task', onClick: () => router.push({ name: 'rules' }), visible: true, }, ].filter((action) => action.visible !== false), ) function setForm() { if (userStore.userInfo) { userFormState.value = { ...userStore.userInfo } } } function copyCode() { const val = userStore.userInfo?.referral_id if (val) { navigator.clipboard.writeText(val) message.info('کد رونوشت شد') } } function shareCode() { const val = `⭐️الان طلا بخر؛ آخر ماه سودشو ببر! با خرید طلای آبشده در مقدارهای کم ولی مستمر، سود بیشتری ببر! این دعوت نامه از طرف "${userStore.userInfo?.name}" عزیز برای شما ارسال شده تا شما هم از بازار طلای کشکول گلد جا نمونید. روی لینک زیر کلیک کنید و ثبت نام خود را تکمیل کنید. kh.kashkolgold.ir/login?id=${userStore.userInfo?.referral_id}` navigator.share({ title: 'کشکول گلد', text: val }) } function ok() { formRef.value?.validateFields().then(async () => { try { loading.value = true await updateMe(userFormState.value) await userStore.getMyInfo() editPassVisible.value = false message.success('اطلاعات شما با موفقیت به روز رسانی شد') location.reload() } catch (e) { console.log(e) } finally { loading.value = false } }) } function submitPassForm() { formPassRef.value?.validateFields().then(async () => { try { loading.value = true await updatePass(userPassForm.value) await userStore.getMyInfo() message.success('پسورد شما با موفقیت به روز رسانی شد') editPasswordVisible.value = false } catch (e) { console.log(e) } finally { loading.value = false } }) } onMounted(() => { setForm() if (userStore.userInfo?.is_legal === 0) { editPassVisible.value = true } }) const context: UserPageContext = { userStore, userFormState, userPassForm, formRef, formPassRef, editPassVisible, editPasswordVisible, isLegal, shareButtonLabel, menuActions, loading, guideTypePassword, isValidPersian, requireNationalCode, passwordRules, copyCode, shareCode, ok, submitPassForm, } provide(USER_PAGE_CONTEXT_KEY, context) return context } export function useUserPageContext() { const context = inject(USER_PAGE_CONTEXT_KEY) if (!context) { throw new Error('useUserPageContext must be used within useUserPage') } return context }