"use client"; import React, { useMemo, useState } from "react"; import { useRouter } from "next/navigation"; import { PlusIcon, ArrowPathIcon, XMarkIcon, ExclamationTriangleIcon, } from "@heroicons/react/24/outline"; import { TopUpModal } from "./TopUpModal"; import { ChangePlanModal } from "./ChangePlanModal"; import { ReissueSimModal } from "./ReissueSimModal"; import { apiClient } from "@/lib/api"; import { AlertBanner } from "@/components/molecules/AlertBanner/AlertBanner"; import { Button } from "@/components/atoms/button"; 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 [showReissueModal, setShowReissueModal] = 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; const canCancel = isActive; const reissueDisabledReason = useMemo(() => { if (!isActive) { return "SIM must be active to request a reissue."; } return null; }, [isActive]); const handleCancelSim = async () => { setLoading("cancel"); setError(null); try { await apiClient.POST("/api/subscriptions/{id}/sim/cancel", { params: { path: { id: subscriptionId } }, body: {}, }); setSuccess("SIM service cancelled successfully"); setShowCancelConfirm(false); onCancelSuccess?.(); } catch (error: unknown) { setError( process.env.NODE_ENV === "development" ? error instanceof Error ? error.message : "Failed to cancel SIM service" : "Unable to cancel SIM service right now. Please try again." ); } 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 */} {!embedded && (

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 */} {/* Change Plan - Secondary Action */} {/* Reissue SIM */} {/* Cancel SIM - Destructive 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 SIM: Submit a replacement request for either a physical SIM or an eSIM. eSIM users can optionally supply a new EID to pair with the replacement profile.
)} {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 SIM Modal */} {showReissueModal && ( { setShowReissueModal(false); setActiveInfo(null); }} onSuccess={() => { setShowReissueModal(false); setSuccess("SIM reissue request submitted successfully"); onReissueSuccess?.(); }} onError={message => { setError(message); }} /> )} {/* 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.

)}
); }