
<?php
    $marketplaces = $marketplaces ?? collect();
    $existing     = $existing ?? [];
    $fields       = $fields ?? [];
    $namePrefix   = $namePrefix ?? 'marketplace_prices';
    $classes      = $classes ?? collect();
    $useClasses   = ($useClasses ?? false) && $classes->isNotEmpty();
    // Satır listesi: sınıflıysa sınıflar, değilse tek "none"
    $classRows = $useClasses ? $classes->values() : collect([null]);

    // Ana pazar: üründe tanımlıysa o; değilse base para birimli pazar; o da yoksa ilk.
    // Ana pazar kartı listenin başına alınır (matris hesapları ilk karta "is-primary" der).
    $primaryMpId = (int) (($tour ?? null)?->product?->primary_marketplace_id ?? 0);
    if (!$marketplaces->contains('id', $primaryMpId)) {
        $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();

    // Açılır-kapanır mod (tarih formlarında yer kazanmak için; qd-fold stilleri _qv-dates-ui'de)
    $collapsible = $collapsible ?? false;
    if ($collapsible) {
        $primaryMp = $marketplaces->first();
        $baseKey = $useClasses ? (optional($classes->firstWhere('is_base', true))->id ?? 'none') : 'none';
        $primaryAdult = $primaryMp ? ($existing[$primaryMp->id][$baseKey] ?? null)?->adult_price : null;
        $mppSummary = $primaryAdult !== null
            ? 'Yetişkin ' . number_format((float) $primaryAdult, 0, ',', '.') . ' ' . ($primaryMp->defaultCurrency->code ?? '') . ' · ana pazar'
            : 'Henüz fiyat girilmedi';
    }
?>

<?php if($collapsible): ?>
<div class="qd-fieldset qd-fold" data-qd-fold data-qd-mppfold>
    <button type="button" class="qd-fold-head" data-qd-fold-toggle aria-expanded="false">
        <span class="qd-fold-ico"><i class="fas fa-store"></i></span>
        <span class="qd-fold-info">
            <b>Pazaryeri Fiyatları <?php if($useClasses): ?><span class="mpp-cls-badge" style="margin-left:.3rem"><i class="fas fa-layer-group"></i> Sınıflı</span><?php endif; ?></b>
            <small data-qd-mpp-summary><?php echo e($mppSummary); ?></small>
        </span>
        <span class="qd-fold-chip <?php echo e($marketplaces->isEmpty() ? 'is-empty' : 'is-auto'); ?>"><?php echo e($marketplaces->count()); ?> pazaryeri</span>
        <i class="fas fa-chevron-down qd-fold-chev"></i>
    </button>
    <div class="qd-fold-body" data-qd-fold-body hidden>
<?php endif; ?>

<div class="mpp-section">
    <?php if (! ($collapsible)): ?>
    <div class="mpp-section-title">
        <i class="fas fa-store"></i> Pazaryeri Fiyatları
        <?php if($useClasses): ?><span class="mpp-cls-badge"><i class="fas fa-layer-group"></i> Sınıflı</span><?php endif; ?>
    </div>
    <?php endif; ?>

    <?php if($marketplaces->isEmpty()): ?>
        <div class="mpp-empty">
            <i class="fas fa-store-slash"></i>
            <div>Bu tura tanımlı <strong>pazaryeri</strong> yok. <strong>Genel</strong> sekmesindeki <strong>Pazaryerleri</strong> bölümünden ekleyin.</div>
        </div>
    <?php else: ?>
        <div class="mpp-wrap" data-mpp data-use-classes="<?php echo e($useClasses ? 1 : 0); ?>">
            <div class="mpp-toolbar">
                <label class="mpp-auto">
                    <input type="checkbox" data-mpp-auto checked>
                    <span><i class="fas fa-wand-magic-sparkles"></i> <strong>Otomatik hesapla</strong>: ana pazar taban fiyatından <?php if($useClasses): ?>sınıflar + <?php endif; ?> diğer pazarlar (kurdan)</span>
                </label>
                <span class="mpp-hint">Her hücre elle de düzenlenebilir</span>
            </div>

            <?php $__currentLoopData = $marketplaces; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $mp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php $cur = $mp->defaultCurrency; $isPrimary = $i === 0; ?>
                <div class="mpp-card <?php echo e($isPrimary ? 'is-primary' : ''); ?>" data-mpp-row
                     data-mp-id="<?php echo e($mp->id); ?>" data-currency="<?php echo e($cur->code ?? 'TRY'); ?>" data-rate="<?php echo e($cur->exchange_rate ?? 1); ?>">
                    <div class="mpp-head">
                        <div class="mpp-name">
                            <span class="mpp-ico"><?php echo e(strtoupper(mb_substr($mp->name, 0, 2))); ?></span>
                            <span><?php echo e($mp->name); ?></span>
                        </div>
                        <div class="mpp-badges">
                            <?php if($isPrimary): ?><span class="mpp-badge primary"><i class="fas fa-star"></i> Ana pazar</span><?php endif; ?>
                            <span class="mpp-cur"><?php echo e($cur->code ?? 'Para birimi yok'); ?></span>
                            <?php if (! ($isPrimary)): ?>
                                <div class="mpp-copy">
                                    <button type="button" class="mpp-copy-btn" data-mpp-copy-toggle><i class="fas fa-copy"></i> Kopyala</button>
                                    <div class="mpp-copy-menu" data-mpp-copy-menu>
                                        <div class="mpp-copy-title">Şu pazardan kurdan çevir:</div>
                                        <?php $__currentLoopData = $marketplaces; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $src): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <?php if($src->id !== $mp->id): ?>
                                                <button type="button" data-mpp-copy-from="<?php echo e($src->id); ?>">
                                                    <i class="fas fa-arrow-right-long"></i> <?php echo e($src->name); ?> <small><?php echo e($src->defaultCurrency->code ?? '—'); ?></small>
                                                </button>
                                            <?php endif; ?>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <?php if(!$cur): ?>
                        <div class="mpp-nocur"><i class="fas fa-triangle-exclamation"></i> Bu pazaryerine para birimi atanmamış. <strong>Ayarlar → Pazaryerleri</strong>'nden atayın.</div>
                    <?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;
                            ?>
                            <div class="mpp-classrow <?php echo e($isBaseCls ? 'is-base-cls' : ''); ?>" 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; ?>>
                                <?php if($useClasses): ?>
                                    <div class="mpp-cls-head">
                                        <span class="mpp-cls-name"><?php echo e($cls->code ?: mb_substr($cls->name, 0, 2)); ?></span>
                                        <span class="mpp-cls-full"><?php echo e($cls->name); ?></span>
                                        <?php if($isBaseCls): ?>
                                            <span class="mpp-cls-tag base">taban</span>
                                        <?php else: ?>
                                            <span class="mpp-cls-tag">+<?php echo e((float) $cls->diff_value); ?><?php echo e($cls->diff_type === 'percent' ? '%' : ''); ?></span>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                                <div class="mpp-grid">
                                    <?php $__currentLoopData = $fields; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $f): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <div class="mpp-fld">
                                            <label><?php echo e($f['label']); ?></label>
                                            <div class="mpp-inp">
                                                <input type="number" step="0.01" min="0" inputmode="decimal"
                                                       name="<?php echo e($namePrefix); ?>[<?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">
                                                <span class="mpp-suffix"><?php echo e($cur->code); ?></span>
                                            </div>
                                        </div>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </div>
                            </div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <?php endif; ?>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
    <?php endif; ?>
</div>

<?php if($collapsible): ?>
    </div>
</div>
<?php endif; ?>
<?php /**PATH C:\xampp\htdocs\acente\resources\views/agency/products/_qv-marketplace-prices.blade.php ENDPATH**/ ?>