<?php $__env->startSection('title', 'Nuevo Envío | Veltrix v2'); ?>

<?php $__env->startSection('content'); ?>

<div class="d-flex align-items-center gap-2 mb-3">
    <a href="<?php echo e(route('envios.index')); ?>" class="btn btn-sm btn-outline-secondary"><i class="bx bx-arrow-back"></i></a>
    <h1 class="h5 mb-0">Nuevo envío</h1>
    <span class="badge bg-light text-secondary border"><?php echo e($nextNumero); ?></span>
</div>

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

<form method="POST" action="<?php echo e(route('envios.store')); ?>">
    <?php echo csrf_field(); ?>
    <div class="row g-3">

        
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-transparent border-0 pb-0 pt-3 px-4">
                    <h2 class="h6 mb-0"><i class="bx bxs-map-pin me-2 text-primary"></i>Destinatario</h2>
                </div>
                <div class="card-body px-4">
                    
                    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($clientes->count()): ?>
                        <div class="mb-3">
                            <label class="form-label form-label-sm">Cargar desde cliente (opcional)</label>
                            <select id="selectCliente" name="cliente_id" class="form-select form-select-sm">
                                <option value="">— Seleccionar cliente —</option>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__currentLoopData = $clientes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                                    <option value="<?php echo e($c->id); ?>"
                                        data-nombre="<?php echo e($c->nombre); ?>"
                                        data-telefono="<?php echo e($c->telefono); ?>"
                                        data-direccion="<?php echo e($c->direccion); ?>"
                                        <?php if(old('cliente_id') == $c->id): echo 'selected'; endif; ?>
                                    ><?php echo e($c->nombre); ?></option>
                                <?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; ?>
                            </select>
                        </div>
                        <hr class="my-2">
                    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>

                    <div class="mb-2">
                        <label class="form-label form-label-sm">Nombre *</label>
                        <input class="form-control form-control-sm" id="destNombre" name="destinatario_nombre" value="<?php echo e(old('destinatario_nombre')); ?>" required>
                    </div>
                    <div class="mb-2">
                        <label class="form-label form-label-sm">Teléfono</label>
                        <input class="form-control form-control-sm" id="destTelefono" name="destinatario_telefono" value="<?php echo e(old('destinatario_telefono')); ?>">
                    </div>
                    <div class="mb-2">
                        <label class="form-label form-label-sm">Dirección *</label>
                        <input class="form-control form-control-sm" id="destDireccion" name="destinatario_direccion" value="<?php echo e(old('destinatario_direccion')); ?>" required>
                    </div>
                    <div class="row g-2">
                        <div class="col-6">
                            <label class="form-label form-label-sm">Ciudad *</label>
                            <input class="form-control form-control-sm" name="destinatario_ciudad" value="<?php echo e(old('destinatario_ciudad')); ?>" required>
                        </div>
                        <div class="col-6">
                            <label class="form-label form-label-sm">Departamento</label>
                            <input class="form-control form-control-sm" name="destinatario_departamento" value="<?php echo e(old('destinatario_departamento')); ?>">
                        </div>
                    </div>
                    <div class="mt-2">
                        <label class="form-label form-label-sm">País</label>
                        <input class="form-control form-control-sm" name="destinatario_pais" value="<?php echo e(old('destinatario_pais', 'Colombia')); ?>">
                    </div>
                </div>
            </div>
        </div>

        
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-transparent border-0 pb-0 pt-3 px-4">
                    <h2 class="h6 mb-0"><i class="bx bxs-store me-2 text-success"></i>Remitente</h2>
                </div>
                <div class="card-body px-4">
                    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($empresa): ?>
                        <div class="alert alert-info py-2 d-flex align-items-center gap-2 mb-3" style="font-size:.8rem">
                            <i class="bx bx-info-circle fs-5"></i>
                            <span>Datos pre-llenados desde tu empresa. Puedes editarlos.</span>
                        </div>
                    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                    <div class="mb-2">
                        <label class="form-label form-label-sm">Nombre *</label>
                        <input class="form-control form-control-sm" name="remitente_nombre"
                               value="<?php echo e(old('remitente_nombre', $empresa?->nombre ?? '')); ?>" required>
                    </div>
                    <div class="mb-2">
                        <label class="form-label form-label-sm">Teléfono</label>
                        <input class="form-control form-control-sm" name="remitente_telefono"
                               value="<?php echo e(old('remitente_telefono', $empresa?->telefono ?? '')); ?>">
                    </div>
                    <div class="mb-2">
                        <label class="form-label form-label-sm">Dirección</label>
                        <input class="form-control form-control-sm" name="remitente_direccion"
                               value="<?php echo e(old('remitente_direccion', $empresa?->direccion ?? '')); ?>">
                    </div>
                    <div class="mb-2">
                        <label class="form-label form-label-sm">Ciudad</label>
                        <input class="form-control form-control-sm" name="remitente_ciudad"
                               value="<?php echo e(old('remitente_ciudad', '')); ?>">
                    </div>
                </div>
            </div>
        </div>

        
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-transparent border-0 pb-0 pt-3 px-4">
                    <h2 class="h6 mb-0"><i class="bx bxs-package me-2 text-warning"></i>Contenido y logística</h2>
                </div>
                <div class="card-body px-4">
                    <div class="mb-2">
                        <label class="form-label form-label-sm">Descripción del contenido *</label>
                        <input class="form-control form-control-sm" name="descripcion_contenido"
                               value="<?php echo e(old('descripcion_contenido')); ?>" placeholder="Ej: Ropa, Electrónico, Documentos…" required>
                    </div>
                    <div class="row g-2">
                        <div class="col-6">
                            <label class="form-label form-label-sm">Peso (kg)</label>
                            <input type="number" step="0.001" min="0" class="form-control form-control-sm" name="peso_kg" value="<?php echo e(old('peso_kg', '0')); ?>">
                        </div>
                        <div class="col-6">
                            <label class="form-label form-label-sm">Valor declarado</label>
                            <input type="number" step="0.01" min="0" class="form-control form-control-sm" name="valor_declarado" value="<?php echo e(old('valor_declarado')); ?>" placeholder="Opcional">
                        </div>
                    </div>
                    <div class="row g-2 mt-1">
                        <div class="col-6">
                            <label class="form-label form-label-sm">Transportadora</label>
                            <input class="form-control form-control-sm" name="transportadora" value="<?php echo e(old('transportadora')); ?>" placeholder="Ej: Servientrega, DHL…">
                        </div>
                        <div class="col-6">
                            <label class="form-label form-label-sm">N° de guía</label>
                            <input class="form-control form-control-sm" name="numero_guia" value="<?php echo e(old('numero_guia')); ?>">
                        </div>
                    </div>
                    <div class="mt-2">
                        <label class="form-label form-label-sm">Observaciones</label>
                        <textarea class="form-control form-control-sm" name="observaciones" rows="2"><?php echo e(old('observaciones')); ?></textarea>
                    </div>
                    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($pedidos->count()): ?>
                        <div class="mt-2">
                            <label class="form-label form-label-sm">Vincular pedido WhatsApp (opcional)</label>
                            <select name="pedido_manychat_id" class="form-select form-select-sm">
                                <option value="">— Ninguno —</option>
                                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__currentLoopData = $pedidos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                                    <option value="<?php echo e($p->id); ?>" <?php if(old('pedido_manychat_id') == $p->id): echo 'selected'; endif; ?>>#<?php echo e($p->id); ?> — <?php echo e($p->cliente_nombre); ?></option>
                                <?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; ?>
                            </select>
                        </div>
                    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                </div>
            </div>
        </div>

        
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-transparent border-0 pb-0 pt-3 px-4">
                    <h2 class="h6 mb-0"><i class="bx bx-qr me-2 text-info"></i>Rótulo de envío</h2>
                </div>
                <div class="card-body px-4">
                    <div class="form-check form-switch mb-3">
                        <input class="form-check-input" type="checkbox" id="genRotulo" name="generar_rotulo" value="1" <?php if(old('generar_rotulo', true)): echo 'checked'; endif; ?>>
                        <label class="form-check-label fw-semibold" for="genRotulo">Generar rótulo al crear</label>
                    </div>
                    <div id="formatoSection">
                        <label class="form-label form-label-sm">Formato del rótulo</label>
                        <div class="row g-2">
                            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__currentLoopData = $formatos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                                <div class="col-6">
                                    <label class="d-block border rounded p-2 text-center cursor-pointer <?php echo e(old('formato_rotulo', '10x15') === $key ? 'border-primary bg-primary bg-opacity-10' : ''); ?>"
                                           style="cursor:pointer;transition:all .15s" id="fmt-label-<?php echo e($key); ?>">
                                        <input type="radio" name="formato_rotulo" value="<?php echo e($key); ?>"
                                               class="d-none vx-fmt-radio"
                                               <?php if(old('formato_rotulo', '10x15') === $key): echo 'checked'; endif; ?>>
                                        <i class="bx bx-receipt d-block fs-4 mb-1 <?php echo e(old('formato_rotulo', '10x15') === $key ? 'text-primary' : 'text-muted'); ?>"></i>
                                        <span class="fw-semibold" style="font-size:.78rem"><?php echo e($label); ?></span>
                                    </label>
                                </div>
                            <?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; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <div class="d-flex gap-2 justify-content-end mt-3">
        <a href="<?php echo e(route('envios.index')); ?>" class="btn btn-outline-secondary">Cancelar</a>
        <button type="submit" class="btn btn-primary px-4"><i class="bx bx-save me-1"></i>Crear envío</button>
    </div>
