import React, { useState } from 'react';
import { Head } from '@inertiajs/react';
import EmployerLayout from '../../Layouts/EmployerLayout';
import { useTranslation } from '../../lib/LanguageContext';
import {
CreditCard,
CheckCircle2,
AlertCircle,
ArrowRight,
Sparkles,
FileText,
Download,
ShieldCheck,
Clock,
User,
Building,
Check,
Lock,
RefreshCw,
X,
TrendingUp
} from 'lucide-react';
import { toast } from 'sonner';
import axios from 'axios';
export default function Subscription({ currentPlan, expiresAt, plans, invoices: initialInvoices }) {
const { t } = useTranslation();
const [selectedPlan, setSelectedPlan] = useState(null);
const [showPayTabsModal, setShowPayTabsModal] = useState(false);
const [showSuccess, setShowSuccess] = useState(false);
// Card inputs for PayTabs Gateway
const [cardName, setCardName] = useState('');
const [cardNumber, setCardNumber] = useState('');
const [cardExpiry, setCardExpiry] = useState('');
const [cardCvv, setCardCvv] = useState('');
const [billingEmail, setBillingEmail] = useState('sponsor@marketplace.ae');
// Failed payment simulation
const [simulatedFailedState, setSimulatedFailedState] = useState(false);
const [activePlan, setActivePlan] = useState(currentPlan || 'Premium Employer Pass');
// Payment History List
const [invoices, setInvoices] = useState(initialInvoices || []);
React.useEffect(() => {
if (initialInvoices) {
setInvoices(initialInvoices);
}
}, [initialInvoices]);
const handleSelectPlan = (plan) => {
setSelectedPlan(plan);
setShowPayTabsModal(true);
};
const handlePayTabsSubmit = (e) => {
e.preventDefault();
if (cardNumber.length < 16) {
toast.error(`💳 ${t('invalid_card_format', 'Invalid card number format')}`);
return;
}
axios.post(route('employer.subscription.purchase'), {
plan_id: selectedPlan.id
})
.then(response => {
if (response.data.success) {
setShowPayTabsModal(false);
setShowSuccess(true);
if (response.data.currentPlan) {
setActivePlan(response.data.currentPlan);
}
setInvoices(response.data.invoices);
toast.success(`🎉 ${t('payment_approved', 'Payment Approved by PayTabs!')}`, {
description: t('successfully_subscribed_desc', 'Successfully subscribed to {plan}. Premium access limits updated instantly.').replace('{plan}', selectedPlan.name),
duration: 5000,
});
// Reset forms
setCardName('');
setCardNumber('');
setCardExpiry('');
setCardCvv('');
setTimeout(() => setShowSuccess(false), 5000);
}
})
.catch(error => {
toast.error(error.response?.data?.error || 'Failed to complete subscription upgrade');
});
};
const triggerFailedPaymentSimulation = () => {
setSimulatedFailedState(true);
toast.warning(t('simulated_billing_failure_triggered', 'Simulated billing failure triggered!'), {
description: t('simulated_billing_failure_desc', 'An automatic system warning will now request payment retry options.')
});
};
const handleRetryPayment = () => {
const premium = plans.find(p => p.id === 'premium') || plans[0];
setSelectedPlan(premium);
setShowPayTabsModal(true);
setSimulatedFailedState(false);
};
const downloadReceipt = (invoiceId) => {
toast.success(`📄 ${t('invoice_generated', 'Invoice {id} generated').replace('{id}', invoiceId)}`, {
description: t('invoice_download_desc', 'Direct MOHRE Sponsor receipt downloaded in PDF format successfully.')
});
};
return (
{t('recurrent_billing_failed_desc', 'Card renewal failed due to insufficient funds. Retry using PayTabs to avoid profile lockout.')}
{t('renewing_auto_payment_on', 'Renewing via auto-payment on')} {expiresAt}
{t('available_subscription_tiers_desc', 'Upgrade or cancel anytime. All contracts compliant with UAE Tadbeer MOHRE guides.')}