import { useEffect, useState } from "react"; import { CardUiRegular } from "@fluentui/react-icons"; import { Button, Input, Tag, message } from "../ui"; import { PolicySwitchCard } from "./PolicySwitchCard"; import { CardPolicyAPN } from "./CardPolicyAPN"; import { useCardPolicyToggles } from "./useCardPolicyToggles"; import { enableVoWiFi, disableVoWiFi, setFlightMode, updateCardPolicy } from "./deviceActions"; import type { CardPolicy } from "../../types"; import { useI18n } from "../../lib/i18n"; import { apiMessage } from "../../api"; export interface CardPolicyPanelProps { deviceId: string; iccid?: string; policy: CardPolicy | null; deviceOnline: boolean; onPolicyChanged: () => void | Promise; } export function CardPolicyPanel({ deviceId, iccid, policy, deviceOnline, onPolicyChanged }: CardPolicyPanelProps) { const { t } = useI18n(); const operable = deviceOnline && !!iccid; const currentPolicy = policy?.iccid === iccid ? policy : null; const [customPhoneNumber, setCustomPhoneNumber] = useState(currentPolicy?.customPhoneNumber || ""); const [phoneSaving, setPhoneSaving] = useState(false); const flags = currentPolicy ? { vowifiEnabled: currentPolicy.vowifiEnabled, airplaneEnabled: currentPolicy.airplaneEnabled } : null; const toggles = useCardPolicyToggles(flags, { applyVoWiFi: (value) => (deviceId ? (value ? enableVoWiFi(deviceId) : disableVoWiFi(deviceId)) : Promise.resolve({ ok: false })), applyAirplane: (value) => (deviceId ? setFlightMode(deviceId, value) : Promise.resolve({ ok: false })), onChanged: onPolicyChanged, }); const isManual = currentPolicy?.source === "user" || currentPolicy?.source === "manual"; const sourceLabel = currentPolicy ? (isManual ? t("手动设置") : t("自动默认")) : ""; const { local } = toggles; const savedPhoneNumber = currentPolicy?.customPhoneNumber || ""; const phoneChanged = customPhoneNumber.trim() !== savedPhoneNumber; useEffect(() => { setCustomPhoneNumber(currentPolicy?.customPhoneNumber || ""); }, [iccid, currentPolicy?.customPhoneNumber]); const saveCustomPhoneNumber = async () => { if (!iccid || phoneSaving || !phoneChanged) return; setPhoneSaving(true); try { const saved = await updateCardPolicy(iccid, { customPhoneNumber: customPhoneNumber.trim() }); setCustomPhoneNumber(saved.customPhoneNumber || ""); message.success(saved.customPhoneNumber ? t("自定义手机号已保存") : t("已恢复显示系统读取的号码")); await onPolicyChanged(); } catch (error) { message.error(apiMessage(error) || t("保存自定义手机号失败")); } finally { setPhoneSaving(false); } }; return (
{t("卡策略")}
{t("VoWiFi / 飞行模式 开关跟着 SIM 卡走,切换即时生效")}
{!iccid ? (
{t("设备尚未识别到 SIM 卡 ICCID,策略不可操作")}
) : null} {iccid && !deviceOnline ? (
{t("设备离线,策略仅展示,切换操作已禁用")}
) : null} {iccid ? (
{t("当前卡 ICCID")}
{iccid}
{sourceLabel ? {sourceLabel} : null}
{t("自定义手机号")}
setCustomPhoneNumber(event.target.value)} onKeyDown={(event) => { if (event.key === "Enter") void saveCustomPhoneNumber(); }} placeholder={t("请输入手机号(可留空)")} inputMode="tel" maxLength={32} disabled={phoneSaving} aria-label={t("自定义手机号")} />
{t("支持开头的 + 和 3-20 位数字;留空时显示系统从 SIM/网络读取的号码")}
) : null}
); }