

<?php $__env->startSection('title', 'Produtos'); ?>

<?php $__env->startSection('content'); ?>
<div class="max-w-7xl mx-auto">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between mb-8">
        <div>
            <h2 class="text-4xl font-bold text-white mb-2 tracking-tight">Produtos</h2>
            <p class="text-slate-400">Gerencie seus produtos para notificações</p>
        </div>
        <button onclick="openModal()" class="mt-4 sm:mt-0 px-6 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-all duration-200 font-semibold flex items-center space-x-2">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
            </svg>
            <span>Adicionar Produto</span>
        </button>
    </div>

    <?php if(isset($credenciais) && $credenciais->count() == 0): ?>
        <div class="bg-slate-900 border border-yellow-900/50 rounded-lg p-6 mb-6">
            <div class="flex items-start gap-4">
                <div class="w-10 h-10 bg-yellow-500/20 rounded-xl flex items-center justify-center flex-shrink-0">
                    <svg class="w-5 h-5 text-yellow-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
                    </svg>
                </div>
                <div>
                    <h3 class="text-yellow-300 font-bold mb-1 text-lg">Nenhuma credencial cadastrada</h3>
                    <p class="text-yellow-200/80">Você precisa cadastrar uma credencial antes de adicionar produtos. <a href="<?php echo e(route('credencial')); ?>" class="font-semibold underline hover:text-yellow-100 transition">Clique aqui para cadastrar</a>.</p>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <?php if(isset($produtos) && $produtos->count() > 0): ?>
        <div class="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
            <?php $__currentLoopData = $produtos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $produto): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <div class="bg-slate-900 border border-slate-800 rounded-lg overflow-hidden hover:border-slate-700 transition-all">
                    <div class="h-32 bg-slate-800 flex items-center justify-center">
                        <svg class="w-16 h-16 text-slate-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
                        </svg>
                    </div>
                    <div class="p-6">
                        <h3 class="text-xl font-bold text-white mb-3"><?php echo e($produto->nome); ?></h3>
                        <div class="mb-4 bg-slate-900/30 p-3 rounded-xl">
                            <p class="text-xs text-slate-500 mb-1">Token vinculado:</p>
                            <p class="text-sm text-slate-300 font-semibold"><?php echo e($produto->credencial->nome); ?></p>
                        </div>
                        <div class="flex items-center justify-between mb-5">
                            <span class="text-3xl font-bold text-green-400">R$ <?php echo e(number_format($produto->preco, 2, ',', '.')); ?></span>
                        </div>
                        <div class="flex gap-2">
                            <button onclick="openEditModal(<?php echo e($produto->id); ?>, <?php echo e($produto->credencial_id); ?>, '<?php echo e(addslashes($produto->nome)); ?>', '<?php echo e($produto->preco); ?>')" class="flex-1 px-4 py-2.5 bg-blue-600/90 hover:bg-blue-600 text-white rounded-xl transition-all text-sm font-semibold shadow-lg">
                                Editar
                            </button>
                            <form method="POST" action="<?php echo e(route('produtos.destroy', $produto)); ?>" onsubmit="return confirm('Tem certeza que deseja excluir este produto?')" class="flex-1">
                                <?php echo csrf_field(); ?>
                                <?php echo method_field('DELETE'); ?>
                                <button type="submit" class="w-full px-4 py-2.5 bg-red-600/90 hover:bg-red-600 text-white rounded-xl transition-all text-sm font-semibold shadow-lg">
                                    Excluir
                                </button>
                            </form>
                        </div>
                        <p class="text-slate-500 text-xs mt-4">Cadastrado em <?php echo e($produto->created_at->format('d/m/Y H:i')); ?></p>
                    </div>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
    <?php else: ?>
        <?php if(isset($credenciais) && $credenciais->count() > 0): ?>
            <div class="bg-slate-900 border border-slate-800 rounded-xl p-16 text-center">
                <div class="w-24 h-24 bg-slate-800 rounded-xl flex items-center justify-center mx-auto mb-6">
                    <svg class="w-12 h-12 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
                    </svg>
                </div>
                <h3 class="text-2xl font-bold text-white mb-3">Nenhum produto cadastrado</h3>
                <p class="text-slate-400 mb-8 max-w-md mx-auto">Adicione seu primeiro produto para começar a disparar notificações</p>
                <button onclick="openModal()" class="px-8 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-all duration-200 font-semibold inline-flex items-center space-x-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                    </svg>
                    <span>Adicionar Primeiro Produto</span>
                </button>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>

