@extends('agency.layouts.app')

@section('title', $tour->name . ' - Fiyatlandırma')
@section('page_title', 'Tur Fiyatlandırması')

@php
    $breadcrumbs = [
        ['title' => 'Ana Sayfa', 'url' => route('agency.dashboard')],
        ['title' => 'Günübirlik Turlar', 'url' => route('agency.products.gunubirlik-turlar.index')],
        ['title' => $tour->name, 'url' => route('agency.products.gunubirlik-turlar.show', $tour)],
        ['title' => 'Fiyatlandırma', 'url' => '']
    ];
@endphp

@push('styles')
    <style>
        .tour-detail-container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .tour-header {
            background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
            color: white;
            padding: 2rem;
            border-radius: var(--radius-xl);
            margin-bottom: 2rem;
            position: relative;
            overflow: hidden;
        }

        .tour-header::before {
            content: '';
            position: absolute;
            top: 0;
            right: 0;
            width: 200px;
            height: 200px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
            transform: translate(50%, -50%);
        }

        .tour-header-content {
            position: relative;
            z-index: 2;
        }

        .tour-title {
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }

        .tour-code {
            font-size: 14px;
            opacity: 0.9;
            margin-bottom: 1rem;
        }

        .tour-meta {
            display: flex;
            gap: 2rem;
            flex-wrap: wrap;
        }

        .tour-meta-item {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            font-size: 14px;
        }

        .tour-meta-icon {
            font-size: 16px;
            opacity: 0.8;
        }

        .tabs-container {
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: var(--radius-xl);
            overflow: hidden;
            margin-bottom: 2rem;
        }

        .tab-content {
            padding: 2rem;
        }

        .pricing-section-title {
            font-size: 16px;
            font-weight: 700;
            color: var(--gray-900);
            margin-bottom: 1.5rem;
            padding-bottom: 0.75rem;
            border-bottom: 2px solid var(--gray-100);
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }

        .pricing-section-icon {
            width: 38px;
            height: 38px;
            background: var(--primary-light);
            color: var(--primary-color);
            border-radius: var(--radius-md);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 16px;
        }

        /* Modern Price Box Display */
        .price-display-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 1.5rem;
            margin-bottom: 2rem;
        }

        .premium-price-box {
            background: white;
            border: 2px solid var(--gray-200);
            border-radius: var(--radius-lg);
            padding: 1.5rem;
            position: relative;
            overflow: hidden;
            transition: all 0.3s ease;
            box-shadow: var(--shadow-sm);
        }

        .premium-price-box:hover {
            border-color: var(--primary-color);
            transform: translateY(-3px);
            box-shadow: var(--shadow-md);
        }

        .premium-price-box.primary {
            border-color: var(--primary-color);
            background: linear-gradient(135deg, rgba(37, 99, 235, 0.03) 0%, rgba(255, 255, 255, 1) 100%);
        }

        .premium-price-box .box-icon {
            position: absolute;
            right: -10px;
            bottom: -15px;
            font-size: 5rem;
            opacity: 0.04;
            color: var(--gray-900);
            transform: rotate(-15deg);
        }

        .premium-price-box .box-label {
            font-size: 12px;
            font-weight: 700;
            text-transform: uppercase;
            color: var(--gray-500);
            letter-spacing: 0.5px;
            margin-bottom: 0.5rem;
        }

        .premium-price-box .box-value {
            font-size: 28px;
            font-weight: 800;
            color: var(--gray-900);
            display: flex;
            align-items: baseline;
            gap: 0.25rem;
        }

        .premium-price-box.primary .box-value {
            color: var(--primary-color);
        }

        .premium-price-box .box-currency {
            font-size: 14px;
            font-weight: 500;
            color: var(--gray-500);
        }

        .premium-price-box .box-meta {
            margin-top: 0.75rem;
            padding-top: 0.75rem;
            border-top: 1px dashed var(--gray-200);
            font-size: 12px;
            color: var(--gray-600);
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        /* Marketplace Pricing Style */
        .marketplace-pricing-card {
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: var(--radius-lg);
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            box-shadow: var(--shadow-sm);
        }

        .marketplace-pricing-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 1.5rem;
            padding-bottom: 0.75rem;
            border-bottom: 2px solid var(--gray-100);
        }

        .marketplace-pricing-title {
            font-size: 16px;
            font-weight: 700;
            color: var(--gray-900);
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }

        .marketplace-pricing-icon {
            width: 32px;
            height: 32px;
            border-radius: var(--radius-md);
            background: rgba(37, 99, 235, 0.08);
            color: var(--primary-color);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 12px;
        }

        .currency-row {
            display: grid;
            grid-template-columns: 120px repeat(3, 1fr) auto;
            gap: 1.25rem;
            align-items: center;
            padding: 1.25rem;
            background: var(--gray-50);
            border: 1px solid var(--gray-200);
            border-radius: var(--radius-md);
            margin-bottom: 1rem;
            transition: all 0.2s ease;
        }

        .currency-row:hover {
            background: white;
            border-color: var(--primary-color);
            box-shadow: var(--shadow-sm);
        }

        .currency-badge {
            background: var(--primary-color);
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 50px;
            font-weight: 800;
            font-size: 13px;
            text-align: center;
            box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2);
        }

        .btn-copy-base {
            background: transparent;
            border: none;
            color: var(--primary-color);
            font-size: 11px;
            font-weight: 700;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 0.25rem;
            padding: 0.4rem 0.75rem;
            border-radius: var(--radius-sm);
            transition: all 0.2s ease;
        }

        .btn-copy-base:hover {
            background: var(--primary-light);
            color: var(--primary-color);
        }

        @media (max-width: 992px) {
            .currency-row {
                grid-template-columns: 1fr;
                gap: 0.75rem;
            }
            .currency-badge {
                width: fit-content;
            }
        }

        .section-separator {
            height: 1px;
            background: var(--gray-200);
            margin: 2.5rem 0;
        }
    </style>
