
<?php
    $d = $d ?? null;
    $copyDates = $copyDates ?? collect();
    $suggestStart = $suggestStart ?? null;
    $suggestEnd = $suggestEnd ?? null;
    $startVal = $d && $d->start_date ? \Carbon\Carbon::parse($d->start_date)->format('Y-m-d') : ($suggestStart ?? '');
    $endVal = $d && $d->end_date ? \Carbon\Carbon::parse($d->end_date)->format('Y-m-d') : ($suggestEnd ?? '');
    // Tarih-seviyesi (pazaryeri-dışı) kopyalanabilir alanlar
    $priceFields = ['room_capacity','child_min_age','child_max_age','infant_min_age','infant_max_age','early_booking_discount','last_minute_discount','group_discount','special_discount','min_group_size','discount_reason'];

    // Pazaryeri fiyat matrisi verileri
    $mpFields = [
        ['key' => 'adult_price',       'label' => 'Yetişkin'],
        ['key' => 'single_supplement', 'label' => 'Tek Kişi Farkı'],
        ['key' => 'extra_bed_price',   'label' => 'Ek Yatak'],
        ['key' => 'child_price_1',     'label' => '1. Çocuk'],
        ['key' => 'child_price_2',     'label' => '2. Çocuk'],
        ['key' => 'infant_price',      'label' => 'Bebek'],
    ];
    $mpList = $tour->product?->marketplaces ?? collect();
    // Mevcut fiyatlar: [marketplace_id][classKey] => satır
    $mpExisting = [];
    if ($d) {
        foreach ($d->marketplacePrices as $mpp) {
            $ck = $mpp->tour_class_id ?? 'none';
            $mpExisting[$mpp->marketplace_id][$ck] = $mpp;
        }
    }
    $mpClasses = $pricingClasses ?? collect();
    $mpUseClasses = (bool) ($tour->use_pricing_classes ?? false);
    // Seri üretim yalnızca yeni tarih eklerken anlamlı (statik dates.blade ile aynı kural).
    $isNew = !$d;

    // ── Yaş aralıkları: yeni tarihte turun genel ayarından otomatik çekilir ──
    $ageFields = ['child_min_age', 'child_max_age', 'infant_min_age', 'infant_max_age'];
    $ageVals = [];
    foreach ($ageFields as $af) {
        $ageVals[$af] = $d ? $d->{$af} : ($tour->{$af} ?? null);
    }
    $agesEmpty = collect($ageVals)->every(fn($v) => $v === null);
    $agesMatchTour = collect($ageFields)->every(fn($af) => (string) ($ageVals[$af] ?? '') === (string) ($tour->{$af} ?? ''));
    $fmtAge = fn($min, $max) => ($min === null && $max === null) ? '—' : (($min ?? '?') . '–' . ($max ?? '?') . ' yaş');
    $ageSummary = 'Çocuk ' . $fmtAge($ageVals['child_min_age'], $ageVals['child_max_age']) . ' · Bebek ' . $fmtAge($ageVals['infant_min_age'], $ageVals['infant_max_age']);
    $ageChip = $agesEmpty ? ['Tanımsız', 'empty'] : ($agesMatchTour ? ['Turun genel ayarı', 'auto'] : ['Bu tarihe özel', 'custom']);
    $cppOn = (bool) ($d?->use_custom_payment_plan);
    $stages = is_array($d?->payment_schedule ?? null) ? $d->payment_schedule : [];
    $stageTriggers = ['at_booking' => 'Rezervasyonda', 'days_before_departure' => 'Kalkıştan X gün önce', 'on_departure' => 'Kalkışta'];
?>

<?php if($copyDates->count()): ?>
    <div class="qd-copybar">
        <i class="fas fa-wand-magic-sparkles"></i>
        <label>Fiyatları başka tarihten kopyala</label>
        <select class="qd-inp" data-qd-copyfrom>
            <option value="">— Tarih seç ve otomatik doldur —</option>
            <?php $__currentLoopData = $copyDates; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cd): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php if(!$d || $cd->id !== $d->id): ?>
                    <option value="<?php echo e($cd->id); ?>"
                            data-prices='<?php echo json_encode(collect($priceFields)->mapWithKeys(fn($f) => [$f => $cd->$f])->toArray(), 15, 512) ?>'
                            data-mp-prices='<?php echo json_encode($cd->marketplacePrices->reduce(function ($acc, $p) use ($mpFields) { $ck = $p->tour_class_id ?? 'none'; $acc[$p->marketplace_id][$ck] = collect($mpFields)->mapWithKeys(fn($f) => [$f['key'] => $p->{$f['key']}])->toArray(); return $acc; }, [])) ?>'>
                        <?php echo e($cd->code); ?> · <?php echo e($cd->start_date?->format('d.m.Y')); ?> · <?php echo e($cd->adult_price !== null ? number_format($cd->adult_price, 0) . ' ' . $cd->currency : 'fiyatsız'); ?>

                    </option>
                <?php endif; ?>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
    </div>
