<?php $__env->startSection('title', 'Soporte | Veltrix v2'); ?>
<?php $__env->startSection('page_title', 'Soporte'); ?>
<?php $__env->startSection('page_subtitle', 'Gestion operativa en tiempo real'); ?>
<?php $__env->startSection('page_icon', 'bx bx-menu-alt-left'); ?>

<?php $__env->startSection('content'); ?>
    <?php
        $ticketRows = $tickets instanceof \Illuminate\Support\Collection ? $tickets : collect($tickets ?? []);
        $eventMap = is_array($eventsByTicket ?? null) ? $eventsByTicket : [];
        $oldPriority = (string) old('prioridad', 'media');
    ?>

    <div class="container-fluid px-4 py-4">
        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($errors->any()): ?>
            <div class="alert alert-danger mb-3">
                <?php echo e($errors->first()); ?>

            </div>
        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

        <div class="row g-3 mb-4">
            <div class="col-lg-5">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <h2 class="h5 mb-3">Crear Ticket de Soporte</h2>
                        <form action="<?php echo e(route('soporte.tickets.store')); ?>" method="post" enctype="multipart/form-data">
                            <?php echo csrf_field(); ?>
                            <div class="mb-2">
                                <label class="form-label">Asunto</label>
                                <input class="form-control" name="asunto" maxlength="180" value="<?php echo e((string) old('asunto', '')); ?>" required>
                            </div>
                            <div class="mb-2">
                                <label class="form-label">Prioridad</label>
                                <select class="form-select" name="prioridad" required>
                                    <option value="baja" <?php if($oldPriority === 'baja'): echo 'selected'; endif; ?>>Baja</option>
                                    <option value="media" <?php if($oldPriority === 'media'): echo 'selected'; endif; ?>>Media</option>
                                    <option value="alta" <?php if($oldPriority === 'alta'): echo 'selected'; endif; ?>>Alta</option>
                                    <option value="critica" <?php if($oldPriority === 'critica'): echo 'selected'; endif; ?>>Crítica</option>
                                </select>
                            </div>
                            <div class="mb-2">
                                <label class="form-label">Descripción</label>
                                <textarea class="form-control" name="descripcion" rows="4" maxlength="2000" required><?php echo e((string) old('descripcion', '')); ?></textarea>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Imagen adjunta (opcional)</label>
                                <input type="file" class="form-control" name="adjunto" accept="image/png,image/jpeg,image/webp,image/gif">
                                <small class="text-muted">Formatos: JPG, PNG, WEBP, GIF. Máximo 5MB.</small>
                            </div>
                            <button class="btn btn-primary w-100" type="submit">Enviar ticket</button>
                        </form>
                    </div>
                </div>
            </div>

            <div class="col-lg-7">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-body">
                        <h2 class="h5 mb-2">¿Cómo funciona?</h2>
                        <p class="text-muted mb-2">
                            Crea el ticket desde tu empresa y el superadministrador lo verá en su panel de soporte para responder y resolver.
                        </p>
                        <ul class="mb-0 text-muted">
                            <li>Incluye asunto claro y descripción detallada.</li>
                            <li>Adjunta imagen cuando aplique para acelerar la solución.</li>
                            <li>Usa comentarios para seguimiento del caso.</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h1 class="h4 mb-3">Mis Tickets</h1>
                <div class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Asunto</th>
                                <th>Estado / Prioridad</th>
                                <th>Asignado</th>
                                <th>Actualizado</th>
                                <th>Acción</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__empty_1 = true; $__currentLoopData = $ticketRows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ticket): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                                <?php
                                    $ticketId = (int) data_get($ticket, 'id', 0);
                                    $status = (string) data_get($ticket, 'estado', 'abierto');
                                    $priority = (string) data_get($ticket, 'prioridad', 'media');
                                    $statusClass = match ($status) {
                                        'abierto' => 'text-bg-warning',
                                        'en_proceso' => 'text-bg-primary',
                                        'resuelto', 'cerrado' => 'text-bg-success',
                                        default => 'text-bg-secondary',
                                    };
                                    $priorityClass = match ($priority) {
                                        'critica' => 'text-bg-danger',
                                        'alta' => 'text-bg-warning',
                                        'media' => 'text-bg-primary',
                                        default => 'text-bg-secondary',
                                    };
                                    $events = is_array($eventMap[$ticketId] ?? null) ? $eventMap[$ticketId] : [];
                                ?>

                                <tr>
                                    <td>#<?php echo e($ticketId); ?></td>
                                    <td>
                                        <strong><?php echo e((string) data_get($ticket, 'asunto', '')); ?></strong>
                                        <small class="d-block text-muted text-truncate" style="max-width: 320px;">
                                            <?php echo e((string) data_get($ticket, 'descripcion', '')); ?>

                                        </small>
                                    </td>
                                    <td>
                                        <span class="badge <?php echo e($statusClass); ?>"><?php echo e($status); ?></span>
                                        <span class="badge <?php echo e($priorityClass); ?>"><?php echo e($priority); ?></span>
                                    </td>
                                    <td><?php echo e((string) data_get($ticket, 'asignado_a', 'Sin asignar')); ?></td>
                                    <td><?php echo e((string) data_get($ticket, 'updated_at', '-')); ?></td>
                                    <td>
                                        <button class="btn btn-sm btn-outline-primary" type="button" data-bs-toggle="collapse" data-bs-target="#supportTicket<?php echo e($ticketId); ?>">
                                            Ver detalle
                                        </button>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="6" class="bg-light">
                                        <div class="collapse" id="supportTicket<?php echo e($ticketId); ?>">
                                            <div class="p-2 border rounded bg-white mb-2">
                                                <form action="<?php echo e(route('soporte.tickets.comment', ['ticket' => $ticketId])); ?>" method="post" enctype="multipart/form-data" class="row g-2">
                                                    <?php echo csrf_field(); ?>
                                                    <div class="col-md-8">
                                                        <label class="form-label">Comentario para soporte</label>
                                                        <input class="form-control" name="mensaje" maxlength="1500" placeholder="Escribe actualización del caso...">
                                                    </div>
                                                    <div class="col-md-3">
                                                        <label class="form-label">Imagen (opcional)</label>
                                                        <input type="file" class="form-control" name="adjunto" accept="image/png,image/jpeg,image/webp,image/gif">
                                                    </div>
                                                    <div class="col-md-1 d-flex align-items-end">
                                                        <button class="btn btn-sm btn-outline-primary w-100" type="submit">Enviar</button>
                                                    </div>
                                                </form>
                                            </div>

                                            <div class="p-2 border rounded bg-white">
                                                <strong class="d-block mb-2">Historial</strong>
                                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($events !== []): ?>
                                                    <ul class="list-group list-group-flush">
                                                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__currentLoopData = array_slice($events, 0, 15); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $event): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                                                            <?php
                                                                $attachment = is_array(data_get($event, 'adjunto')) ? data_get($event, 'adjunto') : null;
                                                                $message = trim((string) data_get($event, 'mensaje_texto', ''));
                                                                if ($message === '') {
                                                                    $message = 'Adjunto sin comentario.';
                                                                }
                                                            ?>
                                                            <li class="list-group-item px-0 py-2">
                                                                <div class="d-flex justify-content-between gap-2">
                                                                    <span>
                                                                        <span class="badge text-bg-light border me-1"><?php echo e((string) data_get($event, 'tipo', '-')); ?></span>
                                                                        <?php echo e($message); ?>

                                                                    </span>
                                                                    <small class="text-muted text-nowrap"><?php echo e((string) data_get($event, 'created_at', '-')); ?></small>
                                                                </div>
                                                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($attachment): ?>
                                                                    <div class="mt-1">
                                                                        <a href="<?php echo e((string) data_get($attachment, 'url', '#')); ?>" target="_blank" rel="noopener">
                                                                            Ver adjunto: <?php echo e((string) data_get($attachment, 'name', 'imagen')); ?>

                                                                        </a>
                                                                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if((bool) data_get($attachment, 'is_image', false)): ?>
                                                                            <div class="mt-2">
                                                                                <img
                                                                                    src="<?php echo e((string) data_get($attachment, 'url', '')); ?>"
                                                                                    alt="<?php echo e((string) data_get($attachment, 'name', 'Adjunto')); ?>"
                                                                                    style="max-width: 220px; width: 100%; border: 1px solid #dce4f4; border-radius: 8px;"
                                                                                >
                                                                            </div>
                                                                        <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                                                                    </div>
                                                                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                                                                <small class="text-muted d-block">
                                                                    <?php echo e((string) data_get($event, 'usuario', 'Sistema')); ?>

                                                                    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if((string) data_get($event, 'estado_anterior', '') !== '' || (string) data_get($event, 'estado_nuevo', '') !== ''): ?>
                                                                        | Estado: <?php echo e((string) data_get($event, 'estado_anterior', '-')); ?> -> <?php echo e((string) data_get($event, 'estado_nuevo', '-')); ?>

                                                                    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                                                                </small>
                                                            </li>
                                                        <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::endLoop(); ?><?php endif; ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::closeLoop(); ?><?php endif; ?>
                                                    </ul>
                                                <?php else: ?>
                                                    <small class="text-muted">Sin eventos todavía.</small>
                                                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::endLoop(); ?><?php endif; ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::closeLoop(); ?><?php endif; ?>
                                <tr>
                                    <td colspan="6" class="text-center text-muted py-4">No hay tickets creados todavía.</td>
                                </tr>
                            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Applications/XAMPP/xamppfiles/htdocs/veltrix-v2/resources/views/modules/soporte/index.blade.php ENDPATH**/ ?>