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

<?php $__env->startSection('content'); ?>
<div class="space-y-6">
    <!-- Header Row & Add Client Trigger -->
    <div class="flex items-center justify-between">
        <div>
            <h2 class="text-2xl font-extrabold text-white tracking-tight">Clients Directory</h2>
            <p class="text-xs text-slate-400 mt-1">Manage active enterprise accounts, contacts, and lifetime billing history.</p>
        </div>
        <button onclick="openAddClientModal()" class="flex items-center gap-2 bg-gradient-to-r from-indigo-600 to-indigo-700 hover:from-indigo-500 hover:to-indigo-600 text-white font-semibold text-xs px-4 py-2.5 rounded-xl shadow-lg shadow-indigo-500/25 transition-all">
            <i data-lucide="user-plus" class="w-4 h-4"></i>
            <span>Add New Client</span>
        </button>
    </div>

    <!-- Search & Status Filter Bar -->
    <div class="glass-card p-4 rounded-2xl flex flex-col md:flex-row gap-4 items-center justify-between">
        <form action="<?php echo e(route('admin.clients.index')); ?>" method="GET" class="flex items-center gap-3 w-full md:w-auto flex-1">
            <div class="relative flex-1 max-w-md">
                <i data-lucide="search" class="w-4 h-4 absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-500"></i>
                <input type="text" name="search" value="<?php echo e(request('search')); ?>" placeholder="Search client name, company, or email..." class="w-full bg-slate-950 border border-slate-800 text-slate-200 text-xs rounded-xl pl-10 pr-4 py-2.5 focus:outline-none focus:border-indigo-500">
            </div>

            <select name="status" onchange="this.form.submit()" class="bg-slate-950 border border-slate-800 text-slate-300 text-xs rounded-xl px-4 py-2.5 focus:outline-none focus:border-indigo-500">
                <option value="">All Statuses</option>
                <option value="Active" <?php echo e(request('status') == 'Active' ? 'selected' : ''); ?>>Active</option>
                <option value="Overdue" <?php echo e(request('status') == 'Overdue' ? 'selected' : ''); ?>>Overdue</option>
                <option value="Inactive" <?php echo e(request('status') == 'Inactive' ? 'selected' : ''); ?>>Inactive</option>
            </select>

            <button type="submit" class="bg-slate-800 text-slate-300 hover:text-white px-4 py-2.5 rounded-xl text-xs font-semibold border border-slate-700 transition-colors">
                Filter
            </button>
        </form>

        <span class="text-xs text-slate-400 font-medium">Showing <?php echo e($clients->total()); ?> Registered Clients</span>
    </div>

    <!-- Clients Table -->
    <div class="glass-card rounded-2xl overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs text-slate-300">
                <thead class="bg-slate-900/90 text-slate-400 uppercase text-[10px] font-bold tracking-wider border-b border-slate-800">
                    <tr>
                        <th class="py-4 px-6">Client / Company</th>
                        <th class="py-4 px-6">Contact Info</th>
                        <th class="py-4 px-6">Status</th>
                        <th class="py-4 px-6">Total Billed</th>
                        <th class="py-4 px-6">Total Paid</th>
                        <th class="py-4 px-6 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-800/60 font-medium">
                    <?php $__empty_1 = true; $__currentLoopData = $clients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $client): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr class="hover:bg-slate-900/40 transition-colors">
                            <td class="py-4 px-6">
                                <div class="flex items-center gap-3">
                                    <div class="w-10 h-10 rounded-xl bg-indigo-600/20 text-indigo-400 border border-indigo-500/20 flex items-center justify-center font-bold text-sm">
                                        <?php echo e(strtoupper(substr($client->company, 0, 2))); ?>

                                    </div>
                                    <div>
                                        <p class="text-sm font-bold text-white"><?php echo e($client->company); ?></p>
                                        <p class="text-xs text-slate-400"><?php echo e($client->name); ?></p>
                                    </div>
                                </div>
                            </td>
                            <td class="py-4 px-6">
                                <p class="text-xs text-slate-200"><?php echo e($client->email); ?></p>
                                <p class="text-[11px] text-slate-400"><?php echo e($client->phone ?? 'No phone'); ?></p>
                            </td>
                            <td class="py-4 px-6">
                                <?php if($client->status === 'Active'): ?>
                                    <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-[11px] font-bold bg-emerald-500/10 text-emerald-400 border border-emerald-500/20">
                                        <span class="w-1.5 h-1.5 rounded-full bg-emerald-400"></span> Active
                                    </span>
                                <?php elseif($client->status === 'Overdue'): ?>
                                    <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-[11px] font-bold bg-rose-500/10 text-rose-400 border border-rose-500/20">
                                        <span class="w-1.5 h-1.5 rounded-full bg-rose-400"></span> Overdue
                                    </span>
                                <?php else: ?>
                                    <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-[11px] font-bold bg-slate-800 text-slate-400 border border-slate-700">
                                        <span class="w-1.5 h-1.5 rounded-full bg-slate-500"></span> Inactive
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td class="py-4 px-6 font-bold text-white">$<?php echo e(number_format($client->total_billed, 2)); ?></td>
                            <td class="py-4 px-6 font-bold text-emerald-400">$<?php echo e(number_format($client->total_paid, 2)); ?></td>
                            <td class="py-4 px-6 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <button onclick='editClient(<?php echo json_encode($client, 15, 512) ?>)' class="p-2 rounded-lg bg-slate-800 text-slate-300 hover:text-white hover:bg-slate-700 transition-colors" title="Edit Client">
                                        <i data-lucide="edit-3" class="w-4 h-4"></i>
                                    </button>
                                    <form action="<?php echo e(route('admin.clients.destroy', $client)); ?>" method="POST" onsubmit="return confirm('Delete client <?php echo e($client->company); ?>?')">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('DELETE'); ?>
                                        <button type="submit" class="p-2 rounded-lg bg-slate-800 text-slate-400 hover:text-rose-400 hover:bg-rose-500/10 transition-colors" title="Delete Client">
                                            <i data-lucide="trash-2" class="w-4 h-4"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="6" class="py-8 text-center text-slate-500">No clients match your filter criteria.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div class="p-4 border-t border-slate-800">
            <?php echo e($clients->links()); ?>

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