<div id="produtoModal" class="hidden fixed inset-0 bg-black/60 backdrop-blur-sm z-50 flex items-center justify-center p-4">
    <div class="bg-slate-900 rounded-xl max-w-md w-full p-8 border border-slate-800 animate-slide-in">
        <div class="flex items-center justify-between mb-8">
            <div>
                <h3 class="text-2xl font-bold text-white mb-1" id="modalTitle">Adicionar Produto</h3>
                <p class="text-slate-400 text-sm">Configure seu produto</p>
            </div>
            <button onclick="closeModal()" class="text-gray-400 hover:text-white transition p-2 hover:bg-slate-700/50 rounded-lg">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                </svg>
            </button>
        </div>

        <form id="produtoForm" method="POST" action="<?php echo e(route('produtos.store')); ?>">
            <?php echo csrf_field(); ?>
            <input type="hidden" id="produtoId" name="produto_id" value="">
            <input type="hidden" id="formMethod" name="_method" value="">

            <div class="space-y-6">
                <div>
                    <label for="credencial_id" class="block text-sm font-semibold text-slate-300 mb-2">Token Umtify</label>
                    <select
                        id="credencial_id"
                        name="credencial_id"
                        required
                        class="w-full px-4 py-3 bg-slate-700/50 border border-slate-600 text-white rounded-xl focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none transition"
                    >
                        <option value="">Selecione uma credencial</option>
                        <?php if(isset($credenciais)): ?>
                            <?php $__currentLoopData = $credenciais; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $credencial): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($credencial->id); ?>"><?php echo e($credencial->nome); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        <?php endif; ?>
                    </select>
                </div>

                <div>
                    <label for="nome" class="block text-sm font-semibold text-slate-300 mb-2">Nome do Produto</label>
                    <input
                        type="text"
                        id="nome"
                        name="nome"
                        required
                        class="w-full px-4 py-3 bg-slate-700/50 border border-slate-600 text-white rounded-xl focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none transition"
                        placeholder="Ex: Produto Premium"
                    >
                </div>

                <div>
                    <label for="preco" class="block text-sm font-semibold text-slate-300 mb-2">Preço do Produto</label>
                    <div class="relative">
                        <span class="absolute left-4 top-1/2 -translate-y-1/2 text-slate-400 font-semibold">R$</span>
                        <input
                            type="text"
                            id="preco"
                            name="preco"
                            required
                            class="w-full pl-12 pr-4 py-3 bg-slate-700/50 border border-slate-600 text-white rounded-xl focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none transition"
                            placeholder="0,00"
                        >
                    </div>
                </div>

                <div class="flex gap-4 pt-6">
                    <button
                        type="button"
                        onclick="closeModal()"
                        class="flex-1 px-6 py-3 bg-slate-800 text-white rounded-lg hover:bg-slate-700 transition-all duration-200 font-semibold"
                    >
                        Cancelar
                    </button>
                    <button
                        type="submit"
                        class="flex-1 px-6 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-all duration-200 font-semibold"
                    >
                        Cadastrar
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    function formatPrice(input) {
        let value = input.value.replace(/\D/g, '');

        if (value === '') {
            input.value = '';
            return;
        }

        value = parseInt(value).toString();

        while (value.length < 3) {
            value = '0' + value;
        }

        const integerPart = value.slice(0, -2);
        const decimalPart = value.slice(-2);

        const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, '.');

        input.value = formattedInteger + ',' + decimalPart;
    }

    document.addEventListener('DOMContentLoaded', function() {
        const precoInput = document.getElementById('preco');

        precoInput.addEventListener('input', function(e) {
            formatPrice(e.target);
        });

        precoInput.addEventListener('keypress', function(e) {
            if (!/[\d]/.test(e.key) && e.key !== 'Backspace' && e.key !== 'Delete' && e.key !== 'Tab') {
                e.preventDefault();
            }
        });
    });

    function openModal() {
        document.getElementById('modalTitle').textContent = 'Adicionar Produto';
        document.getElementById('produtoForm').action = '<?php echo e(route("produtos.store")); ?>';
        document.getElementById('formMethod').value = '';
        document.getElementById('produtoId').value = '';
        document.getElementById('credencial_id').value = '';
        document.getElementById('nome').value = '';
        document.getElementById('preco').value = '';
        document.getElementById('produtoModal').classList.remove('hidden');
        document.querySelector('#produtoForm button[type="submit"]').textContent = 'Cadastrar';
    }

    function openEditModal(id, credencialId, nome, preco) {
        document.getElementById('modalTitle').textContent = 'Editar Produto';
        document.getElementById('produtoForm').action = '/produtos/' + id;
        document.getElementById('formMethod').value = 'PUT';
        document.getElementById('produtoId').value = id;
        document.getElementById('credencial_id').value = credencialId;
        document.getElementById('nome').value = nome;

        const precoFormatted = parseFloat(preco).toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, '.');
        document.getElementById('preco').value = precoFormatted;

        document.getElementById('produtoModal').classList.remove('hidden');
        document.querySelector('#produtoForm button[type="submit"]').textContent = 'Atualizar';
    }

    function closeModal() {
        document.getElementById('produtoModal').classList.add('hidden');
    }

    document.getElementById('produtoModal').addEventListener('click', function(e) {
        if (e.target === this) {
            closeModal();
        }
    });

    document.getElementById('produtoForm').addEventListener('submit', function(e) {
        const precoInput = document.getElementById('preco');
        const precoValue = precoInput.value.replace(/\./g, '').replace(',', '.');

        const hiddenInput = document.createElement('input');
        hiddenInput.type = 'hidden';
        hiddenInput.name = 'preco';
        hiddenInput.value = precoValue;

        precoInput.name = '';

        this.appendChild(hiddenInput);
    });
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\H\Desktop\notfy\resources\views/produtos.blade.php ENDPATH**/ ?>