@extends('layouts.app')

@section('title', $item ? 'Editar Ítem de Checklist' : 'Nuevo Ítem de Checklist')
@section('page-title', $item ? 'Editar Ítem' : 'Nuevo Ítem')

@section('breadcrumb')
<li class="breadcrumb-item"><a href="{{ route('checklist-config.index') }}">Config. Checklist</a></li>
<li class="breadcrumb-item active">{{ $item ? 'Editar' : 'Nuevo' }}</li>
@endsection

@push('styles')
<link href="{{ asset('assets/css/mantenimientos.css') }}" rel="stylesheet">
<style>
    .checklist-form-header {
        background: #1a3a5c;
        border-radius: 12px;
        padding: 24px 28px;
        margin-bottom: 24px;
        color: white;
    }

    .checklist-form-header h1 {
        font-size: 1.5rem;
        font-weight: 600;
        margin: 0;
    }

    .checklist-form-header .breadcrumb {
        margin: 8px 0 0;
        padding: 0;
        background: none;
        font-size: 0.9rem;
    }

    .checklist-form-header .breadcrumb a {
        color: rgba(255,255,255,0.7);
        text-decoration: none;
    }

    .checklist-form-header .breadcrumb a:hover {
        color: white;
    }

    .checklist-form-card {
        background: white;
        border-radius: 12px;
        border: 1px solid #e2e8f0;
        overflow: hidden;
    }

    .checklist-form-card .card-header {
        background: #f8fafc;
        padding: 20px 24px;
        border-bottom: 1px solid #e2e8f0;
    }

    .checklist-form-card .card-header h5 {
        margin: 0;
        font-weight: 600;
        color: #1e293b;
    }

    .checklist-form-card .card-body {
        padding: 24px;
    }

    .checklist-form-group {
        margin-bottom: 24px;
    }

    .checklist-form-group label {
        display: block;
        font-weight: 500;
        color: #475569;
        margin-bottom: 8px;
        font-size: 0.9rem;
    }

    .checklist-form-group label .required {
        color: #ef4444;
    }

    .checklist-form-group .form-control,
    .checklist-form-group textarea {
        width: 100%;
        padding: 12px 16px;
        border: 1px solid #e2e8f0;
        border-radius: 10px;
        font-size: 0.95rem;
        transition: border-color 0.2s, box-shadow 0.2s;
    }

    .checklist-form-group .form-control:focus,
    .checklist-form-group textarea:focus {
        outline: none;
        border-color: #1a3a5c;
        box-shadow: 0 0 0 3px rgba(26, 58, 92, 0.1);
    }

    .checklist-form-group .form-control.is-invalid,
    .checklist-form-group textarea.is-invalid {
        border-color: #ef4444;
    }

    .checklist-form-group textarea {
        min-height: 120px;
        resize: vertical;
    }

    .checklist-form-group .form-text {
        font-size: 0.8rem;
        color: #64748b;
        margin-top: 8px;
    }

    .checklist-form-group .invalid-feedback {
        color: #ef4444;
        font-size: 0.85rem;
        margin-top: 6px;
    }

    .checklist-switch-group {
        display: flex;
        align-items: center;
        gap: 16px;
        padding: 20px;
        background: #f8fafc;
        border-radius: 12px;
        border: 1px solid #e2e8f0;
    }

    .checklist-switch-group .switch-label {
        flex: 1;
    }

    .checklist-switch-group .switch-label strong {
        display: block;
        color: #1e293b;
        margin-bottom: 4px;
        font-size: 0.95rem;
    }

    .checklist-switch-group .switch-label small {
        color: #64748b;
        font-size: 0.85rem;
    }

    .checklist-toggle-switch {
        position: relative;
        display: inline-block;
        width: 56px;
        height: 30px;
    }

    .checklist-toggle-switch input {
        opacity: 0;
        width: 0;
        height: 0;
    }

    .checklist-toggle-switch .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #cbd5e1;
        transition: 0.3s;
        border-radius: 30px;
    }

    .checklist-toggle-switch .slider:before {
        position: absolute;
        content: "";
        height: 24px;
        width: 24px;
        left: 3px;
        bottom: 3px;
        background-color: white;
        transition: 0.3s;
        border-radius: 50%;
        box-shadow: 0 2px 4px rgba(0,0,0,0.15);
    }

    .checklist-toggle-switch input:checked + .slider {
        background-color: #22c55e;
    }

    .checklist-toggle-switch input:checked + .slider:before {
        transform: translateX(26px);
    }

    .checklist-toggle-switch.warning input:checked + .slider {
        background-color: #f59e0b;
    }

    .checklist-form-actions {
        display: flex;
        gap: 12px;
        padding-top: 24px;
        border-top: 1px solid #e2e8f0;
        margin-top: 8px;
    }

    .checklist-btn-submit {
        background: #1a3a5c;
        color: white;
        border: none;
        padding: 12px 28px;
        border-radius: 10px;
        font-weight: 500;
        cursor: pointer;
        transition: background 0.2s;
        font-size: 0.95rem;
    }

    .checklist-btn-submit:hover {
        background: #0f2942;
    }

    .checklist-btn-cancel {
        background: #f1f5f9;
        color: #475569;
        border: 1px solid #e2e8f0;
        padding: 12px 28px;
        border-radius: 10px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.2s;
        text-decoration: none;
        font-size: 0.95rem;
    }

    .checklist-btn-cancel:hover {
        background: #e2e8f0;
        color: #1e293b;
    }

    .checklist-preview-box {
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 12px;
        padding: 24px;
    }

    .checklist-preview-box h6 {
        color: #64748b;
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 16px;
    }

    .checklist-preview-item {
        display: flex;
        align-items: flex-start;
        gap: 14px;
        background: white;
        padding: 18px;
        border-radius: 10px;
        border: 1px solid #e2e8f0;
    }

    .checklist-preview-item .checkbox {
        width: 24px;
        height: 24px;
        border: 2px solid #cbd5e1;
        border-radius: 6px;
        flex-shrink: 0;
        margin-top: 2px;
    }

    .checklist-preview-item .content {
        flex: 1;
    }

    .checklist-preview-item .content .title {
        font-weight: 500;
        color: #1e293b;
        font-size: 0.95rem;
        line-height: 1.4;
    }

    .checklist-preview-item .badge {
        margin-left: auto;
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 0.75rem;
        font-weight: 600;
        background: #fef3c7;
        color: #b45309;
        flex-shrink: 0;
    }

    .checklist-info-box {
        background: #eff6ff;
        border: 1px solid #bfdbfe;
        color: #1e40af;
        padding: 16px 20px;
        border-radius: 10px;
        margin-top: 24px;
        font-size: 0.9rem;
    }

    .checklist-info-box strong {
        display: block;
        margin-bottom: 4px;
    }

    .checklist-error-box {
        background: #fef2f2;
        border: 1px solid #fecaca;
        color: #991b1b;
        padding: 16px 20px;
        border-radius: 10px;
        margin-bottom: 20px;
    }

    .checklist-error-box ul {
        margin: 8px 0 0;
        padding-left: 20px;
    }

    @media (max-width: 992px) {
        .row {
            flex-direction: column;
        }

        .col-lg-8, .col-lg-4 {
            width: 100%;
        }
    }
