<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-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(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> 
        <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-100 leading-tight">
            <?php echo e(__('Bulk Import')); ?>

        </h2>
     <?php $__env->endSlot(); ?>

    <div class="py-12">
        <div class="max-w-6xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    <div class="mb-6">
                        <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-1">Paste your list, we'll do the rest</h3>
                        <p class="text-sm text-gray-600 dark:text-gray-400">
                            One item per line as <code class="bg-gray-100 dark:bg-gray-700 px-1 rounded">Name - $Price</code>
                            (or <code class="bg-gray-100 dark:bg-gray-700 px-1 rounded">$Price - Name</code>).
                            Lines below an item become its description. You'll review everything before anything is created.
                        </p>
                    </div>

                    <form method="POST" action="<?php echo e(route('items.store-mass-listing')); ?>">
                        <?php echo csrf_field(); ?>
                        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                            
                            <div>
                                <textarea
                                    id="items_text"
                                    name="items_text"
                                    rows="18"
                                    class="block w-full border-gray-300 dark:border-gray-600 dark:bg-gray-900 dark:text-gray-100 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm font-mono text-sm"
                                    placeholder="Polyend Tracker - $275

Mutable Instruments Beads - $360
with original box, v1.2 firmware

Akai MPKmini mk2 - $70"
                                    required><?php echo e(old('items_text')); ?></textarea>
                            </div>

                            
                            <div>
                                <div class="border border-dashed border-gray-300 dark:border-gray-600 rounded-md h-full min-h-48 p-4 bg-gray-50 dark:bg-gray-900/50">
                                    <div class="flex items-center justify-between mb-3">
                                        <h4 class="text-sm font-medium text-gray-700 dark:text-gray-300">Preview</h4>
                                        <span id="preview-count" class="text-xs text-gray-400 dark:text-gray-500"></span>
                                    </div>

                                    <p id="preview-empty" class="text-sm text-gray-400 dark:text-gray-500">
                                        Items appear here as you type, so you can check they're being read correctly.
                                    </p>

                                    <ul id="preview-list" class="space-y-1 text-sm max-h-96 overflow-y-auto"></ul>

                                    <p id="preview-skipped" class="hidden mt-3 text-xs text-amber-600 dark:text-amber-400"></p>
                                </div>
                            </div>
                        </div>

                        <div class="mt-6 flex items-center justify-between">
                            <a href="<?php echo e(route('items.my-listings')); ?>" class="text-sm text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200">Cancel</a>
                            <?php if (isset($component)) { $__componentOriginald411d1792bd6cc877d687758b753742c = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginald411d1792bd6cc877d687758b753742c = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.primary-button','data' => ['id' => 'parse-btn']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('primary-button'); ?>
<?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(['id' => 'parse-btn']); ?>
                                <?php echo e(__('Review items')); ?>

                             <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginald411d1792bd6cc877d687758b753742c)): ?>
<?php $attributes = $__attributesOriginald411d1792bd6cc877d687758b753742c; ?>
<?php unset($__attributesOriginald411d1792bd6cc877d687758b753742c); ?>
<?php endif; ?>
<?php if (isset($__componentOriginald411d1792bd6cc877d687758b753742c)): ?>
<?php $component = $__componentOriginald411d1792bd6cc877d687758b753742c; ?>
<?php unset($__componentOriginald411d1792bd6cc877d687758b753742c); ?>
<?php endif; ?>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Mirrors the server-side header patterns in ItemController::parseItemsText —
        // preview only; the server parse remains the source of truth.
        const HEADER_PATTERNS = [
            /^\$(\d+(?:\.\d{2})?)\s*-\s*(.+)$/,   // $price - title
            /^(.+?)\s*-\s*\$(\d+(?:\.\d{2})?)$/,  // title - $price
            /^\$(\d+(?:\.\d{2})?)\s+(.+)$/,       // $price title
            /^(.+?)\s+\$(\d+(?:\.\d{2})?)$/,      // title $price
        ];

        function parseHeader(line) {
            for (let i = 0; i < HEADER_PATTERNS.length; i++) {
                const m = line.match(HEADER_PATTERNS[i]);
                if (m) {
                    const priceFirst = (i === 0 || i === 2);
                    return {
                        price: parseFloat(priceFirst ? m[1] : m[2]),
                        title: (priceFirst ? m[2] : m[1]).trim(),
                    };
                }
            }
            return null;
        }

        function renderPreview() {
            const text = document.getElementById('items_text').value;
            const list = document.getElementById('preview-list');
            const empty = document.getElementById('preview-empty');
            const count = document.getElementById('preview-count');
            const skippedNote = document.getElementById('preview-skipped');
            const btn = document.getElementById('parse-btn');

            const items = [];
            let skipped = 0;
            let sawHeader = false;

            for (const raw of text.split('\n')) {
                const line = raw.trim();
                if (!line) continue;
                const header = parseHeader(line);
                if (header) {
                    items.push(header);
                    sawHeader = true;
                } else if (!sawHeader) {
                    skipped++; // text before any recognizable item is dropped by the parser
                }
                // lines after a header are that item's description — always kept
            }

            list.innerHTML = '';
            for (const item of items.slice(0, 100)) {
                const li = document.createElement('li');
                li.className = 'flex items-baseline gap-2';

                const price = document.createElement('span');
                price.className = 'font-semibold text-gray-700 dark:text-gray-300 w-14 shrink-0 text-right';
                price.textContent = '$' + Math.round(item.price);

                const title = document.createElement('span');
                title.className = 'text-gray-900 dark:text-gray-100 truncate';
                title.textContent = item.title;

                li.append(price, title);
                list.appendChild(li);
            }

            empty.classList.toggle('hidden', items.length > 0);
            count.textContent = items.length > 0
                ? items.length + (items.length === 1 ? ' item found' : ' items found')
                : '';

            skippedNote.classList.toggle('hidden', skipped === 0);
            if (skipped > 0) {
                skippedNote.textContent = skipped + (skipped === 1 ? ' line' : ' lines')
                    + " before the first item couldn't be read and will be skipped."
                    + ' Start each item with a name and price.';
            }

            btn.textContent = items.length > 0
                ? 'Review ' + items.length + (items.length === 1 ? ' item' : ' items')
                : 'Review items';
        }

        const textarea = document.getElementById('items_text');
        textarea.addEventListener('input', renderPreview);
        document.addEventListener('DOMContentLoaded', renderPreview);
    </script>

    
    <?php
        $parseError = $errors->first('items_text') ?: session('error');
    ?>

    <?php if($parseError): ?>
    <div class="fixed bottom-6 left-1/2 -translate-x-1/2 z-50 max-w-md w-full px-4" id="parse-error-toast">
        <div class="bg-red-600 text-white rounded-lg shadow-lg px-4 py-3 text-sm flex items-start gap-3">
            <span class="flex-1"><?php echo e($parseError); ?></span>
            <button onclick="document.getElementById('parse-error-toast').remove()" class="font-bold shrink-0">&times;</button>
        </div>
    </div>
    <?php endif; ?>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php /**PATH /var/www/html/resources/views/items/mass-listing.blade.php ENDPATH**/ ?>