import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { cn } from '@/lib/utils'; import axios from 'axios'; import { Check, Loader2, X } from 'lucide-react'; import { useState } from 'react'; interface Props { examId?: number; onCouponApplied?: (coupon: ExamCoupon) => void; onCouponRemoved?: () => void; className?: string; } const CouponInput = ({ examId, onCouponApplied, onCouponRemoved, className }: Props) => { const [code, setCode] = useState(''); const [loading, setLoading] = useState(false); const [appliedCoupon, setAppliedCoupon] = useState(null); const [error, setError] = useState(''); const handleApply = async () => { if (!code.trim()) { setError('Please enter a coupon code'); return; } setLoading(true); setError(''); try { const response = await axios.post(route('admin.exam-coupons.verify'), { code: code.trim(), exam_id: examId, }); if (response.data.valid) { setAppliedCoupon(response.data.coupon); onCouponApplied?.(response.data.coupon); setCode(''); } } catch (err: any) { setError(err.response?.data?.message || 'Invalid or expired coupon code'); } finally { setLoading(false); } }; const handleRemove = () => { setAppliedCoupon(null); setError(''); onCouponRemoved?.(); }; if (appliedCoupon) { return (

Coupon Applied

Code: {appliedCoupon.code}

{appliedCoupon.discount_type === 'percentage' ? `${appliedCoupon.discount}% OFF` : `$${appliedCoupon.discount} OFF`}
); } return (
{ setCode(e.target.value.toUpperCase()); setError(''); }} placeholder="Enter coupon code" disabled={loading} onKeyPress={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleApply(); } }} />
{error && (

{error}

)}
); }; export default CouponInput;