@extends('layouts.app')

@section('title', 'Vista Previa del Checklist')

@push('styles')
<style>
    .page-header {
        background: #1a3a5c;
        border-radius: 12px;
        padding: 24px 28px;
        margin-bottom: 24px;
        color: white;
    }

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

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

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

    .btn-secondary {
        background: rgba(255,255,255,0.15);
        color: white;
        border: 1px solid rgba(255,255,255,0.3);
        padding: 10px 20px;
        border-radius: 8px;
        font-weight: 500;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }

    .btn-secondary:hover {
        background: rgba(255,255,255,0.25);
        color: white;
    }

    .phone-mockup {
        max-width: 375px;
        margin: 0 auto;
        background: #f8fafc;
        border-radius: 40px;
        box-shadow: 0 20px 60px rgba(0,0,0,0.15);
        overflow: hidden;
        border: 8px solid #1e293b;
    }

    .phone-status-bar {
        background: #1a3a5c;
        padding: 12px 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: white;
        font-size: 0.8rem;
    }

    .phone-header {
        background: #1a3a5c;
        padding: 16px 20px;
        display: flex;
        align-items: center;
        gap: 16px;
        color: white;
    }

    .phone-header .back-btn {
        font-size: 1.2rem;
    }

    .phone-header h2 {
        font-size: 1.1rem;
        font-weight: 600;
        margin: 0;
    }

    .phone-content {
        padding: 0;
        max-height: 600px;
        overflow-y: auto;
    }

    .progress-section {
        background: white;
        padding: 16px 20px;
        border-bottom: 1px solid #e2e8f0;
    }

    .progress-text {
        display: flex;
        justify-content: space-between;
        font-size: 0.85rem;
        color: #64748b;
        margin-bottom: 8px;
    }

    .progress-text strong {
        color: #1a3a5c;
    }

    .progress-bar-bg {
        height: 8px;
        background: #e2e8f0;
        border-radius: 4px;
        overflow: hidden;
    }

    .progress-bar-fill {
        height: 100%;
        background: #1a3a5c;
        border-radius: 4px;
        width: 0%;
        transition: width 0.3s;
    }

    .checklist-section {
        padding: 12px 0;
    }

    .section-header {
        background: #f1f5f9;
        padding: 10px 20px;
        font-size: 0.75rem;
        font-weight: 600;
        color: #475569;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .checklist-item {
        display: flex;
        align-items: flex-start;
        gap: 14px;
        padding: 16px 20px;
        background: white;
        border-bottom: 1px solid #f1f5f9;
        cursor: pointer;
        transition: background 0.2s;
    }

    .checklist-item:hover {
        background: #f8fafc;
    }

    .checklist-item.checked {
        background: #f0fdf4;
    }

    .checklist-item .checkbox {
        width: 24px;
        height: 24px;
        border: 2px solid #cbd5e1;
        border-radius: 6px;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.2s;
    }

    .checklist-item.checked .checkbox {
        background: #22c55e;
        border-color: #22c55e;
        color: white;
    }

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

    .checklist-item .title {
        font-weight: 500;
        color: #1e293b;
        font-size: 0.95rem;
    }

    .checklist-item .desc {
        font-size: 0.8rem;
        color: #64748b;
        margin-top: 4px;
        line-height: 1.4;
    }

    .checklist-item .badge {
        padding: 4px 10px;
        border-radius: 20px;
        font-size: 0.7rem;
        font-weight: 500;
        background: #fef3c7;
        color: #b45309;
        margin-left: 8px;
    }

    .checklist-item .status {
        font-size: 0.75rem;
        color: #22c55e;
        text-align: right;
    }

    .phone-footer {
        background: white;
        padding: 16px 20px;
        border-top: 1px solid #e2e8f0;
        display: flex;
        gap: 12px;
    }

    .phone-btn {
        flex: 1;
        padding: 14px;
        border-radius: 10px;
        text-align: center;
        font-weight: 500;
        font-size: 0.9rem;
        cursor: pointer;
    }

    .phone-btn.secondary {
        background: #f1f5f9;
        color: #475569;
        border: 1px solid #e2e8f0;
    }

    .phone-btn.primary {
        background: #1a3a5c;
        color: white;
        border: none;
    }

    .info-card {
        background: white;
        border-radius: 12px;
        padding: 24px;
        border: 1px solid #e2e8f0;
        margin-top: 30px;
    }

    .info-card h5 {
        font-size: 1rem;
        font-weight: 600;
        color: #1e293b;
        margin-bottom: 16px;
    }

    .info-card ul {
        margin: 0;
        padding-left: 20px;
    }

    .info-card li {
        color: #64748b;
        margin-bottom: 8px;
        font-size: 0.9rem;
    }

    .empty-state {
        text-align: center;
        padding: 60px 20px;
        color: #64748b;
    }

    .empty-state .icon {
        font-size: 3rem;
        margin-bottom: 16px;
    }
