'use client' import { Trash2 } from 'lucide-react' import { SENSOR_TYPES, COMPARISON_TYPES, SensorType, ComparisonType } from './constants' import { getSensorIcon, getSensorLabel, getSensorUnit, getComparisonLabel } from './utils' import { CreateAlertRuleRequest } from '@/lib/api' type RuleFormCardProps = { rule: CreateAlertRuleRequest index: number totalRules: number onUpdate: (index: number, updates: Partial) => void onRemove: (index: number) => void } export function RuleFormCard({ rule, index, totalRules, onUpdate, onRemove }: RuleFormCardProps) { const needsMax = COMPARISON_TYPES.find(c => c.value === rule.comparisonType)?.needsMax const SensorIcon = getSensorIcon(rule.sensorType) return (
{/* Header - Compact */}
{totalRules === 1 ? '⚡ شرط' : `⚡ شرط ${index + 1}`} {/* Compact summary of current selection */} {getSensorLabel(rule.sensorType)} {' | '} {getComparisonLabel(rule.comparisonType)} {' | '} {rule.value1}{needsMax ? ` - ${rule.value2 ?? '?'}` : ''} {getSensorUnit(rule.sensorType)}
{totalRules > 1 && ( )}
{/* Compact row: Sensor | Comparison | Value */}
{/* Sensor Type - Compact Select */}
{/* Comparison Type - Compact Select */}
{/* Threshold Values - Compact Inputs */}
{ const val = e.target.value // Only update if value is not empty; otherwise keep previous value // to avoid Number('') → 0 breaking the input in some browsers if (val !== '') { onUpdate(index, { value1: Number(val) }) } }} className="w-full px-3 py-2 pl-10 bg-white border border-orange-200 rounded-lg text-sm font-medium focus:ring-2 focus:ring-orange-500 focus:border-orange-500" placeholder={needsMax ? 'از' : 'مقدار'} required /> {getSensorUnit(rule.sensorType)}
{needsMax && ( <> -
{ const val = e.target.value // Only update if value is not empty; otherwise keep previous value // to avoid Number('') → 0 breaking the input in some browsers if (val !== '') { onUpdate(index, { value2: Number(val) }) } }} className="w-full px-3 py-2 pl-10 bg-white border border-orange-200 rounded-lg text-sm font-medium focus:ring-2 focus:ring-orange-500 focus:border-orange-500" placeholder="تا" required /> {getSensorUnit(rule.sensorType)}
)}
) }