@endpush

@section('content')
    <div class="tour-detail-container">
        <!-- Tour Header -->
        <div class="tour-header">
            <div class="tour-header-content">
                <h1 class="tour-title">{{ $tour->name }}</h1>
                <div class="tour-code">Tur Kodu: {{ $tour->code }}</div>
                <div class="tour-meta">
                    <div class="tour-meta-item">
                        <i class="fas fa-clock tour-meta-icon"></i>
                        <span>{{ substr($tour->start_time, 0, 5) }} - {{ substr($tour->end_time, 0, 5) }}</span>
                    </div>
                    <div class="tour-meta-item">
                        <i class="fas fa-map-marker-alt tour-meta-icon"></i>
                        <span>{{ $tour->departureCity?->city ?? 'Belirtilmemiş' }}</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Tabs Container -->
        <div class="tabs-container">
            <!-- Tab Navigation -->
            @include('agency.products.gunubirlik-turlar._nav', ['active' => 'pricing'])

            <!-- Tab Content -->
            <div class="tab-content">
                <!-- 1. Current Base Prices Display -->
                <div class="pricing-section-title">
                    <div class="pricing-section-icon">
                        <i class="fas fa-wallet"></i>
                    </div>
                    Mevcut Varsayılan Fiyatlar
                </div>

                <div class="price-display-grid">
                    <!-- Adult Box -->
                    <div class="premium-price-box primary">
                        <i class="fas fa-user-tie box-icon"></i>
                        <div class="box-label">Yetişkin Fiyatı</div>
                        <div class="box-value">
                            {{ number_format($tour->adult_price, 2) }}
                            <span class="box-currency">{{ $tour->currency ?? 'TRY' }}</span>
                        </div>
                        <div class="box-meta">
                            <i class="fas fa-check-circle text-success"></i> Standart Katılımcı Fiyatı
                        </div>
                    </div>

                    <!-- Child Box -->
                    <div class="premium-price-box">
                        <i class="fas fa-child box-icon"></i>
                        <div class="box-label">Çocuk Fiyatı</div>
                        <div class="box-value">
                            {{ number_format($tour->child_price, 2) }}
                            <span class="box-currency">{{ $tour->currency ?? 'TRY' }}</span>
                        </div>
                        <div class="box-meta">
                            <i class="fas fa-info-circle text-primary"></i> 
                            {{ $tour->child_min_age ?? 0 }}-{{ $tour->child_max_age ?? 12 }} Yaş Aralığı
                        </div>
                    </div>

                    <!-- Infant Box -->
                    <div class="premium-price-box">
                        <i class="fas fa-baby box-icon"></i>
                        <div class="box-label">Bebek Fiyatı</div>
                        <div class="box-value">
                            {{ number_format($tour->infant_price, 2) }}
                            <span class="box-currency">{{ $tour->currency ?? 'TRY' }}</span>
                        </div>
                        <div class="box-meta">
                            <i class="fas fa-info-circle text-primary"></i> 
                            0-{{ $tour->infant_max_age ?? 2 }} Yaş Aralığı
                        </div>
                    </div>
                </div>

                <div class="section-separator"></div>

                <!-- 2. Form for all pricing options -->
                <form action="{{ route('agency.products.gunubirlik-turlar.pricing.update', $tour) }}" method="POST">
                    @csrf
                    @method('PUT')

                    <!-- Base Price Settings -->
                    <div class="pricing-section-title">
                        <div class="pricing-section-icon">
                            <i class="fas fa-edit"></i>
                        </div>
                        Varsayılan Fiyat Ayarları
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="adult_price" class="form-label fw-bold">Varsayılan Yetişkin Fiyatı <span class="text-danger">*</span></label>
                            <div class="input-group">
                                <input type="number" step="0.01" class="form-control" id="adult_price" name="adult_price"
                                       value="{{ old('adult_price', $tour->adult_price) }}" required>
                                <select class="form-select" name="currency" id="base_currency_select" style="max-width: 110px;">
                                    @foreach($activeCurrencies as $currency)
                                        <option value="{{ $currency->code }}" {{ ($tour->currency ?? 'TRY') === $currency->code ? 'selected' : '' }}>{{ $currency->code }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>

                        <div class="col-md-3 mb-3">
                            <label for="child_price" class="form-label fw-bold">Varsayılan Çocuk Fiyatı <span class="text-danger">*</span></label>
                            <input type="number" step="0.01" class="form-control" id="child_price" name="child_price"
                                   value="{{ old('child_price', $tour->child_price) }}" required>
                        </div>

                        <div class="col-md-3 mb-3">
                            <label for="infant_price" class="form-label fw-bold">Varsayılan Bebek Fiyatı <span class="text-danger">*</span></label>
                            <input type="number" step="0.01" class="form-control" id="infant_price" name="infant_price"
                                   value="{{ old('infant_price', $tour->infant_price) }}" required>
                        </div>
                    </div>

                    <div class="row mb-4">
                        <div class="col-md-4 mb-3">
                            <label for="child_min_age" class="form-label fw-bold">Çocuk Min. Yaş</label>
                            <input type="number" class="form-control" id="child_min_age" name="child_min_age"
                                   value="{{ old('child_min_age', $tour->child_min_age) }}" placeholder="0">
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="child_max_age" class="form-label fw-bold">Çocuk Max. Yaş</label>
                            <input type="number" class="form-control" id="child_max_age" name="child_max_age"
                                   value="{{ old('child_max_age', $tour->child_max_age) }}" placeholder="12">
                        </div>

                        <div class="col-md-4 mb-3">
                            <label for="infant_max_age" class="form-label fw-bold">Bebek Max. Yaş</label>
                            <input type="number" class="form-control" id="infant_max_age" name="infant_max_age"
                                   value="{{ old('infant_max_age', $tour->infant_max_age) }}" placeholder="2">
                        </div>
                    </div>

                    <div class="section-separator"></div>

                    <!-- Marketplace Custom Currency Prices -->
                    <div class="pricing-section-title">
                        <div class="pricing-section-icon">
                            <i class="fas fa-store"></i>
                        </div>
                        Pazar Özel Para Birimi Fiyatları
                    </div>

                    @if($marketplaces->isEmpty())
                        <div class="alert alert-warning">
                            <i class="fas fa-exclamation-triangle me-2"></i>
                            Bu tura atanmış herhangi bir aktif pazar bulunmamaktadır. Pazarlara özel para birimi fiyatları girmek için önce <strong>Ayarlar</strong> sekmesinden pazarlarını aktif etmelisiniz.
                        </div>
                    @else
                        <div class="marketplace-pricing-list">
                            @foreach($marketplaces as $mp)
                                <div class="marketplace-pricing-card">
                                    <div class="marketplace-pricing-header">
                                        <div class="marketplace-pricing-title">
                                            <div class="marketplace-pricing-icon">
                                                {{ strtoupper(substr($mp->name, 0, 2)) }}
                                            </div>
                                            {{ $mp->name }} - Fiyat Yapılandırması
                                        </div>
                                    </div>

                                    @if(!$mp->defaultCurrency)
                                        <div class="text-muted small">
                                            <i class="fas fa-info-circle me-1"></i> Bu pazar için herhangi bir para birimi atanmamış.
                                        </div>
                                    @else
                                        @php
                                            $price = $marketplacePrices[$mp->id] ?? null;
                                            $adultVal = $price ? $price->adult_price : '';
                                            $childVal = $price ? $price->child_price : '';
                                            $infantVal = $price ? $price->infant_price : '';
                                        @endphp
                                        <div class="currency-row" id="row-{{ $mp->id }}">
                                            <div class="currency-badge">
                                                {{ $mp->defaultCurrency->code }}
                                            </div>

                                            <div class="form-group mb-0">
                                                <label class="form-label small fw-bold mb-1">Yetişkin Fiyatı</label>
                                                <input type="number" step="0.01" class="form-control form-control-sm adult-input"
                                                       name="marketplace_prices[{{ $mp->id }}][adult_price]"
                                                       value="{{ $adultVal }}" placeholder="Örn: 150.00">
                                            </div>

                                            <div class="form-group mb-0">
                                                <label class="form-label small fw-bold mb-1">Çocuk Fiyatı</label>
                                                <input type="number" step="0.01" class="form-control form-control-sm child-input"
                                                       name="marketplace_prices[{{ $mp->id }}][child_price]"
                                                       value="{{ $childVal }}" placeholder="Örn: 75.00">
                                            </div>

                                            <div class="form-group mb-0">
                                                <label class="form-label small fw-bold mb-1">Bebek Fiyatı</label>
                                                <input type="number" step="0.01" class="form-control form-control-sm infant-input"
                                                       name="marketplace_prices[{{ $mp->id }}][infant_price]"
                                                       value="{{ $infantVal }}" placeholder="Örn: 20.00">
                                            </div>

                                            <div>
                                                <button type="button" class="btn-copy-base" onclick="copyBaseToRow({{ $mp->id }})">
                                                    <i class="fas fa-arrow-down"></i> Varsayılandan Doldur
                                                </button>
                                            </div>
                                        </div>
                                    @endif
                                </div>
                            @endforeach
                        </div>
                    @endif

                    <div class="d-flex justify-content-end gap-2 mt-4 pt-3 border-top">
                        <a href="{{ route('agency.products.gunubirlik-turlar.general', $tour) }}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-2"></i> İptal
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save me-2"></i> Fiyatlandırma Bilgilerini Kaydet
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        function copyBaseToRow(marketplaceId) {
            // Read values from default base price inputs
            const defaultAdult = document.getElementById('adult_price').value;
            const defaultChild = document.getElementById('child_price').value;
            const defaultInfant = document.getElementById('infant_price').value;

            // Find target inputs in specific row
            const row = document.getElementById(`row-${marketplaceId}`);
            if (row) {
                row.querySelector('.adult-input').value = defaultAdult;
                row.querySelector('.child-input').value = defaultChild;
                row.querySelector('.infant-input').value = defaultInfant;
            }
        }
    </script>
@endpush
