<?php if (isset($component)) { $__componentOriginale0f1cdd055772eb1d4a99981c240763e = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginale0f1cdd055772eb1d4a99981c240763e = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.admin-layout','data' => ['title' => 'Clientes — OPCON']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('admin-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['title' => 'Clientes — OPCON']); ?>
<div x-data="clientesApp()" x-init="init()">

    
    <div style="display:flex; align-items:flex-start; justify-content:space-between; gap:1rem; margin-bottom:1.75rem; flex-wrap:wrap;">
        <div>
            <h1 style="font-size:1.375rem; font-weight:700; color:#1e3a5f; margin:0 0 3px;">Clientes</h1>
            <p style="font-size:0.8125rem; color:#64748b; margin:0;">Gerencie os dados e o acesso ao portal de cada cliente</p>
        </div>
        <div style="display:flex; gap:0.625rem; flex-wrap:wrap; align-items:center;">
            
            <div style="position:relative;">
                <svg style="position:absolute; left:0.625rem; top:50%; transform:translateY(-50%); width:14px;height:14px;color:#94a3b8; pointer-events:none;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                </svg>
                <input x-model="search" type="text" placeholder="Buscar cliente..." style="padding:0.5rem 0.75rem 0.5rem 2rem; border:1.5px solid #e2e8f0; border-radius:10px; font-size:0.8125rem; font-family:inherit; color:#1e3a5f; outline:none; background:#fff; width:200px; transition:border-color 0.15s;"
                       onfocus="this.style.borderColor='#25b87e'" onblur="this.style.borderColor='#e2e8f0'">
            </div>
            
            <select x-model="filter" style="padding:0.5rem 0.75rem; border:1.5px solid #e2e8f0; border-radius:10px; font-size:0.8125rem; font-family:inherit; color:#1e3a5f; outline:none; background:#fff; cursor:pointer;">
                <option value="">Todos os contratos</option>
                <option value="com_acesso">Com acesso ao portal</option>
                <option value="sem_acesso">Sem acesso ao portal</option>
                <option value="ativo">Contrato ativo</option>
            </select>
        </div>
    </div>

    
    <div style="display:grid; grid-template-columns:repeat(auto-fit,minmax(150px,1fr)); gap:1rem; margin-bottom:1.75rem;">
        <?php
            $total       = $contratos->count();
            $comPortal   = $contratos->whereNotNull('portal_user_id')->count();
            $ativos      = $contratos->where('status_workflow', 'Ativo')->count();
            $semPortal   = $total - $comPortal;
        ?>
        <?php $__currentLoopData = [
            ['Contratos', $total,     '#1e3a5f', '#eff6ff', 'M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z'],
            ['Com portal', $comPortal, '#25b87e', '#f0fdf4', 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z'],
            ['Sem portal',  $semPortal, '#d97706', '#fffbeb', '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'],
            ['Ativos',      $ativos,   '#2563eb', '#eff6ff',  'M13 10V3L4 14h7v7l9-11h-7z'],
        ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value, $color, $bg, $icon]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div style="background:#fff; border:1px solid #e2e8f0; border-radius:14px; padding:1.125rem; display:flex; align-items:center; gap:0.875rem;">
            <div style="width:38px; height:38px; border-radius:10px; background:<?php echo e($bg); ?>; display:flex; align-items:center; justify-content:center; flex-shrink:0;">
                <svg style="width:16px;height:16px;color:<?php echo e($color); ?>;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.75" d="<?php echo e($icon); ?>"/>
                </svg>
            </div>
            <div>
                <p style="font-size:1.375rem; font-weight:800; color:#1e3a5f; margin:0; line-height:1;"><?php echo e($value); ?></p>
                <p style="font-size:0.75rem; color:#94a3b8; margin:2px 0 0; font-weight:500;"><?php echo e($label); ?></p>
            </div>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>

    
    <div style="display:grid; grid-template-columns:repeat(auto-fill,minmax(320px,1fr)); gap:1.125rem;">
        <?php $__currentLoopData = $contratos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $contrato): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <?php
            $user = $contrato->portalUser;
            $ativo = $user && $user->email_verified_at;
            $statusColors = [
                'Ativo'                           => ['bg' => '#f0fdf4', 'color' => '#16a34a', 'border' => '#bbf7d0'],
                'Rascunho'                        => ['bg' => '#f8fafc', 'color' => '#64748b', 'border' => '#e2e8f0'],
                'Aguardando conferência'          => ['bg' => '#fefce8', 'color' => '#a16207', 'border' => '#fde68a'],
                'Aguardando validação financeira' => ['bg' => '#fefce8', 'color' => '#a16207', 'border' => '#fde68a'],
                'Aguardando aprovação diretoria'  => ['bg' => '#fff7ed', 'color' => '#c2410c', 'border' => '#fed7aa'],
                'Encerrado'                       => ['bg' => '#fef2f2', 'color' => '#dc2626', 'border' => '#fecaca'],
            ];
            $sc = $statusColors[$contrato->status_workflow] ?? $statusColors['Rascunho'];
            $initials = strtoupper(substr($contrato->razao_social, 0, 2));
            $colors = ['#25b87e','#2563eb','#7c3aed','#d97706','#dc2626','#0891b2'];
            $colorIdx = crc32($contrato->razao_social) % count($colors);
            $cardColor = $colors[abs($colorIdx)];
        ?>
        <div style="background:#fff; border:1px solid #e8eef5; border-radius:18px; overflow:hidden; transition:box-shadow 0.2s, transform 0.2s;"
             onmouseover="this.style.boxShadow='0 8px 32px rgba(0,0,0,0.1)'; this.style.transform='translateY(-2px)'"
             onmouseout="this.style.boxShadow=''; this.style.transform=''">

            
            <div style="height:4px; background:<?php echo e($cardColor); ?>;"></div>

            
            <div style="padding:1.25rem 1.25rem 1rem;">
                <div style="display:flex; align-items:flex-start; gap:0.875rem; margin-bottom:1rem;">
                    
                    <div style="width:44px; height:44px; border-radius:12px; background:<?php echo e($cardColor); ?>18; border:1.5px solid <?php echo e($cardColor); ?>30; display:flex; align-items:center; justify-content:center; font-size:0.875rem; font-weight:800; color:<?php echo e($cardColor); ?>; flex-shrink:0; letter-spacing:0.02em;">
                        <?php echo e($initials); ?>

                    </div>
                    <div style="flex:1; min-width:0;">
                        <p style="font-size:0.9375rem; font-weight:700; color:#1e3a5f; margin:0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;"><?php echo e($contrato->razao_social); ?></p>
                        <?php if($contrato->nome_fantasia): ?>
                            <p style="font-size:0.75rem; color:#94a3b8; margin:2px 0 0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;"><?php echo e($contrato->nome_fantasia); ?></p>
                        <?php endif; ?>
                    </div>
                    
                    <span style="font-size:0.625rem; font-weight:700; padding:0.2rem 0.5rem; border-radius:100px; background:<?php echo e($sc['bg']); ?>; color:<?php echo e($sc['color']); ?>; border:1px solid <?php echo e($sc['border']); ?>; white-space:nowrap; flex-shrink:0; letter-spacing:0.04em; text-transform:uppercase;">
                        <?php echo e($contrato->status_workflow === 'Ativo' ? 'Ativo' : Str::limit($contrato->status_workflow, 12)); ?>

                    </span>
                </div>

                
                <div style="display:grid; grid-template-columns:1fr 1fr; gap:0.5rem; margin-bottom:1rem;">
                    <?php if($contrato->cnpj): ?>
                    <div style="background:#f8fafc; border-radius:8px; padding:0.5rem 0.625rem;">
                        <p style="font-size:0.625rem; font-weight:600; color:#94a3b8; margin:0 0 2px; text-transform:uppercase; letter-spacing:0.06em;">CNPJ</p>
                        <p style="font-size:0.75rem; font-weight:600; color:#1e3a5f; margin:0; font-family:monospace;"><?php echo e($contrato->cnpj); ?></p>
                    </div>
                    <?php endif; ?>
                    <?php if($contrato->cidade_uf): ?>
                    <div style="background:#f8fafc; border-radius:8px; padding:0.5rem 0.625rem;">
                        <p style="font-size:0.625rem; font-weight:600; color:#94a3b8; margin:0 0 2px; text-transform:uppercase; letter-spacing:0.06em;">Cidade</p>
                        <p style="font-size:0.75rem; font-weight:600; color:#1e3a5f; margin:0;"><?php echo e($contrato->cidade_uf); ?></p>
                    </div>
                    <?php endif; ?>
                    <?php if($contrato->regime_tributario): ?>
                    <div style="background:#f8fafc; border-radius:8px; padding:0.5rem 0.625rem;">
                        <p style="font-size:0.625rem; font-weight:600; color:#94a3b8; margin:0 0 2px; text-transform:uppercase; letter-spacing:0.06em;">Regime</p>
                        <p style="font-size:0.75rem; font-weight:600; color:#1e3a5f; margin:0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;" title="<?php echo e($contrato->regime_tributario); ?>"><?php echo e($contrato->regime_tributario); ?></p>
                    </div>
                    <?php endif; ?>
                    <?php if($contrato->honorario): ?>
                    <div style="background:#f0fdf4; border-radius:8px; padding:0.5rem 0.625rem;">
                        <p style="font-size:0.625rem; font-weight:600; color:#25b87e; margin:0 0 2px; text-transform:uppercase; letter-spacing:0.06em;">Honorário</p>
                        <p style="font-size:0.75rem; font-weight:700; color:#1e3a5f; margin:0;">R$ <?php echo e(number_format($contrato->honorario, 2, ',', '.')); ?></p>
                    </div>
                    <?php endif; ?>
                </div>

                
                <div style="border-top:1px solid #f1f5f9; padding-top:0.875rem;">
                    <?php if($user): ?>
                        <div style="display:flex; align-items:center; gap:0.625rem; margin-bottom:0.625rem;">
                            <div style="width:7px; height:7px; border-radius:50%; background:<?php echo e($ativo ? '#25b87e' : '#e2e8f0'); ?>; flex-shrink:0;"></div>
                            <div style="flex:1; min-width:0;">
                                <p style="font-size:0.8125rem; font-weight:600; color:#1e3a5f; margin:0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;"><?php echo e($user->name); ?></p>
                                <p style="font-size:0.6875rem; color:#94a3b8; margin:0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;"><?php echo e($user->email); ?></p>
                            </div>
                            <span style="font-size:0.625rem; font-weight:700; padding:0.2rem 0.5rem; border-radius:100px; background:<?php echo e($ativo ? '#f0fdf4' : '#f8fafc'); ?>; color:<?php echo e($ativo ? '#16a34a' : '#94a3b8'); ?>; border:1px solid <?php echo e($ativo ? '#bbf7d0' : '#e2e8f0'); ?>; flex-shrink:0;">
                                <?php echo e($ativo ? 'Ativo' : 'Inativo'); ?>

                            </span>
                        </div>
                        <div style="display:flex; gap:0.5rem;">
                            <button @click="openEdit(<?php echo e($contrato->id); ?>, <?php echo e(json_encode($user->name)); ?>, <?php echo e(json_encode($user->email)); ?>)"
                                    style="flex:1; padding:0.5rem; border:1.5px solid #e2e8f0; border-radius:9px; background:#fff; color:#475569; font-size:0.8125rem; font-weight:500; cursor:pointer; font-family:inherit; transition:all 0.15s;"
                                    onmouseover="this.style.background='#f8fafc'; this.style.borderColor='#cbd5e1'" onmouseout="this.style.background='#fff'; this.style.borderColor='#e2e8f0'">
                                Editar acesso
                            </button>
                            <button @click="toggleAcesso(<?php echo e($contrato->id); ?>, $el)"
                                    style="padding:0.5rem 0.75rem; border:1.5px solid <?php echo e($ativo ? '#fecaca' : '#bbf7d0'); ?>; border-radius:9px; background:<?php echo e($ativo ? '#fef2f2' : '#f0fdf4'); ?>; color:<?php echo e($ativo ? '#dc2626' : '#16a34a'); ?>; font-size:0.8125rem; font-weight:500; cursor:pointer; font-family:inherit; transition:all 0.15s; white-space:nowrap;"
                                    :data-id="<?php echo e($contrato->id); ?>">
                                <?php echo e($ativo ? 'Desativar' : 'Ativar'); ?>

                            </button>
                        </div>
                    <?php else: ?>
                        <div style="display:flex; align-items:center; gap:0.5rem; margin-bottom:0.625rem; padding:0.5rem 0.625rem; background:#f8fafc; border-radius:8px;">
                            <svg style="width:14px;height:14px;color:#94a3b8;flex-shrink:0;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/>
                            </svg>
                            <p style="font-size:0.75rem; color:#94a3b8; margin:0;">Sem acesso ao portal</p>
                        </div>
                        <button @click="openCreate(<?php echo e($contrato->id); ?>, <?php echo e(json_encode($contrato->razao_social)); ?>, <?php echo e(json_encode($contrato->email ?? '')); ?>)"
                                style="width:100%; padding:0.5rem; border:1.5px solid #25b87e; border-radius:9px; background:#f0fdf4; color:#16a34a; font-size:0.8125rem; font-weight:600; cursor:pointer; font-family:inherit; transition:all 0.15s; display:flex; align-items:center; justify-content:center; gap:0.375rem;"
                                onmouseover="this.style.background='#dcfce7'" onmouseout="this.style.background='#f0fdf4'">
                            <svg style="width:13px;height:13px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                            </svg>
                            Criar acesso ao portal
                        </button>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>

    
    <?php if($contratos->isEmpty()): ?>
    <div style="text-align:center; padding:4rem 1rem;">
        <div style="width:64px; height:64px; border-radius:16px; background:#f1f5f9; display:flex; align-items:center; justify-content:center; margin:0 auto 1rem;">
            <svg style="width:28px;height:28px;color:#94a3b8;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5"/>
            </svg>
        </div>
        <p style="font-size:1rem; font-weight:600; color:#1e3a5f; margin:0 0 0.5rem;">Nenhum contrato cadastrado</p>
        <p style="font-size:0.875rem; color:#94a3b8; margin:0;">Crie contratos para começar a gerenciar clientes.</p>
    </div>
    <?php endif; ?>

    
    <div x-show="modal"
         x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100"
         x-transition:leave="transition ease-in duration-150" x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0"
         @keydown.escape.window="modal=false"
         style="position:fixed; inset:0; z-index:300; background:rgba(15,23,42,0.6); backdrop-filter:blur(4px);">
        <div style="display:flex; align-items:center; justify-content:center; min-height:100%; width:100%; padding:1rem;">
        <div @click.stop
             x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100"
             style="background:#fff; border-radius:20px; width:100%; max-width:460px; overflow:hidden; box-shadow:0 32px 80px rgba(0,0,0,0.22); display:flex; flex-direction:column;">

            
            <div style="background:linear-gradient(135deg,#1e3a5f 0%,#162e4a 100%); padding:1.375rem 1.5rem; display:flex; align-items:flex-start; justify-content:space-between;">
                <div>
                    <p style="font-size:0.625rem; font-weight:700; text-transform:uppercase; letter-spacing:0.1em; color:rgba(255,255,255,0.38); margin:0 0 3px;">Portal do Cliente</p>
                    <h3 x-text="editId ? 'Editar acesso — ' + clienteNome : 'Novo acesso — ' + clienteNome"
                        style="font-size:1rem; font-weight:700; color:#fff; margin:0; line-height:1.3; max-width:280px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;"></h3>
                </div>
                <button @click="modal=false"
                        style="width:32px; height:32px; border-radius:8px; border:1px solid rgba(255,255,255,0.14); background:rgba(255,255,255,0.07); color:rgba(255,255,255,0.55); cursor:pointer; display:flex; align-items:center; justify-content:center; flex-shrink:0; transition:all 0.15s; margin-top:2px;"
                        onmouseover="this.style.background='rgba(255,255,255,0.14)'; this.style.color='#fff'"
                        onmouseout="this.style.background='rgba(255,255,255,0.07)'; this.style.color='rgba(255,255,255,0.55)'">
                    <svg style="width:14px;height:14px;" 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>

            
            <div style="padding:1.5rem; background:#f8fafc; flex:1; display:flex; flex-direction:column; gap:1rem;">
                <div style="background:#fff; border:1px solid #eef2f7; border-radius:14px; padding:1.25rem; display:grid; grid-template-columns:1fr 1fr; gap:1rem;">
                    <div style="grid-column:1/-1;">
                        <label class="clabel">Nome completo</label>
                        <input x-model="form.name" type="text" placeholder="Nome do responsável" class="cinput">
                    </div>
                    <div style="grid-column:1/-1;">
                        <label class="clabel">E-mail de login</label>
                        <input x-model="form.email" type="email" placeholder="email@empresa.com.br" class="cinput">
                    </div>
                    <div style="grid-column:1/-1;">
                        <label class="clabel">
                            Senha
                            <span x-show="editId" style="font-weight:400; color:#94a3b8; font-size:0.6875rem; margin-left:0.25rem;">(deixe em branco para manter)</span>
                        </label>
                        <input x-model="form.password" type="password" placeholder="Mínimo 6 caracteres" class="cinput">
                    </div>
                </div>

                <div style="background:#eff6ff; border:1px solid #bfdbfe; border-radius:12px; padding:0.875rem 1rem; display:flex; align-items:flex-start; gap:0.625rem;">
                    <svg style="width:15px;height:15px;color:#2563eb;flex-shrink:0;margin-top:1px;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                    <p style="font-size:0.8125rem; color:#1d4ed8; margin:0; line-height:1.5;">O cliente usará este e-mail e senha para acessar o <strong>Portal do Cliente</strong> e visualizar contratos, documentos e cobranças.</p>
                </div>

                <div x-show="errMsg" x-text="errMsg"
                     style="font-size:0.8125rem; color:#dc2626; padding:0.75rem 1rem; background:#fef2f2; border-radius:10px; border:1.5px solid #fecaca;"></div>
            </div>

            
            <div style="padding:1rem 1.5rem; border-top:1.5px solid #f1f5f9; display:flex; gap:0.625rem; justify-content:space-between; align-items:center; background:#fff;">
                <div x-show="editId">
                    <button @click="revogar()"
                            style="padding:0.5625rem 0.875rem; border:1.5px solid #fecaca; border-radius:10px; background:#fff; color:#dc2626; font-size:0.8125rem; font-weight:500; cursor:pointer; font-family:inherit; transition:all 0.15s;"
                            onmouseover="this.style.background='#fef2f2'" onmouseout="this.style.background='#fff'">
                        Revogar acesso
                    </button>
                </div>
                <div x-show="!editId"></div>
                <div style="display:flex; gap:0.5rem;">
                    <button @click="modal=false"
                            style="padding:0.5625rem 1rem; border:1.5px solid #e2e8f0; border-radius:10px; background:#fff; color:#64748b; font-size:0.875rem; font-weight:500; cursor:pointer; font-family:inherit; transition:all 0.15s;"
                            onmouseover="this.style.background='#f8fafc'" onmouseout="this.style.background='#fff'">
                        Cancelar
                    </button>
                    <button @click="save()" :disabled="saving"
                            style="padding:0.5625rem 1.375rem; background:#25b87e; border:none; border-radius:10px; color:#fff; font-size:0.875rem; font-weight:600; cursor:pointer; font-family:inherit; transition:all 0.15s; box-shadow:0 2px 8px rgba(37,184,126,0.3);"
                            :style="saving ? 'opacity:0.7;cursor:wait;' : ''"
                            onmouseover="if(!this.disabled)this.style.background='#1fa96e'" onmouseout="if(!this.disabled)this.style.background='#25b87e'">
                        <span x-text="saving ? 'Salvando...' : (editId ? 'Salvar alterações' : 'Criar acesso')"></span>
                    </button>
                </div>
            </div>

        </div>
        </div>
    </div>