</style>


@section('content')
<div class="page-header">
    <div class="d-flex justify-content-between align-items-center">
        <div>
            <h1>Vista Previa del 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>
                Vista Previa
            </nav>
        </div>
        <a href="{{ route('checklist-config.index') }}" class="btn-secondary">
            Volver
        </a>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-md-5">
        <div class="phone-mockup">
            <div class="phone-status-bar">
                <span>9:41</span>
                <span>100%</span>
            </div>
            <div class="phone-header">
                <span class="back-btn">&larr;</span>
                <h2>Checklist de Mantenimiento</h2>
            </div>

            @if($items->count() > 0)
                <div class="progress-section">
                    <div class="progress-text">
                        <span>Progreso: <strong id="progress-count">0</strong> de {{ $items->count() }} ítems</span>
                        <strong id="progress-percent">0%</strong>
                    </div>
                    <div class="progress-bar-bg">
                        <div class="progress-bar-fill" id="progress-bar"></div>
                    </div>
                </div>

                <div class="phone-content">
                    <div class="checklist-section">
                        <div class="section-header">Ítems de Revisión</div>

                        @foreach($items as $item)
                            <div class="checklist-item" data-id="{{ $item->id }}" onclick="toggleItem(this)">
                                <div class="checkbox">
                                    <span style="display: none;">&check;</span>
                                </div>
                                <div class="content">
                                    <div class="title">
                                        {{ $item->nombre }}
                                        @if($item->obligatorio)
                                            <span class="badge">Obligatorio</span>
                                        @endif
                                    </div>
                                    @if($item->descripcion)
                                        <div class="desc">{{ $item->descripcion }}</div>
                                    @endif
                                </div>
                                <div class="status" style="display: none;">Completado</div>
                            </div>
                        @endforeach
                    </div>
                </div>

                <div class="phone-footer">
                    <div class="phone-btn secondary">Foto</div>
                    <div class="phone-btn secondary">Nota</div>
                    <div class="phone-btn primary">Continuar</div>
                </div>
            @else
                <div class="phone-content">
                    <div class="empty-state">
                        <div class="icon">&#128203;</div>
                        <p>No hay ítems configurados en el checklist</p>
                    </div>
                </div>
            @endif
        </div>
    </div>

    <div class="col-md-4">
        <div class="info-card">
            <h5>Cómo funciona el Checklist</h5>
            <ul>
                <li>El técnico ve esta lista al ejecutar un mantenimiento</li>
                <li>Debe completar todos los ítems <strong>obligatorios</strong> para poder finalizar</li>
                <li>Puede agregar fotos y observaciones a cada ítem</li>
                <li>El progreso se guarda automáticamente</li>
            </ul>
        </div>

        <div class="info-card">
            <h5>Estadísticas</h5>
            <ul>
                <li><strong>{{ $items->count() }}</strong> ítems activos en el checklist</li>
                <li><strong>{{ $items->where('obligatorio', true)->count() }}</strong> ítems obligatorios</li>
                <li><strong>{{ $items->where('obligatorio', false)->count() }}</strong> ítems opcionales</li>
            </ul>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
const totalItems = {{ $items->count() }};
let checkedItems = 0;

function toggleItem(element) {
    const checkbox = element.querySelector('.checkbox');
    const checkmark = checkbox.querySelector('span');
    const status = element.querySelector('.status');

    if (element.classList.contains('checked')) {
        element.classList.remove('checked');
        checkmark.style.display = 'none';
        status.style.display = 'none';
        checkedItems--;
    } else {
        element.classList.add('checked');
        checkmark.style.display = 'block';
        status.style.display = 'block';
        checkedItems++;
    }

    updateProgress();
}

function updateProgress() {
    const percent = Math.round((checkedItems / totalItems) * 100);
    document.getElementById('progress-count').textContent = checkedItems;
    document.getElementById('progress-percent').textContent = percent + '%';
    document.getElementById('progress-bar').style.width = percent + '%';
}
</script>
@endsection