<!-- Modal: Add New Client -->
<div id="addClientModal" class="fixed inset-0 z-50 hidden bg-slate-950/80 backdrop-blur-sm flex items-center justify-center p-4">
    <div class="bg-slate-900 border border-slate-800 rounded-2xl max-w-lg w-full p-6 shadow-2xl space-y-4">
        <div class="flex items-center justify-between border-b border-slate-800 pb-3">
            <h3 class="text-lg font-bold text-white flex items-center gap-2">
                <i data-lucide="user-plus" class="w-5 h-5 text-indigo-400"></i>
                <span>Add Client Account</span>
            </h3>
            <button onclick="closeAddClientModal()" class="text-slate-400 hover:text-white">&times;</button>
        </div>

        <form action="<?php echo e(route('admin.clients.store')); ?>" method="POST" class="space-y-4">
            <?php echo csrf_field(); ?>
            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Company Name</label>
                    <input type="text" name="company" required class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Contact Name</label>
                    <input type="text" name="name" required class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Email Address</label>
                    <input type="email" name="email" required class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Phone Number</label>
                    <input type="text" name="phone" class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
            </div>

            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Billing Address</label>
                <input type="text" name="address" class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
            </div>

            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Initial Status</label>
                <select name="status" class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                    <option value="Active">Active</option>
                    <option value="Inactive">Inactive</option>
                    <option value="Overdue">Overdue</option>
                </select>
            </div>

            <div class="flex items-center justify-end gap-3 pt-3 border-t border-slate-800">
                <button type="button" onclick="closeAddClientModal()" class="px-4 py-2 text-xs font-semibold text-slate-400 hover:text-white">Cancel</button>
                <button type="submit" class="bg-indigo-600 hover:bg-indigo-500 text-white font-semibold text-xs px-4 py-2 rounded-xl">Save Client</button>
            </div>
        </form>
    </div>
</div>

<!-- Modal: Edit Client -->
<div id="editClientModal" class="fixed inset-0 z-50 hidden bg-slate-950/80 backdrop-blur-sm flex items-center justify-center p-4">
    <div class="bg-slate-900 border border-slate-800 rounded-2xl max-w-lg w-full p-6 shadow-2xl space-y-4">
        <div class="flex items-center justify-between border-b border-slate-800 pb-3">
            <h3 class="text-lg font-bold text-white flex items-center gap-2">
                <i data-lucide="edit-3" class="w-5 h-5 text-indigo-400"></i>
                <span>Edit Client Details</span>
            </h3>
            <button onclick="closeEditClientModal()" class="text-slate-400 hover:text-white">&times;</button>
        </div>

        <form id="editClientForm" method="POST" class="space-y-4">
            <?php echo csrf_field(); ?>
            <?php echo method_field('PUT'); ?>
            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Company Name</label>
                    <input type="text" id="edit_company" name="company" required class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Contact Name</label>
                    <input type="text" id="edit_name" name="name" required class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Email Address</label>
                    <input type="email" id="edit_email" name="email" required class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Phone Number</label>
                    <input type="text" id="edit_phone" name="phone" class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                </div>
            </div>

            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Billing Address</label>
                <input type="text" id="edit_address" name="address" class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
            </div>

            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Account Status</label>
                <select id="edit_status" name="status" class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-indigo-500 focus:outline-none">
                    <option value="Active">Active</option>
                    <option value="Inactive">Inactive</option>
                    <option value="Overdue">Overdue</option>
                </select>
            </div>

            <div class="flex items-center justify-end gap-3 pt-3 border-t border-slate-800">
                <button type="button" onclick="closeEditClientModal()" class="px-4 py-2 text-xs font-semibold text-slate-400 hover:text-white">Cancel</button>
                <button type="submit" class="bg-indigo-600 hover:bg-indigo-500 text-white font-semibold text-xs px-4 py-2 rounded-xl">Update Client</button>
            </div>
        </form>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
    function openAddClientModal() {
        document.getElementById('addClientModal').classList.remove('hidden');
    }
    function closeAddClientModal() {
        document.getElementById('addClientModal').classList.add('hidden');
    }
    function editClient(client) {
        document.getElementById('editClientForm').action = `/admin/clients/${client.id}`;
        document.getElementById('edit_company').value = client.company;
        document.getElementById('edit_name').value = client.name;
        document.getElementById('edit_email').value = client.email;
        document.getElementById('edit_phone').value = client.phone || '';
        document.getElementById('edit_address').value = client.address || '';
        document.getElementById('edit_status').value = client.status;
        document.getElementById('editClientModal').classList.remove('hidden');
    }
    function closeEditClientModal() {
        document.getElementById('editClientModal').classList.add('hidden');
    }
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\RentEz\Desktop\projects\billingCRM\resources\views/admin/clients/index.blade.php ENDPATH**/ ?>