import { useTheme } from '@/components/ThemeContext'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useRouter } from 'expo-router'; import { ArrowLeft } from 'lucide-react-native'; import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, Pressable, ScrollView, StyleSheet, Switch, Text, TextInput, View } from 'react-native'; import { Toast } from 'toastify-react-native'; import { user_api } from '../lib/api'; type FormType = { code: string; referral_share: string; description: string; is_agent: boolean; }; export default function CreateReferrals() { const { isDark } = useTheme(); const { t } = useTranslation(); const router = useRouter(); const queryClient = useQueryClient(); const [form, setForm] = useState({ code: '', referral_share: '', description: '', is_agent: false, }); const { mutate, isPending } = useMutation({ mutationFn: (body: { code: string; referral_share: number; description: string; is_agent: boolean; }) => user_api.create_referral(body), onSuccess: () => { Toast.success(t('Referral yaratildi')); queryClient.refetchQueries({ queryKey: ['my_referrals'] }); router.back(); }, onError: () => { Toast.error(t('Xatolik yuz berdi')); }, }); const [errors, setErrors] = useState({}); const update = (key: keyof FormType, value: any) => setForm((p) => ({ ...p, [key]: value })); const validate = () => { const e: any = {}; if (!form.description || form.description.length < 5) e.description = 'Tavsif kamida 5 ta belgidan iborat bo‘lishi kerak'; if (form.is_agent) { if (!form.referral_share || Number(form.referral_share) <= 0) e.referral_share = 'Agent uchun foiz majburiy'; } setErrors(e); return Object.keys(e).length === 0; }; const handleSave = () => { if (!validate()) return; const payload = { code: form.code, referral_share: form.is_agent ? Number(form.referral_share) : 0, description: form.description, is_agent: form.is_agent, }; mutate(payload); }; return ( {/* HEADER */} router.back()}> {t('Referral yaratish')} {isPending ? ( ) : ( {t('Saqlash')} )} {/* NOM */} {t('Referral nomi')} update('code', v)} /> {errors.code && {t(errors.code)}} {/* TAVSIF */} {t('Tavsif')} update('description', v)} /> {errors.description && {t(errors.description)}} {/* AGENT SWITCH */} {t('Agentmi?')} { update('is_agent', v); if (!v) update('referral_share', ''); }} /> {/* đŸ‘‰ FOIZ FAQAT AGENT YOQILGANDA */} {form.is_agent && ( <> {t('Referral foizi (%)')} { // faqat 1–5 oralig‘ini qabul qiladi if (v === '') { update('referral_share', ''); return; } const num = Number(v); if (num >= 1 && num <= 5) { update('referral_share', v); } }} /> {errors.referral_share && {t(errors.referral_share)}} )} ); } const theme = (isDark: boolean) => ({ backgroundColor: isDark ? '#1e293b' : '#fff', borderColor: isDark ? '#334155' : '#e2e8f0', }); const styles = StyleSheet.create({ header: { padding: 16, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', elevation: 3, }, headerTitle: { fontSize: 18, fontWeight: '700' }, save: { color: '#3b82f6', fontSize: 16, fontWeight: '600' }, container: { padding: 16, gap: 10 }, label: { fontSize: 15, fontWeight: '700' }, error: { color: '#ef4444', fontSize: 13, marginLeft: 6 }, inputBox: { flexDirection: 'row', borderRadius: 16, borderWidth: 1, paddingHorizontal: 6, height: 56, }, textArea: { height: 120, alignItems: 'flex-start' }, input: { flex: 1, fontSize: 16, }, switchRow: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginTop: 10, }, });