</form>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
(function () {
    // Pre-fill from cliente selector
    const sel = document.getElementById('selectCliente');
    if (sel) {
        sel.addEventListener('change', function () {
            const opt = this.options[this.selectedIndex];
            if (opt.value) {
                const d = opt.dataset;
                document.getElementById('destNombre').value    = d.nombre || '';
                document.getElementById('destTelefono').value  = d.telefono || '';
                document.getElementById('destDireccion').value = d.direccion || '';
            }
        });
    }

    // Toggle formato section
    const toggle = document.getElementById('genRotulo');
    const section = document.getElementById('formatoSection');
    if (toggle && section) {
        const update = () => { section.style.opacity = toggle.checked ? '1' : '.4'; section.style.pointerEvents = toggle.checked ? 'auto' : 'none'; };
        toggle.addEventListener('change', update);
        update();
    }

    // Format radio UI
    document.querySelectorAll('.vx-fmt-radio').forEach(radio => {
        radio.addEventListener('change', function () {
            document.querySelectorAll('.vx-fmt-radio').forEach(r => {
                const lbl = document.getElementById('fmt-label-' + r.value);
                if (lbl) {
                    lbl.classList.toggle('border-primary', r.checked);
                    lbl.classList.toggle('bg-primary', r.checked);
                    lbl.classList.toggle('bg-opacity-10', r.checked);
                    const icon = lbl.querySelector('i');
                    if (icon) { icon.classList.toggle('text-primary', r.checked); icon.classList.toggle('text-muted', !r.checked); }
                }
            });
        });
    });
})();
</script>
<?php $__env->stopPush(); ?>

<?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/envios/create.blade.php ENDPATH**/ ?>