
<?php
    $marketplaces = $marketplaces ?? collect();
    $existing     = $existing ?? [];
    $classes      = $classes ?? collect();
    $useClasses   = ($useClasses ?? false) && $classes->isNotEmpty();
    $classRows    = $useClasses ? $classes->values() : collect([null]);

    $fields = collect($fields ?? [])->map(fn ($f) => [
        'key'   => $f['key'],
        'short' => $f['short'] ?? ($f['label'] ?? $f['key']),
    ])->values()->all();

    // Ana pazar başa: matrisin tabanı ilk satırdır (motor "ilk kart taban" der)
    $primaryMpId = (int) (($tour ?? null)?->product?->primary_marketplace_id ?? 0);
    $primaryExplicit = $primaryMpId > 0 && $marketplaces->contains('id', $primaryMpId);
    if (!$primaryExplicit) {
        $primaryMpId = (int) (optional($marketplaces->first(fn($m) => $m->defaultCurrency && $m->defaultCurrency->is_base))->id
            ?? (optional($marketplaces->first())->id ?? 0));
    }
    $marketplaces = $marketplaces->sortBy(fn($m) => $m->id === $primaryMpId ? 0 : 1)->values();
?>

<?php if($marketplaces->isEmpty()): ?>
    <div class="qvdp-empty"><i class="fas fa-store-slash"></i> Tanımlı pazaryeri yok — <b>Ayarlar → Pazaryerleri</b>'nden ekleyin.</div>
<?php else: ?>
    <div class="qvdp-wrap" data-mpp data-use-classes="<?php echo e($useClasses ? 1 : 0); ?>"
         data-mpp-ids="<?php echo e($marketplaces->pluck('id')->implode(',')); ?>"
         data-mpp-primary-id="<?php echo e($primaryExplicit ? $primaryMpId : ''); ?>">
        <table class="qvdp">
            <thead>
                <tr>
                    <th></th>
                    <?php $__currentLoopData = $fields; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $f): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><th><?php echo e($f['short']); ?></th><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tr>
            </thead>
            <?php $__currentLoopData = $marketplaces; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $mp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php
                    $cur = $mp->defaultCurrency;
                    $isPrimary = $i === 0;
                ?>
                <tbody data-mpp-row data-mp-id="<?php echo e($mp->id); ?>" data-mp-name="<?php echo e(mb_strtolower($mp->name)); ?>"
                       data-currency="<?php echo e($cur->code ?? 'TRY'); ?>" data-rate="<?php echo e($cur->exchange_rate ?? 1); ?>"
                       class="<?php echo e($isPrimary ? 'is-primary' : ''); ?>">
                    <?php if(!$cur): ?>
                        <tr><th class="qvdp-mp"><?php echo e($mp->name); ?></th>
                            <td colspan="<?php echo e(count($fields)); ?>" class="qvdp-nocur"><i class="fas fa-triangle-exclamation"></i> Para birimi atanmamış</td></tr>
                    <?php else: ?>
                        <?php $__currentLoopData = $classRows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cls): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php
                                $classKey  = $cls ? $cls->id : 'none';
                                $isBaseCls = $cls ? $cls->is_base : true;
                                $ex = $existing[$mp->id][$classKey] ?? null;
                            ?>
                            <tr data-mpp-classrow data-class-id="<?php echo e($classKey); ?>" data-is-base="<?php echo e($isBaseCls ? 1 : 0); ?>"
                                <?php if($cls): ?> data-diff-type="<?php echo e($cls->diff_type); ?>" data-diff-value="<?php echo e((float) $cls->diff_value); ?>" <?php endif; ?>>
                                <th class="qvdp-mp <?php echo e($loop->first ? '' : 'qvdp-cls'); ?>">
                                    <?php if($loop->first): ?>
                                        <?php if($isPrimary): ?><i class="fas fa-star qvdp-star" title="Ana pazar — diğer pazarlar bu fiyattan kurla hesaplanır"></i><?php endif; ?>
                                        <?php echo e($mp->name); ?> <small><?php echo e($cur->code); ?></small>
                                        <?php if($useClasses && $cls): ?><b class="qvdp-tag <?php echo e($isBaseCls ? 'base' : ''); ?>"><?php echo e($cls->code ?: $cls->name); ?></b><?php endif; ?>
                                    <?php else: ?>
                                        <b class="qvdp-tag <?php echo e($isBaseCls ? 'base' : ''); ?>"><?php echo e($cls->code ?: $cls->name); ?></b>
                                        <small><?php echo e($isBaseCls ? 'taban' : '+' . (float) $cls->diff_value . ($cls->diff_type === 'percent' ? '%' : '')); ?></small>
                                    <?php endif; ?>
                                </th>
                                <?php $__currentLoopData = $fields; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $f): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <td>
                                        <input type="number" step="0.01" min="0" inputmode="decimal"
                                               name="marketplace_prices[<?php echo e($mp->id); ?>][<?php echo e($classKey); ?>][<?php echo e($f['key']); ?>]"
                                               value="<?php echo e(optional($ex)[$f['key']] ?? ''); ?>"
                                               data-mpp-field="<?php echo e($f['key']); ?>"
                                               <?php if(optional($ex)[$f['key']] !== null && (!$isPrimary || !$isBaseCls)): ?> data-manual="1" <?php endif; ?>
                                               placeholder="0" title="<?php echo e($f['short']); ?> — <?php echo e($mp->name); ?>">
                                    </td>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <?php endif; ?>
                </tbody>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </table>
        <div class="qvdp-hint">
            <i class="fas fa-wand-magic-sparkles"></i>
            İlk satır tabandır; diğer pazarlar kurdan otomatik hesaplanır, <b>elle girilen hücre</b> (mor) korunur.
        </div>
    </div>