<?php endif; ?>

<div class="qd-fieldset">
    <div class="qd-fs-title"><i class="fas fa-calendar-day"></i> Tarih & Süre</div>
    <div class="qd-form">
        <div class="qd-fld"><label>Başlangıç</label><input type="date" name="start_date" value="<?php echo e($startVal); ?>" class="qd-inp" data-qd-start required></div>
        <div class="qd-fld"><label>Bitiş <span class="qd-auto">otomatik</span></label><input type="date" name="end_date" value="<?php echo e($endVal); ?>" class="qd-inp" data-qd-end required></div>
    </div>
</div>

<div class="qd-fieldset">
    <div class="qd-fs-title"><i class="fas fa-bed"></i> Oda</div>
    <div class="qd-form">
        <div class="qd-fld"><label>Oda Kapasitesi</label><input type="number" min="1" name="room_capacity" value="<?php echo e($d?->room_capacity); ?>" class="qd-inp" placeholder="1 odada maks kişi"></div>
    </div>
</div>


<?php echo $__env->make('agency.products._qv-marketplace-prices', ['marketplaces' => $mpList, 'existing' => $mpExisting, 'fields' => $mpFields, 'classes' => $mpClasses, 'useClasses' => $mpUseClasses, 'collapsible' => true], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

<div class="qd-fieldset">
    <div class="qd-fs-title"><i class="fas fa-users"></i> Kapasite</div>
    <div class="qd-form">
        <div class="qd-fld"><label>Toplam Kapasite</label><input type="number" name="total_capacity" value="<?php echo e($d?->total_capacity); ?>" class="qd-inp" required></div>
        <div class="qd-fld"><label>Min Katılımcı</label><input type="number" name="min_participants" value="<?php echo e($d?->min_participants); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Yetişkin Kapasitesi</label><input type="number" min="0" name="adult_capacity" value="<?php echo e($d?->adult_capacity); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Çocuk Kapasitesi</label><input type="number" min="0" name="child_capacity" value="<?php echo e($d?->child_capacity); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Bebek Kapasitesi</label><input type="number" min="0" name="infant_capacity" value="<?php echo e($d?->infant_capacity); ?>" class="qd-inp"></div>
    </div>
</div>


<div class="qd-fieldset qd-fold" data-qd-fold>
    <button type="button" class="qd-fold-head" data-qd-fold-toggle aria-expanded="false">
        <span class="qd-fold-ico"><i class="fas fa-child-reaching"></i></span>
        <span class="qd-fold-info">
            <b>Yaş Aralıkları</b>
            <small data-qd-ages-summary><?php echo e($ageSummary); ?></small>
        </span>
        <span class="qd-fold-chip is-<?php echo e($ageChip[1]); ?>" data-qd-ages-chip><?php echo e($ageChip[0]); ?></span>
        <i class="fas fa-chevron-down qd-fold-chev"></i>
    </button>
    <div class="qd-fold-body" data-qd-fold-body hidden>
        <p class="qd-hint" style="margin-top:0">Boş bırakılan alanlar için turun genel ayarı geçerlidir. Buradaki değerler yalnızca bu tarihe uygulanır.</p>
        <div class="qd-form">
            <div class="qd-fld"><label>Çocuk Min Yaş</label><input type="number" name="child_min_age" value="<?php echo e($ageVals['child_min_age']); ?>" class="qd-inp" placeholder="<?php echo e($tour->child_min_age ?? 2); ?>" data-qd-age="cmin"></div>
            <div class="qd-fld"><label>Çocuk Max Yaş</label><input type="number" name="child_max_age" value="<?php echo e($ageVals['child_max_age']); ?>" class="qd-inp" placeholder="<?php echo e($tour->child_max_age ?? 12); ?>" data-qd-age="cmax"></div>
            <div class="qd-fld"><label>Bebek Min Yaş</label><input type="number" name="infant_min_age" value="<?php echo e($ageVals['infant_min_age']); ?>" class="qd-inp" placeholder="<?php echo e($tour->infant_min_age ?? 0); ?>" data-qd-age="imin"></div>
            <div class="qd-fld"><label>Bebek Max Yaş</label><input type="number" name="infant_max_age" value="<?php echo e($ageVals['infant_max_age']); ?>" class="qd-inp" placeholder="<?php echo e($tour->infant_max_age ?? 2); ?>" data-qd-age="imax"></div>
        </div>
    </div>
</div>

<div class="qd-fieldset">
    <div class="qd-fs-title"><i class="fas fa-tags"></i> İndirimler</div>
    <div class="qd-form">
        <div class="qd-fld"><label>Erken Rez. %</label><input type="number" step="0.01" name="early_booking_discount" value="<?php echo e($d?->early_booking_discount); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Son Dakika %</label><input type="number" step="0.01" name="last_minute_discount" value="<?php echo e($d?->last_minute_discount); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Grup %</label><input type="number" step="0.01" name="group_discount" value="<?php echo e($d?->group_discount); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Grup Min Kişi</label><input type="number" min="0" name="min_group_size" value="<?php echo e($d?->min_group_size); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Özel %</label><input type="number" step="0.01" name="special_discount" value="<?php echo e($d?->special_discount); ?>" class="qd-inp"></div>
        <div class="qd-fld"><label>Özel İnd. Açıklama</label><input type="text" name="discount_reason" value="<?php echo e($d?->discount_reason); ?>" class="qd-inp"></div>
    </div>
</div>

<div class="qd-fieldset">
    <div class="qd-fs-title"><i class="fas fa-hourglass-half"></i> Geçerlilik & Rezervasyon Zamanları</div>
    <div class="qd-form">
        <?php echo $__env->make('agency.products._qv-deadline-rule', ['name' => 'early_booking_rule', 'label' => 'Erken Rezervasyon indirimi — şu ana kadar geçerli', 'hint' => '(bu ana kadar rezervasyonda indirim)', 'rule' => $d?->early_booking_rule, 'anchors' => ['departure' => 'Kalkış', 'publish' => 'Yayın Tarihi']], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
        <?php echo $__env->make('agency.products._qv-deadline-rule', ['name' => 'last_minute_rule', 'label' => 'Son Dakika indirimi — şu andan itibaren geçerli', 'hint' => '(bu andan sonra rezervasyonda indirim)', 'rule' => $d?->last_minute_rule, 'anchors' => ['departure' => 'Kalkış', 'booking_close' => 'Rez. Kapanışı']], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
        <?php echo $__env->make('agency.products._qv-deadline-rule', ['name' => 'booking_deadline_rule', 'label' => 'Son Rezervasyon — rezervasyon şu ana kadar açık', 'hint' => '(bu andan sonra rezervasyon kapanır)', 'rule' => $d?->booking_deadline_rule, 'anchors' => ['departure' => 'Kalkış', 'publish' => 'Yayın Tarihi']], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
    </div>
</div>

<div class="qd-fieldset">
    <div class="qd-fs-title"><i class="fas fa-toggle-on"></i> Sezon & Durum</div>
    <div class="qd-form">
        <div class="qd-fld"><label>Sezon</label><select name="season_type" class="qd-inp"><?php $__currentLoopData = ['low'=>'Düşük','medium'=>'Orta','high'=>'Yüksek','peak'=>'Zirve']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v => $l): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($v); ?>" <?php if(($d->season_type ?? 'medium') === $v): echo 'selected'; endif; ?>><?php echo e($l); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div>
        <div class="qd-fld"><label>Durum</label><select name="status" class="qd-inp"><?php $__currentLoopData = ['draft'=>'Taslak','published'=>'Yayında','booking_open'=>'Rez. Açık','booking_closed'=>'Rez. Kapalı','confirmed'=>'Onaylı','in_progress'=>'Devam Ediyor','completed'=>'Tamamlandı','cancelled'=>'İptal']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v => $l): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($v); ?>" <?php if(($d->status ?? 'booking_open') === $v): echo 'selected'; endif; ?>><?php echo e($l); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div>
    </div>
    <div class="qd-form">
        <div class="qd-fld full"><label>Notlar</label><textarea name="special_notes" class="qd-inp"><?php echo e($d?->special_notes); ?></textarea></div>
    </div>
    <label class="qd-checks">
        <span><input type="checkbox" name="is_active" value="1" <?php echo e(!$d || $d->is_active ? 'checked' : ''); ?>> Aktif</span>
        <span><input type="checkbox" name="is_bookable" value="1" <?php echo e(!$d || $d->is_bookable ? 'checked' : ''); ?>> Rezervasyona Açık</span>
        <span><input type="checkbox" name="guarantee_departure" value="1" <?php echo e($d && $d->guarantee_departure ? 'checked' : ''); ?>> Kesin Kalkış</span>
        <span><input type="checkbox" name="auto_guarantee_on_min" value="1" <?php echo e($d ? ($d->auto_guarantee_on_min ? 'checked' : '') : (($tour->auto_guarantee_on_min ?? false) ? 'checked' : '')); ?>> Min katılımcıda otomatik kesin kalkış</span>
    </label>
</div>


<div class="qd-fieldset">
    <div class="qd-fs-title"><i class="fas fa-money-check-dollar"></i> Bu Tarihe Özel Ödeme Planı</div>
    <label class="qd-checks" style="margin-top:0">
        <span><input type="checkbox" name="use_custom_payment_plan" value="1" data-qd-cpp-toggle <?php if($cppOn): echo 'checked'; endif; ?>> Bu tarih için özel kademeli ödeme planı kullan</span>
    </label>
    <div data-qd-cpp-wrap style="<?php echo e($cppOn ? '' : 'display:none'); ?>">
        <p class="qd-hint">Aşamalar toplamı %100 olmalıdır. Boş bırakılırsa turun genel ödeme planı geçerlidir.</p>
        <div class="pp-order-note"><i class="fas fa-circle-info"></i> Ödemeler, aşamaları <strong>eklediğiniz sıraya göre</strong> (1, 2, 3…) tahsil edilir. Numaralar ödeme sırasını gösterir.</div>
        <div data-qd-stage-rows>
            <?php $__currentLoopData = $stages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $st): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <div class="qd-form qd-stage-row">
                    <div class="qd-fld"><label>Etiket</label><input type="text" name="payment_schedule[<?php echo e($i); ?>][label]" value="<?php echo e($st['label'] ?? ''); ?>" class="qd-inp" placeholder="Ör: Kapora" <?php if(!$cppOn): echo 'disabled'; endif; ?>></div>
                    <div class="qd-fld"><label>Yüzde %</label><input type="number" min="0" max="100" name="payment_schedule[<?php echo e($i); ?>][percent]" value="<?php echo e($st['percent'] ?? ''); ?>" class="qd-inp" <?php if(!$cppOn): echo 'disabled'; endif; ?>></div>
                    <div class="qd-fld"><label>Tetikleyici</label><select name="payment_schedule[<?php echo e($i); ?>][trigger]" class="qd-inp" <?php if(!$cppOn): echo 'disabled'; endif; ?>><?php $__currentLoopData = $stageTriggers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v => $l): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($v); ?>" <?php if(($st['trigger'] ?? '') === $v): echo 'selected'; endif; ?>><?php echo e($l); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div>
                    <div class="qd-fld"><label>Gün (X)</label><input type="number" min="0" name="payment_schedule[<?php echo e($i); ?>][offset_days]" value="<?php echo e($st['offset_days'] ?? ''); ?>" class="qd-inp" <?php if(!$cppOn): echo 'disabled'; endif; ?>></div>
                    <div class="qd-fld qd-stage-act"><button type="button" class="pqv-iconbtn danger" data-qd-stage-remove title="Aşamayı sil"><i class="fas fa-times"></i></button></div>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </div>
        <button type="button" class="pqv-btn pqv-btn-soft" data-qd-stage-add><i class="fas fa-plus"></i> Aşama Ekle</button>
    </div>
