289 lines
13 KiB
TypeScript
289 lines
13 KiB
TypeScript
import { useState, useEffect } from 'react';
|
||
import { DollarSign, Save, Plus, X, Heart, Percent, Wallet } from 'lucide-react';
|
||
import { toast } from 'react-hot-toast';
|
||
import api from '../services/api';
|
||
import Spinner from '../components/ui/Spinner';
|
||
import PriceInput from '../components/ui/PriceInput';
|
||
import Button from '../components/ui/Button';
|
||
import type { FinancialSettings } from '../types/admin';
|
||
|
||
|
||
export default function FinancialSettingsPage() {
|
||
const [financial, setFinancial] = useState<FinancialSettings>({
|
||
taxPercentage: 10,
|
||
freeShippingThreshold: 2000000,
|
||
standardShippingFee: 85000,
|
||
charityDonationOptions: [10000, 20000, 50000],
|
||
walletWithdrawalEnabled: false,
|
||
});
|
||
const [charityStep, setCharityStep] = useState('10000');
|
||
const [refillEnabled, setRefillEnabled] = useState(false);
|
||
const [refillPercent, setRefillPercent] = useState('5');
|
||
const [isLoading, setIsLoading] = useState(true);
|
||
const [isSaving, setIsSaving] = useState(false);
|
||
const [donationInput, setDonationInput] = useState('');
|
||
|
||
useEffect(() => {
|
||
let isSubscribed = true;
|
||
Promise.all([
|
||
api.get('/settings/financial'),
|
||
api.get('/admin/settings'),
|
||
])
|
||
.then(([resFin, resAdmin]) => {
|
||
if (!isSubscribed) return;
|
||
const data = resFin.data?.data || resFin.data;
|
||
if (data) {
|
||
setFinancial({
|
||
taxPercentage: Number(data.taxPercentage ?? 10),
|
||
freeShippingThreshold: Number(data.freeShippingThreshold ?? 2000000),
|
||
standardShippingFee: Number(data.standardShippingFee ?? 85000),
|
||
charityDonationOptions: Array.isArray(data.charityDonationOptions)
|
||
? data.charityDonationOptions.map(Number)
|
||
: [10000, 20000, 50000],
|
||
walletWithdrawalEnabled: Boolean(data.walletWithdrawalEnabled ?? false),
|
||
});
|
||
}
|
||
if (resAdmin.data?.success) {
|
||
setCharityStep(resAdmin.data.data.CHARITY_ROUND_STEP || '10000');
|
||
setRefillEnabled(resAdmin.data.data.REFILL_SUBSCRIPTION_ENABLED === 'true');
|
||
setRefillPercent(resAdmin.data.data.REFILL_REWARD_PERCENT || '5');
|
||
}
|
||
})
|
||
.catch((err) => {
|
||
console.warn('Failed to fetch Financial settings', err);
|
||
})
|
||
.finally(() => {
|
||
if (isSubscribed) setIsLoading(false);
|
||
});
|
||
return () => {
|
||
isSubscribed = false;
|
||
};
|
||
}, []);
|
||
|
||
const handleSave = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
try {
|
||
setIsSaving(true);
|
||
await Promise.all([
|
||
api.patch('/settings/financial', financial),
|
||
api.put('/admin/settings', {
|
||
TAX_PERCENTAGE: String(financial.taxPercentage),
|
||
CHARITY_ROUND_STEP: charityStep,
|
||
REFILL_SUBSCRIPTION_ENABLED: refillEnabled ? 'true' : 'false',
|
||
REFILL_REWARD_PERCENT: refillPercent,
|
||
}),
|
||
]);
|
||
toast.success('تنظیمات مالی با موفقیت بروزرسانی شد');
|
||
} catch (err) {
|
||
console.error('Failed to update financial settings:', err);
|
||
toast.error('خطا در بروزرسانی تنظیمات مالی');
|
||
} finally {
|
||
setIsSaving(false);
|
||
}
|
||
};
|
||
|
||
const addDonationOption = () => {
|
||
const val = Number(donationInput);
|
||
if (val > 0 && !financial.charityDonationOptions.includes(val)) {
|
||
setFinancial({
|
||
...financial,
|
||
charityDonationOptions: [...financial.charityDonationOptions, val].sort((a, b) => a - b),
|
||
});
|
||
setDonationInput('');
|
||
}
|
||
};
|
||
|
||
const removeDonationOption = (amount: number) => {
|
||
setFinancial({
|
||
...financial,
|
||
charityDonationOptions: financial.charityDonationOptions.filter((a) => a !== amount),
|
||
});
|
||
};
|
||
|
||
return (
|
||
<div className="space-y-6 font-vazir" dir="rtl">
|
||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
||
<div>
|
||
<h2 className="text-2xl font-black text-gray-900 flex items-center gap-2">
|
||
<DollarSign className="w-6 h-6 text-emerald-600" />
|
||
تنظیمات مالی، مالیات و خیریه (Financial Settings)
|
||
</h2>
|
||
<p className="text-gray-500 font-medium mt-1">
|
||
تعیین درصد مالیات بر ارزش افزوده، کمکهای خیریه ردپای مهربانی و تسویهحساب کیف پول
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
{isLoading ? (
|
||
<div className="flex justify-center p-12">
|
||
<Spinner size="lg" className="text-emerald-600" />
|
||
</div>
|
||
) : (
|
||
<form onSubmit={handleSave} className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-6">
|
||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||
{/* Tax Percentage */}
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1.5 flex items-center gap-1.5">
|
||
<Percent className="w-4 h-4 text-emerald-600" />
|
||
<span>درصد مالیات بر ارزش افزوده (VAT) *</span>
|
||
</label>
|
||
<div className="relative">
|
||
<input
|
||
required
|
||
type="number"
|
||
min="0"
|
||
max="100"
|
||
value={financial.taxPercentage}
|
||
onChange={(e) => setFinancial({ ...financial, taxPercentage: Number(e.target.value) })}
|
||
className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-emerald-500 outline-none font-bold text-sm"
|
||
dir="ltr"
|
||
/>
|
||
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold">%</span>
|
||
</div>
|
||
<p className="text-xs text-gray-500 mt-1">درصد قانونی محاسبه مالیات بر ارزش افزوده در فاکتور نهایی سفارشات.</p>
|
||
</div>
|
||
|
||
{/* Charity Round Step */}
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1.5 flex items-center gap-1.5">
|
||
<Heart className="w-4 h-4 text-rose-500" />
|
||
<span>پله رند کردن کمک به خیریه ردپای مهربانی (تومان)</span>
|
||
</label>
|
||
<PriceInput
|
||
value={charityStep}
|
||
onChange={(val) => setCharityStep(val)}
|
||
placeholder="10000"
|
||
className="px-4 py-3 rounded-xl border border-gray-200 focus:border-emerald-500 text-sm"
|
||
/>
|
||
<p className="text-xs text-gray-500 mt-1">مبلغ فاکتور به نزدیکترین ضریب این عدد (مثلاً ۱۰,۰۰۰ یا ۵۰,۰۰۰ تومان) رند میگردد.</p>
|
||
</div>
|
||
|
||
{/* Charity Donation Options */}
|
||
<div className="md:col-span-2 space-y-2 pt-3 border-t border-gray-100">
|
||
<label className="block text-sm font-bold text-gray-700 mb-1 flex items-center gap-1.5">
|
||
<Heart className="w-4 h-4 text-rose-500" />
|
||
<span>گزینههای دلخواه کمک مستقیم به خیریه (charityDonationOptions)</span>
|
||
</label>
|
||
<div className="flex gap-2 max-w-md">
|
||
<PriceInput
|
||
placeholder="مبلغ جدید (مثال: ۱۰۰,۰۰۰)"
|
||
value={donationInput}
|
||
onChange={(val) => setDonationInput(val)}
|
||
className="px-4 py-2 rounded-xl border border-gray-200 text-xs"
|
||
/>
|
||
<Button
|
||
type="button"
|
||
variant="primary"
|
||
size="xs"
|
||
startIcon={Plus}
|
||
onClick={addDonationOption}
|
||
>
|
||
افزودن
|
||
</Button>
|
||
</div>
|
||
|
||
<div className="flex flex-wrap gap-2 pt-2">
|
||
{financial.charityDonationOptions.map((amount) => (
|
||
<span
|
||
key={amount}
|
||
className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-rose-50 text-rose-700 text-xs font-bold rounded-xl border border-rose-200"
|
||
>
|
||
{amount.toLocaleString('fa-IR')} تومان
|
||
<button
|
||
type="button"
|
||
onClick={() => removeDonationOption(amount)}
|
||
className="hover:text-red-500 cursor-pointer"
|
||
>
|
||
<X className="w-3.5 h-3.5" />
|
||
</button>
|
||
</span>
|
||
))}
|
||
</div>
|
||
</div>
|
||
|
||
{/* Refill Subscription and Reward Cashback */}
|
||
<div className="md:col-span-2 pt-4 border-t border-gray-100 space-y-4">
|
||
<label className="flex items-center justify-between cursor-pointer p-4 bg-indigo-50/50 hover:bg-indigo-50 rounded-2xl border border-indigo-100 transition-colors">
|
||
<div className="flex items-start gap-3">
|
||
<input
|
||
type="checkbox"
|
||
checked={refillEnabled}
|
||
onChange={(e) => setRefillEnabled(e.target.checked)}
|
||
className="w-5 h-5 mt-0.5 rounded-lg text-indigo-600 focus:ring-indigo-500 border-gray-300 cursor-pointer"
|
||
/>
|
||
<div>
|
||
<span className="text-sm font-black text-gray-900 block flex items-center gap-2">
|
||
<Percent className="w-4 h-4 text-indigo-600" />
|
||
فعالسازی سرویس «رزرو هوشمند تمدید و پاداش خرید بعدی» (Refill Subscription)
|
||
</span>
|
||
<span className="text-xs text-gray-500 font-medium block mt-0.5">
|
||
در صورت فعال بودن، گزینهای در سبد خرید و تسویهحساب به مشتری پیشنهاد میشود تا با فعالسازی یادآور تمدید، درصد مشخصی پاداش (اعتبار خرید بعدی) رزرو کند.
|
||
</span>
|
||
</div>
|
||
</div>
|
||
</label>
|
||
|
||
{refillEnabled && (
|
||
<div className="p-4 bg-white rounded-2xl border border-indigo-100 grid grid-cols-1 md:grid-cols-2 gap-4">
|
||
<div>
|
||
<label className="block text-xs font-bold text-gray-700 mb-1.5">
|
||
درصد پاداش رزرو تمدید (Cashback Reward Percent)
|
||
</label>
|
||
<div className="relative">
|
||
<input
|
||
type="number"
|
||
min="1"
|
||
max="100"
|
||
value={refillPercent}
|
||
onChange={(e) => setRefillPercent(e.target.value)}
|
||
className="w-full px-4 py-2.5 rounded-xl border border-gray-200 focus:border-indigo-500 outline-none font-bold text-xs"
|
||
dir="ltr"
|
||
/>
|
||
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold text-xs">%</span>
|
||
</div>
|
||
<p className="text-[11px] text-gray-400 mt-1">پیشفرض: ۵ درصد</p>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* Wallet Withdrawal Feature Toggle */}
|
||
<div className="md:col-span-2 pt-4 border-t border-gray-100">
|
||
<label className="flex items-center gap-3 cursor-pointer p-4 bg-emerald-50/50 hover:bg-emerald-50 rounded-2xl border border-emerald-100 transition-colors">
|
||
<input
|
||
type="checkbox"
|
||
checked={Boolean(financial.walletWithdrawalEnabled)}
|
||
onChange={(e) => setFinancial({ ...financial, walletWithdrawalEnabled: e.target.checked })}
|
||
className="w-5 h-5 rounded-lg text-emerald-600 focus:ring-emerald-500 border-gray-300 cursor-pointer"
|
||
/>
|
||
<div>
|
||
<span className="text-sm font-black text-gray-900 block flex items-center gap-2">
|
||
<Wallet className="w-4 h-4 text-emerald-600" />
|
||
فعالسازی امکان ثبت درخواست برداشت و تسویهحساب کیف پول
|
||
</span>
|
||
<span className="text-xs text-gray-500 font-medium block mt-0.5">
|
||
در صورت فعال بودن، کاربران میتوانند از طریق داشبورد خود درخواست تسویه و واریز وجه کیف پول به شماره شبا ثبت کنند.
|
||
</span>
|
||
</div>
|
||
</label>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="flex justify-end pt-4 border-t border-gray-100">
|
||
<Button
|
||
type="submit"
|
||
variant="primary"
|
||
size="md"
|
||
startIcon={Save}
|
||
isLoading={isSaving}
|
||
>
|
||
ذخیره تنظیمات مالی
|
||
</Button>
|
||
</div>
|
||
</form>
|
||
)}
|
||
|
||
</div>
|
||
);
|
||
}
|