'use client'

import { useState } from 'react'
import type { ProcessTask, CompleteFormData } from './types'
import { EMPTY_COMPLETE } from './types'

export function CompleteModal({
  task,
  onClose,
  onComplete,
}: {
  task: ProcessTask
  onClose: () => void
  onComplete: (data: CompleteFormData) => Promise<void>
}) {
  const [form, setForm] = useState<CompleteFormData>({ ...EMPTY_COMPLETE })
  const [saving, setSaving] = useState(false)

  const handleSave = async () => {
    setSaving(true)
    try {
      await onComplete(form)
      onClose()
    } catch {
      setSaving(false)
    }
  }

  return (
    <div className="fixed inset-0 bg-black/40 z-50 flex items-center justify-center p-4" onClick={e => e.target === e.currentTarget && onClose()}>
      <div className="bg-white rounded-2xl shadow-2xl w-full max-w-md">
        <div className="flex items-center justify-between p-6 border-b">
          <h2 className="text-lg font-semibold text-gray-900">Aufgabe erledigen</h2>
          <button onClick={onClose} className="text-gray-400 hover:text-gray-600 text-xl">{'\u2715'}</button>
        </div>
        <div className="p-6 space-y-4">
          <p className="text-sm text-gray-600 font-medium">{task.title}</p>
          <div>
            <label className="block text-sm font-medium text-gray-700 mb-1">Erledigt von</label>
            <input type="text" value={form.completed_by}
              onChange={e => setForm(prev => ({ ...prev, completed_by: e.target.value }))}
              className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-purple-500"
              placeholder="Name / Rolle" />
          </div>
          <div>
            <label className="block text-sm font-medium text-gray-700 mb-1">Ergebnis</label>
            <textarea value={form.result} onChange={e => setForm(prev => ({ ...prev, result: e.target.value }))}
              rows={3}
              className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-purple-500"
              placeholder="Ergebnis der Pruefung / Massnahme..." />
          </div>
          <div>
            <label className="block text-sm font-medium text-gray-700 mb-1">Notizen</label>
            <textarea value={form.notes} onChange={e => setForm(prev => ({ ...prev, notes: e.target.value }))}
              rows={2}
              className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-purple-500"
              placeholder="Zusaetzliche Hinweise..." />
          </div>
        </div>
        <div className="flex justify-end gap-3 p-6 border-t">
          <button onClick={onClose} className="px-4 py-2 text-sm text-gray-600 hover:bg-gray-100 rounded-lg">Abbrechen</button>
          <button onClick={handleSave} disabled={saving}
            className="px-5 py-2 text-sm bg-green-600 text-white rounded-lg hover:bg-green-700 disabled:opacity-50">
            {saving ? 'Speichern...' : 'Als erledigt markieren'}
          </button>
        </div>
      </div>
    </div>
  )
}