</style>
@endpush

@section('content')
<div class="checklist-form-header">
    <h1>{{ $item ? 'Editar Ítem de Checklist' : 'Nuevo Ítem de Checklist' }}</h1>
    <nav class="breadcrumb">
        <a href="{{ route('checklist-config.index') }}">Configuración de Checklist</a>
        <span style="margin: 0 8px; opacity: 0.5;">/</span>
        {{ $item ? 'Editar' : 'Nuevo' }}
    </nav>
</div>

<div class="row">
    <div class="col-lg-8">
        <div class="checklist-form-card">
            <div class="card-header">
                <h5>{{ $item ? 'Información del Ítem' : 'Datos del Nuevo Ítem' }}</h5>
            </div>
            <div class="card-body">
                @if($errors->any())
                    <div class="checklist-error-box">
                        <strong>Por favor corrija los siguientes errores:</strong>
                        <ul>
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif

                <form action="{{ $item ? route('checklist-config.update', $item->id_item) : route('checklist-config.store') }}" method="POST">
                    @csrf
                    @if($item)
                        @method('PUT')
                    @endif

                    <div class="checklist-form-group">
                        <label>Descripción del Ítem <span class="required">*</span></label>
                        <textarea name="detalle"
                                  class="form-control @error('detalle') is-invalid @enderror"
                                  placeholder="Ej: Verificar estado de conexiones eléctricas y ajustar si es necesario..."
                                  required>{{ old('detalle', $item->detalle ?? '') }}</textarea>
                        @error('detalle')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <div class="form-text">Descripción detallada de lo que el técnico debe revisar o realizar</div>
                    </div>

                    <div class="checklist-form-group">
                        <div class="checklist-switch-group">
                            <div class="switch-label">
                                <strong>Ítem Obligatorio</strong>
                                <small>El técnico debe completar este ítem para finalizar el mantenimiento</small>
                            </div>
                            <label class="checklist-toggle-switch warning">
                                <input type="hidden" name="obligatorio" value="0">
                                <input type="checkbox"
                                       name="obligatorio"
                                       value="1"
                                       {{ old('obligatorio', $item->obligatorio ?? 0) == 1 ? 'checked' : '' }}>
                                <span class="slider"></span>
                            </label>
                        </div>
                    </div>

                    <div class="checklist-form-group">
                        <div class="checklist-switch-group">
                            <div class="switch-label">
                                <strong>Estado Activo</strong>
                                <small>Los ítems inactivos no aparecen en el checklist de la app móvil</small>
                            </div>
                            <label class="checklist-toggle-switch">
                                <input type="hidden" name="estado" value="0">
                                <input type="checkbox"
                                       name="estado"
                                       value="1"
                                       {{ old('estado', $item->estado ?? 1) == 1 ? 'checked' : '' }}>
                                <span class="slider"></span>
                            </label>
                        </div>
                    </div>

                    <div class="checklist-form-actions">
                        <button type="submit" class="checklist-btn-submit">
                            <i class="fas fa-save"></i> {{ $item ? 'Guardar Cambios' : 'Crear Ítem' }}
                        </button>
                        <a href="{{ route('checklist-config.index') }}" class="checklist-btn-cancel">Cancelar</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="checklist-preview-box">
            <h6>Vista Previa en la App</h6>
            <div class="checklist-preview-item">
                <div class="checkbox"></div>
                <div class="content">
                    <div class="title" id="preview-title">{{ $item->detalle ?? 'Descripción del ítem de revisión' }}</div>
                </div>
                <span class="badge" id="preview-badge" style="{{ ($item->obligatorio ?? 0) == 1 ? '' : 'display: none;' }}">Obligatorio</span>
            </div>
        </div>

        <div class="checklist-info-box">
            <strong><i class="fas fa-lightbulb"></i> Consejo</strong>
            Los ítems del checklist deben ser claros y específicos para que el técnico pueda completarlos fácilmente durante el mantenimiento.
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    const detalleInput = document.querySelector('textarea[name="detalle"]');
    const obligatorioInput = document.querySelector('input[name="obligatorio"][type="checkbox"]');

    const previewTitle = document.getElementById('preview-title');
    const previewBadge = document.getElementById('preview-badge');

    function updatePreview() {
        previewTitle.textContent = detalleInput.value || 'Descripción del ítem de revisión';
        previewBadge.style.display = obligatorioInput.checked ? 'block' : 'none';
    }

    detalleInput.addEventListener('input', updatePreview);
    obligatorioInput.addEventListener('change', updatePreview);

    updatePreview();
});
</script>
@endpush
