import { useState } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { Shield, Lock, LogOut, User, ShieldCheck, Copy, RefreshCw, X } from 'lucide-react'
import client from '../../../api/client'
import SignInSettings from '../sections/SignInSettings'
import { PANEL_CLASS } from '../../../ui/tokens'
import Button from '../../../ui/Button'
const INACTIVITY_PRESETS = [
{ label: '5 δευτερόλεπτα', value: 5 },
{ label: '10 δευτερόλεπτα', value: 10 },
{ label: '30 δευτερόλεπτα', value: 30 },
{ label: '1 λεπτό', value: 60 },
{ label: '2 λεπτά', value: 120 },
{ label: '5 λεπτά', value: 300 },
{ label: '10 λεπτά', value: 600 },
{ label: '15 λεπτά', value: 900 },
{ label: '30 λεπτά', value: 1800 },
{ label: '1 ώρα', value: 3600 },
]
function fmtSeconds(s) {
const n = parseInt(s, 10)
if (isNaN(n) || n <= 0) return ''
if (n < 60) return `${n}s`
if (n < 3600) return `${Math.round(n / 60)}m`
return `${Math.round(n / 3600)}h`
}
// ─── Shared primitives ────────────────────────────────────────────────────────
function SectionCard({ icon: Icon, title, description, children }) {
return (
{title}
{description &&
{description}
}
{children}
)
}
function OptionRow({ label, description, children }) {
return (
{label}
{description &&
{description}
}
{children}
)
}
function Toggle({ checked, onChange, disabled }) {
return (
)
}
function TimeSelect({ value, onChange, disabled }) {
return (
)
}
// ─── Recovery Codes section ───────────────────────────────────────────────────
function RecoveryCodesSection() {
const [revealedCodes, setRevealedCodes] = useState(null)
const [confirming, setConfirming] = useState(false)
const { data: statusData, refetch: refetchStatus } = useQuery({
queryKey: ['recovery-code-status'],
queryFn: () => client.get('/api/recovery/status').then(r => r.data),
})
const generateMut = useMutation({
mutationFn: () => client.post('/api/recovery/generate').then(r => r.data),
onSuccess: (data) => {
setRevealedCodes(data.codes)
setConfirming(false)
refetchStatus()
},
onError: () => toast.error('Σφάλμα δημιουργίας κωδικών'),
})
const unused = statusData?.unused_count ?? null
function copyAll() {
if (!revealedCodes) return
navigator.clipboard.writeText(revealedCodes.join('\n'))
toast.success('Οι κωδικοί αντιγράφηκαν!')
}
return (
{/* Status badge */}
Διαθέσιμοι κωδικοί:
{unused === null ? (
Φόρτωση…
) : (
{unused} / 5
)}
{unused === 0 && (
Δεν υπάρχουν κωδικοί!
)}
{unused > 0 && unused <= 2 && (
Λίγοι κωδικοί
)}
{/* Revealed codes (shown once after generation) */}
{revealedCodes && (
Αντιγράψτε τώρα — δεν θα εμφανιστούν ξανά!
{revealedCodes.map((code, i) => (
{code}
))}
)}
{/* Generate / confirm */}
{!revealedCodes && (
confirming ? (
Αυτό θα ακυρώσει τους {unused} υπάρχοντες κωδικούς και θα δημιουργήσει 5 νέους.
) : (
)
)}
Αποθηκεύστε τους σε ασφαλές μέρος. Κάθε κωδικός δουλεύει μόνο μία φορά.
Χρησιμοποιείται στη σελίδα σύνδεσης με το όνομα χρήστη + κωδικό ανάκτησης.
)
}
// ─── Main tab ─────────────────────────────────────────────────────────────────
export default function SecurityTab() {
const qc = useQueryClient()
const { data: settings, isLoading } = useQuery({
queryKey: ['pos-settings'],
queryFn: () => client.get('/api/settings/').then(r => r.data),
staleTime: 30_000,
})
const updateMut = useMutation({
mutationFn: ({ key, value }) => client.put(`/api/settings/${key}`, { value }),
onSuccess: () => {
toast.success('Αποθηκεύτηκε')
qc.invalidateQueries({ queryKey: ['pos-settings'] })
},
onError: () => toast.error('Σφάλμα αποθήκευσης'),
})
function get(key, fallback) {
return settings?.[key]?.value ?? fallback
}
function save(key, value) {
updateMut.mutate({ key, value })
}
const busy = updateMut.isPending
if (isLoading) {
return (
Φόρτωση…
)
}
const autoLock = get('security.auto_lock', 'false') === 'true'
const autoLockSecs = get('security.auto_lock_seconds', '300')
const autoLogout = get('security.auto_logout', 'false') === 'true'
const autoLogoutSecs = get('security.auto_logout_seconds', '1800')
return (
{/* Sign-in (2.6f): the v2 settings auth.* replace the v1 security.login_method card */}
{/* ── Αυτόματο κλείδωμα ────────────────────────────────────────────── */}
save('security.auto_lock', v ? 'true' : 'false')}
disabled={busy}
/>
{autoLock && (
save('security.auto_lock_seconds', v)}
disabled={busy}
/>
)}
{/* ── Αυτόματη αποσύνδεση ──────────────────────────────────────────── */}
save('security.auto_logout', v ? 'true' : 'false')}
disabled={busy}
/>
{autoLogout && (
save('security.auto_logout_seconds', v)}
disabled={busy}
/>
)}
{autoLock && autoLogout && parseInt(autoLogoutSecs, 10) <= parseInt(autoLockSecs, 10) && (
Ο χρόνος αποσύνδεσης είναι μικρότερος ή ίσος με τον χρόνο κλειδώματος — η οθόνη θα αποσυνδεθεί πριν κλειδώσει.
)}
{/* ── Κωδικοί Ανάκτησης ────────────────────────────────────────────── */}
)
}