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 (

Settings

Configure your payment system preferences

{/* Message */} {message && (
{message}
)}
{/* Sidebar */}
{/* Content */}
{/* Payment Settings */} {activeTab === 'payment' && (

Payment Configuration

handleDirectChange('paymentApiKey', e.target.value)} className="input-field" placeholder="Enter your payment API key" />

Your secure API key for payment processing

handleDirectChange('webhookUrl', e.target.value)} className="input-field" placeholder="https://your-domain.com/webhook" />

URL to receive payment notifications

)} {/* Notification Settings */} {activeTab === 'notifications' && (

Notification Preferences

Receive notifications via email

Receive notifications via SMS

Send notifications to webhook URL

)} {/* Security Settings */} {activeTab === 'security' && (

Security Settings

Enable 2FA for enhanced security

handleInputChange('security', 'sessionTimeout', parseInt(e.target.value))} className="input-field w-32" />

Automatically log out after inactivity

)} {/* Data Settings */} {activeTab === 'data' && (

Data Management

handleInputChange('data', 'retentionPeriod', parseInt(e.target.value))} className="input-field w-32" />

How long to keep transaction data

Automatically backup data daily

)} {/* Voucher Settings */} {activeTab === 'voucher' && (

Voucher Configuration

{voucherLoading ? (
) : (
handleVoucherChange('expireInDays', Number(e.target.value) || 0)} className="input-field w-32" />

Number of days before vouchers expire

Automatically refund when voucher expires

Allow purchases using agent-generated vouchers

Allow top-up using user-generated vouchers

)}
)} {/* Save Button */}
{activeTab === 'voucher' ? ( ) : ( )}
); }; export default Settings;