"use client"; import React, { useState } from 'react'; import { useRouter } from 'next/navigation'; import { PlusIcon, ArrowPathIcon, XMarkIcon, ExclamationTriangleIcon, CheckCircleIcon } from '@heroicons/react/24/outline'; import { TopUpModal } from './TopUpModal'; import { ChangePlanModal } from './ChangePlanModal'; import { authenticatedApi } from '@/lib/api'; interface SimActionsProps { subscriptionId: number; simType: 'physical' | 'esim'; status: string; onTopUpSuccess?: () => void; onPlanChangeSuccess?: () => void; onCancelSuccess?: () => void; onReissueSuccess?: () => void; embedded?: boolean; // when true, render content without card container currentPlanCode?: string; } export function SimActions({ subscriptionId, simType, status, onTopUpSuccess, onPlanChangeSuccess, onCancelSuccess, onReissueSuccess, embedded = false, currentPlanCode }: SimActionsProps) { const router = useRouter(); const [showTopUpModal, setShowTopUpModal] = useState(false); const [showCancelConfirm, setShowCancelConfirm] = useState(false); const [showReissueConfirm, setShowReissueConfirm] = useState(false); const [loading, setLoading] = useState(null); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [showChangePlanModal, setShowChangePlanModal] = useState(false); const [activeInfo, setActiveInfo] = useState< 'topup' | 'reissue' | 'cancel' | 'changePlan' | null >(null); const isActive = status === 'active'; const canTopUp = isActive; const canReissue = isActive && simType === 'esim'; const canCancel = isActive; const handleReissueEsim = async () => { setLoading('reissue'); setError(null); try { await authenticatedApi.post(`/subscriptions/${subscriptionId}/sim/reissue-esim`); setSuccess('eSIM profile reissued successfully'); setShowReissueConfirm(false); onReissueSuccess?.(); } catch (error: any) { setError(error instanceof Error ? error.message : 'Failed to reissue eSIM profile'); } finally { setLoading(null); } }; const handleCancelSim = async () => { setLoading('cancel'); setError(null); try { await authenticatedApi.post(`/subscriptions/${subscriptionId}/sim/cancel`, {}); setSuccess('SIM service cancelled successfully'); setShowCancelConfirm(false); onCancelSuccess?.(); } catch (error: any) { setError(error instanceof Error ? error.message : 'Failed to cancel SIM service'); } finally { setLoading(null); } }; // Clear success/error messages after 5 seconds React.useEffect(() => { if (success || error) { const timer = setTimeout(() => { setSuccess(null); setError(null); }, 5000); return () => clearTimeout(timer); } return; }, [success, error]); return (
{/* Header */}

SIM Management Actions

Manage your SIM service

{/* Content */}
{/* Status Messages */} {success && (

{success}

)} {error && (

{error}

)} {!isActive && (

SIM management actions are only available for active services.

)} {/* Action Buttons */}
{/* Top Up Data - Primary Action */} {/* Reissue eSIM (only for eSIMs) */} {simType === 'esim' && ( )} {/* Cancel SIM - Destructive Action */} {/* Change Plan - Secondary Action */}
{/* Action Description (contextual) */} {activeInfo && (
{activeInfo === 'topup' && (
Top Up Data: Add additional data quota to your SIM service. You can choose the amount and schedule it for later if needed.
)} {activeInfo === 'reissue' && (
Reissue eSIM: Generate a new eSIM profile for download. Use this if your previous download failed or you need to install on a new device.
)} {activeInfo === 'cancel' && (
Cancel SIM: Permanently cancel your SIM service. This action cannot be undone and will terminate your service immediately.
)} {activeInfo === 'changePlan' && (
Change Plan: Switch to a different data plan. Important: Plan changes must be requested before the 25th of the month. Changes will take effect on the 1st of the following month.
)}
)}
{/* Top Up Modal */} {showTopUpModal && ( { setShowTopUpModal(false); setActiveInfo(null); }} onSuccess={() => { setShowTopUpModal(false); setSuccess('Data top-up completed successfully'); onTopUpSuccess?.(); }} onError={(message) => setError(message)} /> )} {/* Change Plan Modal */} {showChangePlanModal && ( { setShowChangePlanModal(false); setActiveInfo(null); }} onSuccess={() => { setShowChangePlanModal(false); setSuccess('SIM plan change submitted successfully'); onPlanChangeSuccess?.(); }} onError={(message) => setError(message)} /> )} {/* Reissue eSIM Confirmation */} {showReissueConfirm && (

Reissue eSIM Profile

This will generate a new eSIM profile for download. Your current eSIM will remain active until you activate the new profile.

)} {/* Cancel Confirmation */} {showCancelConfirm && (

Cancel SIM Service

Are you sure you want to cancel this SIM service? This action cannot be undone and will permanently terminate your service.

)}
); }