</div>

<style>
.clabel { display:block; font-size:0.75rem; font-weight:600; color:#475569; margin-bottom:0.375rem; }
.cinput { width:100%; padding:0.7rem 0.9rem; border:1.5px solid #e2e8f0; border-radius:10px; font-size:0.875rem; font-family:inherit; color:#1e3a5f; outline:none; box-sizing:border-box; background:#fff; transition:border-color 0.15s, box-shadow 0.15s; }
.cinput:focus { border-color:#25b87e; box-shadow:0 0 0 3px rgba(37,184,126,0.12); }
.cinput::placeholder { color:#c8d3de; }
</style>

<script>
function clientesApp() {
    return {
        modal:      false,
        editId:     null,
        contratoId: null,
        clienteNome: '',
        saving:     false,
        errMsg:     '',
        search:     '',
        filter:     '',
        form: { name: '', email: '', password: '' },

        init() {
            // Client-side search/filter
            this.$watch('search', () => this.applyFilter());
            this.$watch('filter', () => this.applyFilter());
        },

        applyFilter() {
            const q   = this.search.toLowerCase();
            const f   = this.filter;
            document.querySelectorAll('[data-cliente-card]').forEach(card => {
                const name   = (card.dataset.nome   || '').toLowerCase();
                const cnpj   = (card.dataset.cnpj   || '').toLowerCase();
                const portal = card.dataset.portal;
                const status = card.dataset.status;
                let show = true;
                if (q && !name.includes(q) && !cnpj.includes(q)) show = false;
                if (f === 'com_acesso' && portal !== '1') show = false;
                if (f === 'sem_acesso' && portal !== '0') show = false;
                if (f === 'ativo'      && status !== 'Ativo') show = false;
                card.style.display = show ? '' : 'none';
            });
        },

        openCreate(contratoId, nome, email) {
            this.editId     = null;
            this.contratoId = contratoId;
            this.clienteNome = nome;
            this.form       = { name: nome, email: email || '', password: '' };
            this.errMsg     = '';
            this.modal      = true;
        },

        openEdit(contratoId, name, email) {
            this.editId     = contratoId;
            this.contratoId = contratoId;
            this.clienteNome = name;
            this.form       = { name, email, password: '' };
            this.errMsg     = '';
            this.modal      = true;
        },

        async save() {
            this.saving = true;
            this.errMsg = '';
            const url    = this.editId
                ? `/admin/clientes/${this.contratoId}/acesso`
                : `/admin/clientes/${this.contratoId}/acesso`;
            const method = this.editId ? 'PATCH' : 'POST';
            try {
                const res  = await fetch(url, {
                    method,
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRF-TOKEN': document.querySelector('meta[name=csrf-token]').content,
                        'Accept':       'application/json',
                    },
                    body: JSON.stringify(this.form),
                });
                const json = await res.json();
                if (!res.ok) {
                    this.errMsg = json.message || Object.values(json.errors ?? {})[0]?.[0] || 'Erro ao salvar.';
                    return;
                }
                location.reload();
            } catch {
                this.errMsg = 'Erro de conexão. Tente novamente.';
            } finally {
                this.saving = false;
            }
        },

        async revogar() {
            if (!confirm('Revogar o acesso ao portal? O usuário não poderá mais fazer login.')) return;
            await fetch(`/admin/clientes/${this.contratoId}/acesso`, {
                method: 'DELETE',
                headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name=csrf-token]').content },
            });
            location.reload();
        },

        async toggleAcesso(id) {
            await fetch(`/admin/clientes/${id}/acesso/toggle`, {
                method: 'PATCH',
                headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name=csrf-token]').content },
            });
            location.reload();
        },
    };
}
</script>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginale0f1cdd055772eb1d4a99981c240763e)): ?>
<?php $attributes = $__attributesOriginale0f1cdd055772eb1d4a99981c240763e; ?>
<?php unset($__attributesOriginale0f1cdd055772eb1d4a99981c240763e); ?>
<?php endif; ?>
<?php if (isset($__componentOriginale0f1cdd055772eb1d4a99981c240763e)): ?>
<?php $component = $__componentOriginale0f1cdd055772eb1d4a99981c240763e; ?>
<?php unset($__componentOriginale0f1cdd055772eb1d4a99981c240763e); ?>
<?php endif; ?>
<?php /**PATH C:\ProjetosWeb\OPCON\resources\views\pages\admin\clientes.blade.php ENDPATH**/ ?>