import React, { useState, useEffect } from 'react'; import { Save, Key, Globe, DollarSign, Bell, Shield, Database, Ticket } from 'lucide-react'; import { generalAPI } from '../services/api'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import { getErrorMessage, getSuccessMessage } from '../utils/errorHandler'; const Settings = () => { const [settings, setSettings] = useState({ paymentApiKey: '', webhookUrl: '', currency: 'USD', notifications: { email: true, sms: false, webhook: true }, security: { twoFactor: false, sessionTimeout: 30 }, data: { retentionPeriod: 365, autoBackup: true } }); const [voucherConfig, setVoucherConfig] = useState({ expireInDays: 0, refundUponVoucherExpiration: true, canPurchaseByAgentVouchers: true, canTopUpWithUserVouchers: true }); const [loading, setLoading] = useState(false); const [voucherLoading, setVoucherLoading] = useState(false); const [message, setMessage] = useState(''); const [activeTab, setActiveTab] = useState('payment'); useEffect(() => { loadSettings(); loadVoucherConfiguration(); }, []); const loadSettings = () => { const savedSettings = localStorage.getItem('adminSettings'); if (savedSettings) { setSettings(JSON.parse(savedSettings)); } }; const loadVoucherConfiguration = async () => { try { setVoucherLoading(true); const config = await generalAPI.getVoucherConfiguration(); if (config) { setVoucherConfig({ expireInDays: config.expireInDays ?? 0, refundUponVoucherExpiration: config.refundUponVoucherExpiration ?? true, canPurchaseByAgentVouchers: config.canPurchaseByAgentVouchers ?? true, canTopUpWithUserVouchers: config.canTopUpWithUserVouchers ?? true }); } } catch (err) { console.error('Error loading voucher configuration:', err); toast.error(getErrorMessage(err)); } finally { setVoucherLoading(false); } }; const handleInputChange = (section, field, value) => { setSettings(prev => ({ ...prev, [section]: { ...prev[section], [field]: value } })); }; const handleDirectChange = (field, value) => { setSettings(prev => ({ ...prev, [field]: value })); }; const handleSave = async () => { setLoading(true); setMessage(''); try { // Simulate API call await new Promise(resolve => setTimeout(resolve, 1000)); localStorage.setItem('adminSettings', JSON.stringify(settings)); setMessage('Settings saved successfully!'); setTimeout(() => setMessage(''), 3000); } catch (error) { setMessage('Failed to save settings'); } finally { setLoading(false); } }; const handleVoucherSave = async () => { setVoucherLoading(true); try { const result = await generalAPI.updateVoucherConfiguration(voucherConfig); toast.success(getSuccessMessage(result) || 'Voucher configuration saved successfully'); await loadVoucherConfiguration(); } catch (err) { toast.error(getErrorMessage(err)); } finally { setVoucherLoading(false); } }; const handleVoucherChange = (field, value) => { setVoucherConfig(prev => ({ ...prev, [field]: value })); }; const tabs = [ { id: 'payment', label: 'Payment', icon: DollarSign }, { id: 'notifications', label: 'Notifications', icon: Bell }, { id: 'security', label: 'Security', icon: Shield }, { id: 'data', label: 'Data', icon: Database }, { id: 'voucher', label: 'Voucher', icon: Ticket } ]; const currencies = [ { value: 'USD', label: 'US Dollar (USD)' }, { value: 'EUR', label: 'Euro (EUR)' }, { value: 'GBP', label: 'British Pound (GBP)' }, { value: 'CAD', label: 'Canadian Dollar (CAD)' }, { value: 'AUD', label: 'Australian Dollar (AUD)' }, { value: 'JPY', label: 'Japanese Yen (JPY)' } ]; return (
Configure your payment system preferences
Your secure API key for payment processing
URL to receive payment notifications
Receive notifications via email
Receive notifications via SMS
Send notifications to webhook URL
Enable 2FA for enhanced security
Automatically log out after inactivity
How long to keep transaction data
Automatically backup data daily
Number of days before vouchers expire
Automatically refund when voucher expires
Allow purchases using agent-generated vouchers
Allow top-up using user-generated vouchers