</div>

<?php if($isNew): ?>
    
    <div class="qd-fieldset">
        <div class="qd-fs-title"><i class="fas fa-repeat"></i> Seri Tarih Oluştur</div>
        <label class="qd-checks" style="margin-top:0">
            <span><input type="checkbox" name="__series_enabled" value="1" data-qd-series-toggle> Seri oluştur (aynı fiyatlarla birden çok tarih)</span>
        </label>
        <div data-qd-series-wrap style="display:none">
            <div class="qd-form">
                <div class="qd-fld"><label>Tekrar Sıklığı</label><select name="repeat_frequency" class="qd-inp" disabled><?php $__currentLoopData = ['weekly' => 'Haftalık', 'biweekly' => '2 Haftada bir', 'monthly' => 'Aylık', 'interval' => 'Her N günde bir']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v => $l): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($v); ?>"><?php echo e($l); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div>
                <div class="qd-fld"><label>Gün Aralığı (N)</label><input type="number" name="repeat_interval_days" min="1" max="365" class="qd-inp" placeholder="Örn: 10" disabled></div>
                <div class="qd-fld"><label>Kaç Adet Tarih?</label><input type="number" name="repeat_count" min="1" max="52" class="qd-inp" placeholder="Örn: 8" disabled></div>
            </div>
            <p class="qd-hint">İlk tarih yukarıdaki başlangıç/bitiş tarihidir; sonrakiler seçilen sıklıkla ötelenir.</p>
        </div>
    </div>
<?php endif; ?>
<?php /**PATH C:\xampp\htdocs\acente\resources\views/agency/products/_qv-date-fields-kultur.blade.php ENDPATH**/ ?>