@extends('layouts.app')

@section('title', 'Incidencias de Seguridad - Ojo de Águila')
@section('header_title', 'Reporte de Incidencias')

@section('content')
<div class="dashboard-grid">
    <!-- Formulario para Reportar -->
    <div class="card">
        <div class="card-header">
            <h3 class="card-title">🚨 Reportar Incidencia</h3>
        </div>
        <div class="card-body">
            <form action="{{ route('incidents.store') }}" method="POST">
                @csrf
                <div class="form-group">
                    <label for="title" class="form-label required">Título / Suceso</label>
                    <input type="text" name="title" id="title" class="form-control" placeholder="Ej: Falla eléctrica portón, sospechoso en perímetro..." required>
                </div>

                <div class="form-group">
                    <label for="severity" class="form-label required">Severidad / Urgencia</label>
                    <select name="severity" id="severity" class="form-control" required>
                        <option value="low">Baja (Mantenimiento menor)</option>
                        <option value="medium">Media (Atención rutinaria)</option>
                        <option value="high">Alta (Requiere acción rápida)</option>
                        <option value="critical">Crítica (Emergencia de seguridad)</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="description" class="form-label required">Descripción detallada</label>
                    <textarea name="description" id="description" class="form-control" rows="5" placeholder="Ingresa detalles sobre lo ocurrido y acciones tomadas..." required></textarea>
                </div>

                <button type="submit" class="btn btn-primary btn-full" style="height: 44px; margin-top: 8px;">
                    🛡️ Registrar Incidencia
                </button>
            </form>
        </div>
    </div>

    <!-- Listado de Incidencias -->
    <div style="display: flex; flex-direction: column; gap: 20px;">
        @forelse($incidents as $inc)
            @php
                $severityClass = $inc->severity === 'critical' || $inc->severity === 'high' ? 'badge-danger' : ($inc->severity === 'medium' ? 'badge-warning' : 'badge-success');
                $statusColor = $inc->status === 'open' ? 'var(--danger-color)' : 'var(--success-color)';
                $statusLabel = $inc->status === 'open' ? 'Abierto' : 'Resuelto';
            @endphp
            <div class="card" style="margin-bottom: 0;">
                <div class="card-header" style="padding: 16px 20px; display: flex; align-items: center; justify-content: space-between;">
                    <div style="display: flex; align-items: center; gap: 10px;">
                        <span style="width: 8px; height: 8px; border-radius: 50%; background-color: {{ $statusColor }};"></span>
                        <span style="font-weight: 700; font-size: 14px; color: var(--text-primary);">{{ $inc->title }}</span>
                    </div>
                    <span class="badge {{ $severityClass }}" style="font-size: 10px;">
                        {{ $inc->severity }}
                    </span>
                </div>
                <div class="card-body" style="padding: 20px;">
                    <p style="font-size: 13px; color: var(--text-secondary); margin-bottom: 16px;">
                        {{ $inc->description }}
                    </p>
                    <div style="display: flex; justify-content: space-between; align-items: center; font-size: 11px; color: var(--text-light); font-weight: 600;">
                        <span>👮 Reportado por: <strong>{{ $inc->reporter->name }}</strong></span>
                        <span>⏱️ {{ $inc->created_at instanceof \Carbon\Carbon ? $inc->created_at->diffForHumans() : \Carbon\Carbon::parse($inc->created_at)->diffForHumans() }}</span>
                    </div>
                </div>
            </div>
        @empty
            <div class="card">
                <div class="card-body" style="text-align: center; color: var(--text-light); padding: 40px;">
                    No se registran incidencias hoy.
                </div>
            </div>
        @endforelse
    </div>
</div>
@endsection