<?php endif; ?>

<?php if (! $__env->hasRenderedOnce('c654f0c4-369b-4c90-81b7-af9f21ce7c8d')): $__env->markAsRenderedOnce('c654f0c4-369b-4c90-81b7-af9f21ce7c8d'); ?>
<style>
    /* ── Minimal fiyat tablosu (qvdp) ── */
    .qvdp-wrap { overflow-x: auto; }
    .qvdp { width: 100%; border-collapse: collapse; }
    .qvdp thead th {
        padding: 0 4px 3px; text-align: center;
        font-size: .56rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; color: #9aa7bb;
    }
    .qvdp thead th:first-child { text-align: left; }
    .qvdp td, .qvdp tbody th { padding: 2px 3px; }
    .qvdp tbody + tbody tr:first-child th, .qvdp tbody + tbody tr:first-child td { border-top: 1px solid #f1f5f9; padding-top: 4px; }
    .qvdp tbody tr:hover th, .qvdp tbody tr:hover td { background: #f7faff; }

    .qvdp-mp { text-align: left; font-size: .74rem; font-weight: 750; color: #0f172a; white-space: nowrap; }
    .qvdp-mp small { font-size: .58rem; font-weight: 800; color: #b6c2d2; letter-spacing: .03em; }
    .qvdp-mp.qvdp-cls { padding-left: 16px; font-weight: 600; color: #64748b; }
    .qvdp-star { font-size: .58rem; color: #f59e0b; margin-right: 3px; }
    .qvdp-tag {
        display: inline-block; font-size: .54rem; font-weight: 800; line-height: 1;
        color: #64748b; background: #f1f5f9; border-radius: 5px; padding: 2px 5px;
    }
    .qvdp-tag.base { color: #4338ca; background: #eef2ff; }

    .qvdp input {
        width: 100%; min-width: 68px; box-sizing: border-box;
        border: 1.5px solid #e2e8f0; border-radius: 8px; background: #fff;
        padding: 3px 7px; text-align: right;
        font: inherit; font-size: .76rem; font-weight: 700; color: #0f172a;
        font-variant-numeric: tabular-nums;
        -moz-appearance: textfield; appearance: textfield;
        transition: border-color .13s, box-shadow .13s;
    }
    .qvdp input::-webkit-outer-spin-button, .qvdp input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
    .qvdp input:focus { outline: none; border-color: #2563eb; box-shadow: 0 0 0 3px rgba(37, 99, 235, .1); }
    /* Motorun durum işaretleri: elle / otomatik / boş */
    .qvdp input[data-manual="1"] { border-color: #c4b5fd; background: #fbfaff; box-shadow: inset 2px 0 0 #8b5cf6; }
    .qvdp input[data-auto="1"] { border-color: #dbeafe; background: #f8fbff; }
    .qvdp input[data-blank="1"] { border-style: dashed; border-color: #fcd34d; background: #fffdf5; }
    .qvdp .is-primary tr[data-is-base="1"] input { font-weight: 800; }

    .qvdp-nocur { font-size: .68rem; font-weight: 650; color: #b45309; }
    .qvdp-nocur i { color: #f59e0b; margin-right: 3px; }

    .qvdp-hint { display: flex; align-items: baseline; gap: 5px; margin-top: 4px; font-size: .62rem; font-weight: 600; color: #b6c2d2; }
    .qvdp-hint i { font-size: .58rem; }
    .qvdp-hint b { color: #8b5cf6; }

    .qvdp-empty { font-size: .72rem; font-weight: 600; color: #94a3b8; padding: .3rem 0; }
    .qvdp-empty i { color: #cbd5e1; margin-right: 4px; }
</style>
<?php endif; ?>
<?php /**PATH C:\xampp\htdocs\acente\resources\views/agency/products/_qv-date-prices-mini.blade.php ENDPATH**/ ?>