'use client';

import { Input } from '@/components/ui/input';
import type { FieldConfig } from '@/types/invyone-component';

interface DateFieldProps {
  field: FieldConfig;
  value: any;
  onChange: (value: any) => void;
  mode: 'form' | 'search';
  disabled?: boolean;
  error?: string;
}

export function DateField({ field, value, onChange, mode, disabled, error }: DateFieldProps) {
  if (mode === 'search') {
    // 검색: DateRangePicker (시작~종료)
    const from = value?.from ?? '';
    const to = value?.to ?? '';
    return (
      <div className="flex items-center gap-1">
        <Input
          type="date"
          value={from}
          onChange={(e) => onChange({ ...value, from: e.target.value || undefined })}
          disabled={disabled}
          className={`h-7 text-xs flex-1 ${error ? 'border-destructive' : ''}`}
        />
        <span className="text-xs text-[var(--v5-text-muted)]">~</span>
        <Input
          type="date"
          value={to}
          onChange={(e) => onChange({ ...value, to: e.target.value || undefined })}
          disabled={disabled}
          className={`h-7 text-xs flex-1 ${error ? 'border-destructive' : ''}`}
        />
      </div>
    );
  }

  return (
    <Input
      type="date"
      value={value ?? ''}
      onChange={(e) => onChange(e.target.value || null)}
      disabled={disabled}
      className={`h-7 text-xs ${error ? 'border-destructive' : ''}`}
    />
  );
}
