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

<?php $__env->startSection('content'); ?>
<div class="space-y-6">
    <!-- Header & Action Row -->
    <div class="flex items-center justify-between">
        <div>
            <h2 class="text-2xl font-extrabold text-white tracking-tight">Invoice Management</h2>
            <p class="text-xs text-slate-400 mt-1">Create itemized invoices, calculate tax rates, track payment statuses, and print statements.</p>
        </div>
        <button onclick="openCreateInvoiceModal()" class="flex items-center gap-2 bg-gradient-to-r from-brand-600 to-indigo-600 hover:from-brand-500 hover:to-indigo-500 text-white font-semibold text-xs px-4 py-2.5 rounded-xl shadow-lg shadow-brand-500/25 transition-all">
            <i data-lucide="plus-circle" class="w-4 h-4"></i>
            <span>Create New Invoice</span>
        </button>
    </div>

    <!-- Status Tabs & Filter Bar -->
    <div class="glass-card p-4 rounded-2xl flex flex-col md:flex-row gap-4 items-center justify-between">
        <div class="flex items-center gap-2 overflow-x-auto w-full md:w-auto">
            <?php $currentStatus = request('status', ''); ?>
            <a href="<?php echo e(route('admin.invoices.index')); ?>" class="px-3.5 py-2 rounded-xl text-xs font-semibold transition-all <?php echo e($currentStatus == '' ? 'bg-indigo-600 text-white shadow-md' : 'bg-slate-900 text-slate-400 hover:text-white'); ?>">All Invoices</a>
            <a href="<?php echo e(route('admin.invoices.index', ['status' => 'Paid'])); ?>" class="px-3.5 py-2 rounded-xl text-xs font-semibold transition-all <?php echo e($currentStatus == 'Paid' ? 'bg-emerald-600 text-white shadow-md' : 'bg-slate-900 text-slate-400 hover:text-white'); ?>">Paid</a>
            <a href="<?php echo e(route('admin.invoices.index', ['status' => 'Unpaid'])); ?>" class="px-3.5 py-2 rounded-xl text-xs font-semibold transition-all <?php echo e($currentStatus == 'Unpaid' ? 'bg-indigo-600 text-white shadow-md' : 'bg-slate-900 text-slate-400 hover:text-white'); ?>">Unpaid</a>
            <a href="<?php echo e(route('admin.invoices.index', ['status' => 'Overdue'])); ?>" class="px-3.5 py-2 rounded-xl text-xs font-semibold transition-all <?php echo e($currentStatus == 'Overdue' ? 'bg-rose-600 text-white shadow-md' : 'bg-slate-900 text-slate-400 hover:text-white'); ?>">Overdue</a>
        </div>

        <form action="<?php echo e(route('admin.invoices.index')); ?>" method="GET" class="flex items-center gap-2 w-full md:w-auto">
            <div class="relative flex-1 max-w-xs">
                <i data-lucide="search" class="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-slate-500"></i>
                <input type="text" name="search" value="<?php echo e(request('search')); ?>" placeholder="Invoice # or client..." class="w-full bg-slate-950 border border-slate-800 text-slate-200 text-xs rounded-xl pl-9 pr-3 py-2 focus:outline-none focus:border-indigo-500">
            </div>
            <button type="submit" class="bg-slate-800 text-slate-300 px-3 py-2 rounded-xl text-xs font-semibold border border-slate-700">Search</button>
        </form>
    </div>

    <!-- Invoices 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">Invoice #</th>
                        <th class="py-4 px-6">Client Company</th>
                        <th class="py-4 px-6">Issue / Due Date</th>
                        <th class="py-4 px-6">Subtotal</th>
                        <th class="py-4 px-6">Tax (10%)</th>
                        <th class="py-4 px-6">Total Amount</th>
                        <th class="py-4 px-6">Status</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 = $invoices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $inv): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr class="hover:bg-slate-900/40 transition-colors">
                            <td class="py-4 px-6 font-mono text-indigo-400 font-bold"><?php echo e($inv->invoice_number); ?></td>
                            <td class="py-4 px-6">
                                <p class="text-xs font-bold text-white"><?php echo e($inv->client->company ?? 'N/A'); ?></p>
                                <p class="text-[11px] text-slate-400"><?php echo e($inv->client->name ?? ''); ?></p>
                            </td>
                            <td class="py-4 px-6 text-slate-300">
                                <p><?php echo e($inv->issue_date->format('M d, Y')); ?></p>
                                <p class="text-[11px] text-slate-500">Due <?php echo e($inv->due_date->format('M d, Y')); ?></p>
                            </td>
                            <td class="py-4 px-6">$<?php echo e(number_format($inv->subtotal, 2)); ?></td>
                            <td class="py-4 px-6 text-slate-400">$<?php echo e(number_format($inv->tax_amount, 2)); ?></td>
                            <td class="py-4 px-6 font-extrabold text-white text-sm">$<?php echo e(number_format($inv->total_amount, 2)); ?></td>
                            <td class="py-4 px-6">
                                <?php if($inv->status === 'Paid'): ?>
                                    <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> Paid
                                    </span>
                                <?php elseif($inv->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-amber-500/10 text-amber-400 border border-amber-500/20">
                                        <span class="w-1.5 h-1.5 rounded-full bg-amber-400"></span> Unpaid
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td class="py-4 px-6 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <button onclick="viewInvoiceModal(<?php echo e($inv->id); ?>)" class="p-2 rounded-lg bg-slate-800 text-indigo-400 hover:text-white hover:bg-indigo-600 transition-colors" title="View & Print Invoice">
                                        <i data-lucide="eye" class="w-4 h-4"></i>
                                    </button>

                                    <?php if($inv->status !== 'Paid'): ?>
                                        <form action="<?php echo e(route('admin.invoices.pay', $inv)); ?>" method="POST">
                                            <?php echo csrf_field(); ?>
                                            <button type="submit" class="p-2 rounded-lg bg-emerald-500/10 text-emerald-400 border border-emerald-500/20 hover:bg-emerald-600 hover:text-white transition-colors" title="Mark as Paid">
                                                <i data-lucide="check" class="w-4 h-4"></i>
                                            </button>
                                        </form>
                                    <?php endif; ?>

                                    <form action="<?php echo e(route('admin.invoices.destroy', $inv)); ?>" method="POST" onsubmit="return confirm('Delete invoice <?php echo e($inv->invoice_number); ?>?')">
                                        <?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">
                                            <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="8" class="py-8 text-center text-slate-500">No invoices found.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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

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

<!-- Modal: Create New Invoice Builder -->
<div id="createInvoiceModal" 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-2xl w-full p-6 shadow-2xl space-y-4 max-h-[90vh] overflow-y-auto">
        <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="plus-circle" class="w-5 h-5 text-indigo-400"></i>
                <span>Invoice Builder</span>
            </h3>
            <button onclick="closeCreateInvoiceModal()" class="text-slate-400 hover:text-white">&times;</button>
        </div>

        <form action="<?php echo e(route('admin.invoices.store')); ?>" method="POST" class="space-y-4">
            <?php echo csrf_field(); ?>
            <div class="grid grid-cols-3 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Select Client</label>
                    <select name="client_id" 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">
                        <option value="">-- Choose Client --</option>
                        <?php $__currentLoopData = $clients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($c->id); ?>"><?php echo e($c->company); ?> (<?php echo e($c->name); ?>)</option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Issue Date</label>
                    <input type="date" name="issue_date" value="<?php echo e(date('Y-m-d')); ?>" 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">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Due Date</label>
                    <input type="date" name="due_date" value="<?php echo e(date('Y-m-d', strtotime('+15 days'))); ?>" 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">
                </div>
            </div>

            <!-- Dynamic Line Items Section -->
            <div class="border border-slate-800 rounded-xl p-4 bg-slate-950/60 space-y-3">
                <div class="flex items-center justify-between">
                    <h4 class="text-xs font-bold text-slate-300 uppercase tracking-wider">Line Items</h4>
                    <button type="button" onclick="addInvoiceRow()" class="text-xs font-semibold text-indigo-400 hover:text-indigo-300 flex items-center gap-1">
                        <i data-lucide="plus" class="w-3.5 h-3.5"></i> Add Item
                    </button>
                </div>

                <div id="invoiceItemsContainer" class="space-y-2">
                    <div class="grid grid-cols-12 gap-2 items-center">
                        <div class="col-span-6">
                            <input type="text" name="items[0][description]" placeholder="Description (e.g. Monthly SaaS Subscription)" required class="w-full bg-slate-900 border border-slate-800 text-slate-100 text-xs rounded-lg px-3 py-2 focus:border-indigo-500">
                        </div>
                        <div class="col-span-2">
                            <input type="number" name="items[0][quantity]" value="1" min="1" required class="w-full bg-slate-900 border border-slate-800 text-slate-100 text-xs rounded-lg px-3 py-2 text-center focus:border-indigo-500">
                        </div>
                        <div class="col-span-3">
                            <input type="number" step="0.01" name="items[0][unit_price]" value="149.00" min="0" required class="w-full bg-slate-900 border border-slate-800 text-slate-100 text-xs rounded-lg px-3 py-2 text-right focus:border-indigo-500">
                        </div>
                        <div class="col-span-1 text-center">
                            <button type="button" onclick="this.closest('.grid').remove()" class="text-slate-500 hover:text-rose-400">&times;</button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Tax Percentage (%)</label>
                    <input type="number" step="0.1" name="tax_percent" value="10.0" 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">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Discount Amount ($)</label>
                    <input type="number" step="0.01" name="discount_amount" value="0.00" 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">
                </div>
            </div>

            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Invoice Notes / Terms</label>
                <textarea name="notes" rows="2" class="w-full bg-slate-950 border border-slate-800 text-slate-100 text-xs rounded-xl px-3 py-2 focus:border-indigo-500">Payment is due within 15 days of invoice date. Thank you for your business!</textarea>
            </div>

            <div class="flex items-center justify-end gap-3 pt-3 border-t border-slate-800">
                <button type="button" onclick="closeCreateInvoiceModal()" 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-5 py-2.5 rounded-xl shadow-lg shadow-indigo-500/25">Generate Invoice</button>
            </div>
        </form>
    </div>
</div>

<!-- Modal: Printable / Detailed View Invoice -->
<div id="viewInvoiceModal" 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-2xl w-full p-8 shadow-2xl space-y-6 max-h-[90vh] overflow-y-auto">
        <div id="invoicePrintArea">
            <div class="flex items-center justify-between border-b border-slate-800 pb-6">
                <div>
                    <div class="flex items-center gap-2">
                        <div class="w-8 h-8 rounded-lg bg-indigo-600 text-white font-black flex items-center justify-center text-sm">C</div>
                        <h2 class="text-xl font-extrabold text-white">Centipid Billing Systems</h2>
                    </div>
                    <p class="text-xs text-slate-400 mt-1">100 Billing Way, Suite 500 &bull; San Francisco, CA</p>
                </div>
                <div class="text-right">
                    <span id="invModalStatus" class="inline-block px-3 py-1 rounded-full text-xs font-bold bg-emerald-500/10 text-emerald-400 border border-emerald-500/20 mb-2">PAID</span>
                    <p id="invModalNumber" class="text-lg font-mono font-bold text-indigo-400">INV-2026-0001</p>
                    <p id="invModalDates" class="text-xs text-slate-400">Issued: Jan 10, 2026</p>
                </div>
            </div>

            <div class="grid grid-cols-2 gap-6 py-6 border-b border-slate-800 text-xs">
                <div>
                    <p class="font-bold text-slate-400 uppercase tracking-wider mb-1">Billed To:</p>
                    <p id="invModalClientCompany" class="text-sm font-bold text-white">Apex Cloud Solutions</p>
                    <p id="invModalClientName" class="text-slate-300">Alex Rivera</p>
                    <p id="invModalClientEmail" class="text-slate-400">alex@apexcloud.io</p>
                </div>
                <div class="text-right">
                    <p class="font-bold text-slate-400 uppercase tracking-wider mb-1">Payment Status:</p>
                    <p id="invModalPaymentRef" class="font-mono text-emerald-400">TXN-892347</p>
                    <p id="invModalPaymentMethod" class="text-slate-400">Stripe Gateway</p>
                </div>
            </div>

            <table class="w-full text-left text-xs my-6">
                <thead class="bg-slate-950 text-slate-400 uppercase text-[10px] font-bold">
                    <tr>
                        <th class="py-2.5 px-3">Item Description</th>
                        <th class="py-2.5 px-3 text-center">Qty</th>
                        <th class="py-2.5 px-3 text-right">Unit Price</th>
                        <th class="py-2.5 px-3 text-right">Total</th>
                    </tr>
                </thead>
                <tbody id="invModalItemsTable" class="divide-y divide-slate-800/80 font-medium">
                    <!-- Dynamic Rows Insertion -->
                </tbody>
            </table>

            <div class="flex justify-end pt-4 border-t border-slate-800 text-xs">
                <div class="w-64 space-y-2">
                    <div class="flex justify-between text-slate-400">
                        <span>Subtotal:</span>
                        <span id="invModalSubtotal" class="font-mono text-white">$0.00</span>
                    </div>
                    <div class="flex justify-between text-slate-400">
                        <span>Tax (10%):</span>
                        <span id="invModalTax" class="font-mono text-white">$0.00</span>
                    </div>
                    <div class="flex justify-between text-base font-bold text-white pt-2 border-t border-slate-800">
                        <span>Total Due:</span>
                        <span id="invModalTotal" class="font-mono text-emerald-400">$0.00</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="flex items-center justify-between pt-4 border-t border-slate-800">
            <button onclick="closeViewInvoiceModal()" class="px-4 py-2 text-xs font-semibold text-slate-400 hover:text-white">Close</button>
            <button onclick="window.print()" class="bg-indigo-600 hover:bg-indigo-500 text-white font-semibold text-xs px-5 py-2 rounded-xl flex items-center gap-2">
                <i data-lucide="printer" class="w-4 h-4"></i> Print Statement
            </button>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
    let itemIndex = 1;
    function openCreateInvoiceModal() {
        document.getElementById('createInvoiceModal').classList.remove('hidden');
    }
    function closeCreateInvoiceModal() {
        document.getElementById('createInvoiceModal').classList.add('hidden');
    }
    function addInvoiceRow() {
        const container = document.getElementById('invoiceItemsContainer');
        const row = document.createElement('div');
        row.className = 'grid grid-cols-12 gap-2 items-center';
        row.innerHTML = `
            <div class="col-span-6">
                <input type="text" name="items[${itemIndex}][description]" placeholder="Item description" required class="w-full bg-slate-900 border border-slate-800 text-slate-100 text-xs rounded-lg px-3 py-2">
            </div>
            <div class="col-span-2">
                <input type="number" name="items[${itemIndex}][quantity]" value="1" min="1" required class="w-full bg-slate-900 border border-slate-800 text-slate-100 text-xs rounded-lg px-3 py-2 text-center">
            </div>
            <div class="col-span-3">
                <input type="number" step="0.01" name="items[${itemIndex}][unit_price]" value="99.00" min="0" required class="w-full bg-slate-900 border border-slate-800 text-slate-100 text-xs rounded-lg px-3 py-2 text-right">
            </div>
            <div class="col-span-1 text-center">
                <button type="button" onclick="this.closest('.grid').remove()" class="text-slate-500 hover:text-rose-400">&times;</button>
            </div>
        `;
        container.appendChild(row);
        itemIndex++;
    }

    function viewInvoiceModal(id) {
        fetch(`/admin/invoices/${id}`)
            .then(res => res.json())
            .then(inv => {
                document.getElementById('invModalNumber').innerText = inv.invoice_number;
                document.getElementById('invModalStatus').innerText = inv.status.toUpperCase();
                document.getElementById('invModalDates').innerText = `Issued: ${inv.issue_date} | Due: ${inv.due_date}`;
                document.getElementById('invModalClientCompany').innerText = inv.client ? inv.client.company : 'N/A';
                document.getElementById('invModalClientName').innerText = inv.client ? inv.client.name : '';
                document.getElementById('invModalClientEmail').innerText = inv.client ? inv.client.email : '';
                document.getElementById('invModalSubtotal').innerText = `$${parseFloat(inv.subtotal).toFixed(2)}`;
                document.getElementById('invModalTax').innerText = `$${parseFloat(inv.tax_amount).toFixed(2)}`;
                document.getElementById('invModalTotal').innerText = `$${parseFloat(inv.total_amount).toFixed(2)}`;

                const tbody = document.getElementById('invModalItemsTable');
                tbody.innerHTML = '';
                inv.items.forEach(item => {
                    tbody.innerHTML += `
                        <tr>
                            <td class="py-2.5 px-3 text-white">${item.description}</td>
                            <td class="py-2.5 px-3 text-center text-slate-300">${item.quantity}</td>
                            <td class="py-2.5 px-3 text-right text-slate-300">$${parseFloat(item.unit_price).toFixed(2)}</td>
                            <td class="py-2.5 px-3 text-right font-bold text-white">$${parseFloat(item.total).toFixed(2)}</td>
                        </tr>
                    `;
                });

                document.getElementById('viewInvoiceModal').classList.remove('hidden');
            });
    }

    function closeViewInvoiceModal() {
        document.getElementById('viewInvoiceModal').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/invoices/index.blade.php ENDPATH**/ ?>