<?php $__env->startSection('title', $config['entity'] . ' — Index'); ?>

<?php $__env->startSection('content'); ?>
<?php echo $__env->make('partials.qi-styles', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

<?php
    $gridColumns = 'repeat(' . (count($config['columns']) + 1) . ', minmax(120px, 1fr))';
?>

<div class="qi-wrap">
    <nav class="qi-bc">
        <?php $__currentLoopData = $config['breadcrumbs']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $crumb): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <?php if(! $loop->first): ?><span>›</span><?php endif; ?>
            <?php if($crumb['url']): ?>
                <a href="<?php echo e($crumb['url']); ?>"><?php echo e($crumb['label']); ?></a>
            <?php else: ?>
                <span><?php echo e($crumb['label']); ?></span>
            <?php endif; ?>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        <span>›</span>
        <span class="qi-bc-current"><?php echo e($config['entity']); ?></span>
    </nav>

    <div class="qi-hero">
        <div>
            <h1><?php echo e($config['entity']); ?></h1>
            <p><?php echo e($config['description']); ?></p>
        </div>
        <div class="qi-badge">
            <div class="qi-badge-val"><?php echo e($records->total()); ?></div>
            <div class="qi-badge-lab">Records</div>
        </div>
    </div>

    <?php if(session('success')): ?>
        <div class="qi-alert qi-alert-success"><?php echo e(session('success')); ?></div>
    <?php endif; ?>
    <?php if($errors->any()): ?>
        <div class="qi-alert qi-alert-danger">
            <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $e): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><div><?php echo e($e); ?></div><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
    <?php endif; ?>

    <form method="GET" action="<?php echo e(route($config['route'])); ?>">
        <div class="qi-toolbar">
            <a href="<?php echo e($config['parentUrl']); ?>" class="qi-btn">← Back</a>
            <div class="qi-search">
                <input name="search" type="search" value="<?php echo e(request('search')); ?>" placeholder="Search...">
            </div>
            <div class="qi-toolbar-right">
                <?php if($config['createUrl']): ?>
                    <a href="<?php echo e($config['createUrl']); ?>" class="qi-btn qi-btn-primary">+ Add <?php echo e($config['entity']); ?></a>
                <?php endif; ?>
            </div>
        </div>

        <div class="qi-filters">
            <?php $__currentLoopData = $config['filters']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $filter): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <div>
                    <label class="qi-filter-label" for="qi-filter-<?php echo e($filter['name']); ?>"><?php echo e($filter['label']); ?></label>
                    <?php if(($filter['type'] ?? 'text') === 'select'): ?>
                        <select class="qi-filter-control" id="qi-filter-<?php echo e($filter['name']); ?>" name="<?php echo e($filter['name']); ?>">
                            <option value="">All</option>
                            <?php $__currentLoopData = ($config['filterOptions'][$filter['name']] ?? []); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $option): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($option->value ?? $option['value']); ?>" <?php if((string) request($filter['name']) === (string) ($option->value ?? $option['value'])): echo 'selected'; endif; ?>>
                                    <?php echo e($option->label ?? $option['label']); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    <?php else: ?>
                        <input class="qi-filter-control" id="qi-filter-<?php echo e($filter['name']); ?>" name="<?php echo e($filter['name']); ?>"
                               type="<?php echo e(str_starts_with(($filter['type'] ?? 'text'), 'date_') ? 'date' : 'text'); ?>"
                               value="<?php echo e(request($filter['name'])); ?>">
                    <?php endif; ?>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <div class="qi-filter-actions">
                <button type="submit" class="qi-btn qi-btn-primary">Apply</button>
                <a href="<?php echo e(route($config['route'])); ?>" class="qi-btn">Reset</a>
            </div>
        </div>
    </form>

    <div class="qi-table" style="--qi-cols: <?php echo e($gridColumns); ?>;">
        <div class="qi-thead">
            <?php $__currentLoopData = $config['columns']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $label => $column): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <div><?php echo e($label); ?></div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <div>Actions</div>
        </div>

        <?php $__empty_1 = true; $__currentLoopData = $records; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $record): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
            <div class="qi-trow">
                <?php $__currentLoopData = $config['columns']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $label => $column): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php $value = property_exists($record, $column) ? $record->{$column} : '—'; ?>
                    <div class="qi-cell" data-label="<?php echo e($label); ?>">
                        <?php if($column === 'status' && $value !== '—'): ?>
                            <span class="qi-status qi-status-<?php echo e($value); ?>"><?php echo e(str_replace('_', ' ', $value)); ?></span>
                        <?php elseif(is_numeric($value) && str_contains(strtolower($label), 'total') || str_contains(strtolower($label), 'amount') || str_contains(strtolower($label), 'balance') || str_contains(strtolower($label), 'price') || str_contains(strtolower($label), 'tax')): ?>
                            <?php echo e(is_numeric($value) ? number_format((float) $value, 2) : $value); ?>

                        <?php else: ?>
                            <?php echo e($value ?? '—'); ?>

                        <?php endif; ?>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <div class="qi-actions">
                    <?php if(! empty($config['subNav']) && \Illuminate\Support\Facades\Route::has('chart-of-accounts.main-accounts.sub-accounts.index')): ?>
                        <a class="qi-act-sub" href="<?php echo e(route('chart-of-accounts.main-accounts.sub-accounts.index', $record->id)); ?>">Sub Accounts →</a>
                    <?php endif; ?>
                    <?php if(! empty($config['documentActions']) && property_exists($record, 'id')): ?>
                        <?php if(\Illuminate\Support\Facades\Route::has('documents.pdf') && ($config['table'] ?? '') === 'document_headers'): ?>
                            <a class="qi-act-view" href="<?php echo e(route('documents.pdf', $record->id)); ?>">View</a>
                        <?php elseif(\Illuminate\Support\Facades\Route::has('supplier.documents.pdf') && ($config['table'] ?? '') === 'supplier_document_headers'): ?>
                            <a class="qi-act-view" href="<?php echo e(route('supplier.documents.pdf', $record->id)); ?>">View</a>
                        <?php else: ?>
                            <span class="qi-act-view">View</span>
                        <?php endif; ?>
                        <?php if(($record->status ?? null) === 'draft' && \Illuminate\Support\Facades\Route::has('documents.process') && ($config['table'] ?? '') === 'document_headers'): ?>
                            <a class="qi-act-edit" href="<?php echo e(route('documents.process', $record->id)); ?>">Process</a>
                        <?php endif; ?>
                        <?php if(\Illuminate\Support\Facades\Route::has('documents.download-pdf') && ($config['table'] ?? '') === 'document_headers'): ?>
                            <a class="qi-act-sub" href="<?php echo e(route('documents.download-pdf', [$record->type ?? 'document', $record->id])); ?>">PDF</a>
                        <?php endif; ?>

                        <!-- Document Conversions & Credit Allocations -->
                        <?php if(($config['key'] ?? '') === 'quotations' && ($record->status ?? '') !== 'cancelled' && ($record->status ?? '') !== 'converted'): ?>
                            <a class="qi-act-convert" href="<?php echo e(route('documents.convert-to-invoice', $record->id)); ?>">→ Invoice</a>
                            <a class="qi-act-convert" href="<?php echo e(route('documents.convert-to-sales-order', $record->id)); ?>">→ Sales Order</a>
                        <?php endif; ?>

                        <?php if(($config['key'] ?? '') === 'sales-orders' && ($record->status ?? '') !== 'cancelled' && ($record->status ?? '') !== 'converted'): ?>
                            <a class="qi-act-convert" href="<?php echo e(route('documents.convert-to-invoice-from-sales-order', $record->id)); ?>">→ Invoice</a>
                        <?php endif; ?>

                        <?php if(($config['key'] ?? '') === 'credit-notes' && ($record->status ?? '') !== 'cancelled' && empty($record->parent_document_id)): ?>
                            <a class="qi-act-link" href="<?php echo e(route('documents.apply-credit-note-form', $record->id)); ?>">Apply Credit</a>
                        <?php endif; ?>

                        <?php if(($config['key'] ?? '') === 'purchase-orders' && ($record->status ?? '') !== 'cancelled' && ($record->status ?? '') !== 'converted'): ?>
                            <a class="qi-act-convert" href="<?php echo e(route('supplier.documents.convert-to-grv', $record->id)); ?>">→ GRV</a>
                            <a class="qi-act-convert" href="<?php echo e(route('supplier.documents.convert-to-supplier-invoice', $record->id)); ?>">→ Invoice</a>
                        <?php endif; ?>

                        <?php if(($config['key'] ?? '') === 'grvs' && ($record->status ?? '') !== 'cancelled' && ($record->status ?? '') !== 'converted'): ?>
                            <a class="qi-act-convert" href="<?php echo e(route('supplier.documents.convert-to-supplier-invoice-from-grv', $record->id)); ?>">→ Invoice</a>
                        <?php endif; ?>

                        <?php if(($config['key'] ?? '') === 'supplier-credit-notes' && ($record->status ?? '') !== 'cancelled' && empty($record->parent_document_id)): ?>
                            <a class="qi-act-link" href="<?php echo e(route('supplier.documents.apply-credit-note-form', $record->id)); ?>">Apply Credit</a>
                        <?php endif; ?>
                    <?php else: ?>
                        <span class="qi-act-view">View</span>
                        <?php if($config['createUrl']): ?>
                            <a class="qi-act-edit" href="<?php echo e($config['createUrl']); ?>">Edit</a>
                        <?php endif; ?>

                        <!-- Receipt Linking -->
                        <?php if(($config['key'] ?? '') === 'receipts' && ($record->status ?? '') !== 'cancelled' && empty($record->invoice_id)): ?>
                            <a class="qi-act-link" href="<?php echo e(route('receipts.link-invoice-form', $record->id)); ?>">Link Invoice</a>
                        <?php endif; ?>

                        <?php if(property_exists($record, 'id')): ?>
                            <form method="POST" action="<?php echo e(route($config['destroyRoute'], [$config['key'], $record->id])); ?>" onsubmit="return confirm('Delete this record?')">
                                <?php echo csrf_field(); ?>
                                <?php echo method_field('DELETE'); ?>
                                <button class="qi-act-delete">Delete</button>
                            </form>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
            <div class="qi-empty">No records found.</div>
        <?php endif; ?>
    </div>

    <div class="qi-pagination"><?php echo e($records->withQueryString()->links()); ?></div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\Users\admin\Desktop\melsoft\resources\views/quanta-index/index.blade.php ENDPATH**/ ?>