@csrf
@php
    $dt = old('recorded_at', isset($medicalRecord) && $medicalRecord->exists ? $medicalRecord->recorded_at->format('Y-m-d\TH:i') : now()->format('Y-m-d\TH:i'));
@endphp

<div class="grid gap-6 md:grid-cols-2">
    <div>
        <x-input-label for="recorded_at" :value="__('Fecha y hora')" />
        <x-text-input id="recorded_at" class="block mt-1 w-full" type="datetime-local" name="recorded_at" :value="$dt" required />
        <x-input-error :messages="$errors->get('recorded_at')" class="mt-2" />
    </div>
    <div>
        <x-input-label for="title" :value="__('Título / resumen (opcional)')" />
        <x-text-input id="title" class="block mt-1 w-full" type="text" name="title" :value="old('title', $medicalRecord->title ?? '')" />
        <x-input-error :messages="$errors->get('title')" class="mt-2" />
    </div>
    <div class="md:col-span-2">
        <x-input-label for="chief_complaint" :value="__('Motivo de consulta / queja principal')" />
        <x-text-input id="chief_complaint" class="block mt-1 w-full" type="text" name="chief_complaint" :value="old('chief_complaint', $medicalRecord->chief_complaint ?? '')" />
        <x-input-error :messages="$errors->get('chief_complaint')" class="mt-2" />
    </div>
    <div>
        <x-input-label for="weight_kg" :value="__('Peso (kg)')" />
        <x-text-input id="weight_kg" class="block mt-1 w-full" type="number" step="0.001" min="0" name="weight_kg" :value="old('weight_kg', $medicalRecord->weight_kg ?? '')" />
        <x-input-error :messages="$errors->get('weight_kg')" class="mt-2" />
    </div>
    <div>
        <x-input-label for="temperature_c" :value="__('Temperatura (°C)')" />
        <x-text-input id="temperature_c" class="block mt-1 w-full" type="number" step="0.1" min="0" max="50" name="temperature_c" :value="old('temperature_c', $medicalRecord->temperature_c ?? '')" />
        <x-input-error :messages="$errors->get('temperature_c')" class="mt-2" />
    </div>
    <div class="md:col-span-2">
        <x-input-label for="physical_exam" :value="__('Examen físico')" />
        <textarea id="physical_exam" name="physical_exam" rows="4"
                  class="block mt-1 w-full rounded-md border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm">{{ old('physical_exam', $medicalRecord->physical_exam ?? '') }}</textarea>
        <x-input-error :messages="$errors->get('physical_exam')" class="mt-2" />
    </div>
    <div class="md:col-span-2">
        <x-input-label for="diagnosis" :value="__('Diagnóstico')" />
        <textarea id="diagnosis" name="diagnosis" rows="3"
                  class="block mt-1 w-full rounded-md border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm">{{ old('diagnosis', $medicalRecord->diagnosis ?? '') }}</textarea>
        <x-input-error :messages="$errors->get('diagnosis')" class="mt-2" />
    </div>
    <div class="md:col-span-2">
        <x-input-label for="treatment" :value="__('Tratamiento / plan')" />
        <textarea id="treatment" name="treatment" rows="3"
                  class="block mt-1 w-full rounded-md border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm">{{ old('treatment', $medicalRecord->treatment ?? '') }}</textarea>
        <x-input-error :messages="$errors->get('treatment')" class="mt-2" />
    </div>
    <div class="md:col-span-2">
        <x-input-label for="medications" :value="__('Medicación indicada')" />
        <textarea id="medications" name="medications" rows="3"
                  class="block mt-1 w-full rounded-md border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm">{{ old('medications', $medicalRecord->medications ?? '') }}</textarea>
        <x-input-error :messages="$errors->get('medications')" class="mt-2" />
    </div>
    <div class="md:col-span-2">
        <x-input-label for="body" :value="__('Notas adicionales / evolución')" />
        <textarea id="body" name="body" rows="6"
                  class="block mt-1 w-full rounded-md border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 font-mono text-sm">{{ old('body', $medicalRecord->body ?? '') }}</textarea>
        <x-input-error :messages="$errors->get('body')" class="mt-2" />
    </div>
    <div class="md:col-span-2">
        <x-input-label for="attachments" :value="__('Adjuntos (imágenes o PDF, máx. 10 archivos)')" />
        <input id="attachments" name="attachments[]" type="file" multiple accept=".jpg,.jpeg,.png,.gif,.webp,.pdf,image/*,application/pdf"
               class="mt-1 block w-full text-sm text-gray-600 dark:text-gray-300 file:mr-4 file:rounded-md file:border-0 file:bg-indigo-50 file:px-4 file:py-2 file:text-sm file:font-medium file:text-indigo-700 dark:file:bg-indigo-900/40 dark:file:text-indigo-200" />
        <x-input-error :messages="$errors->get('attachments')" class="mt-2" />
    </div>
</div>

<div class="flex items-center justify-end gap-3 mt-8">
    <a href="{{ isset($medicalRecord) && $medicalRecord->exists ? route('clients.patients.medical-records.show', [$client, $patient, $medicalRecord]) : route('clients.patients.medical-records.index', [$client, $patient]) }}"
       class="text-sm text-gray-600 dark:text-gray-400 hover:underline">{{ __('Cancelar') }}</a>
    <x-primary-button>{{ $submitLabel }}</x-primary-button>
</div>
