@extends('agency.layouts.app')

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

@php
    $breadcrumbs = [
        ['title' => 'Ana Sayfa', 'url' => route('agency.dashboard')],
        ['title' => 'Kültür Turları', 'url' => route('agency.products.kultur-turlari.index')],
        ['title' => $tour->name, 'url' => route('agency.products.kultur-turlari.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;
        }

        .nav-tabs {
            background: var(--gray-50);
            border-bottom: 1px solid var(--gray-200);
            padding: 0 1rem;
            margin: 0;
        }

        .nav-tabs .nav-link {
            border: none;
            border-radius: 0;
            color: var(--gray-600);
            font-weight: 500;
            padding: 1rem 1.5rem;
            position: relative;
            transition: var(--transition);
        }

        .nav-tabs .nav-link:hover {
            color: var(--primary-color);
            background: transparent;
            border-color: transparent;
        }

        .nav-tabs .nav-link.active {
            color: var(--primary-color);
            background: transparent;
            border-color: transparent;
            font-weight: 600;
        }

        .nav-tabs .nav-link.active::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: var(--primary-color);
            border-radius: 3px 3px 0 0;
        }

        .tab-content {
            padding: 2rem;
        }

        .pricing-card {
            background: white;
            border: 2px solid var(--gray-200);
            border-radius: var(--radius-lg);
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            transition: var(--transition);
        }

        .pricing-card:hover {
            border-color: var(--primary-color);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        .pricing-card.default {
            border-color: var(--success-color);
            background: linear-gradient(to right, rgba(34, 197, 94, 0.05), transparent);
        }

        .pricing-header {
            display: flex;
            justify-content: space-between;
            align-items: start;
            margin-bottom: 1rem;
        }

        .pricing-name {
            font-size: 1.3rem;
            font-weight: 700;
            color: var(--gray-900);
        }

        .pricing-badge {
            background: var(--success-color);
            color: white;
            padding: 0.25rem 0.75rem;
            border-radius: var(--radius-md);
            font-size: 0.75rem;
            font-weight: 600;
        }

        .pricing-description {
            color: var(--gray-600);
            margin-bottom: 1rem;
        }

        .pricing-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1rem;
            margin-bottom: 1rem;
        }

        .price-item {
            background: var(--gray-50);
            padding: 0.75rem;
            border-radius: var(--radius-md);
        }

        .price-label {
            font-size: 0.85rem;
            color: var(--gray-600);
            margin-bottom: 0.25rem;
        }

        .price-value {
            font-size: 1.25rem;
            font-weight: 700;
            color: var(--primary-color);
        }

        .pricing-actions {
            display: flex;
            gap: 0.5rem;
        }

        .empty-state {
            text-align: center;
            padding: 3rem;
            color: var(--gray-500);
        }
    </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-calendar tour-meta-icon"></i>
                        <span>{{ $tour->days }} Gün {{ $tour->nights }} Gece</span>
                    </div>
                    <div class="tour-meta-item">
                        <i class="fas fa-map-marker-alt tour-meta-icon"></i>
                        <span>{{ $tour->departureCity?->name ?? 'Belirtilmemiş' }}</span>
                    </div>
                    @if($tour->tour_type)
                        <div class="tour-meta-item">
                            <i class="fas fa-tag tour-meta-icon"></i>
                            <span>{{ $tour->tour_type_display_name }}</span>
                        </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Tabs Container -->
        <div class="tabs-container">
            <!-- Tab Navigation -->
            <ul class="nav nav-tabs" role="tablist">
                <li class="nav-item">
                    <a class="nav-link" href="{{ route('agency.products.kultur-turlari.general', $tour) }}">
                        <i class="fas fa-info-circle me-2"></i>Genel Bilgiler
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ route('agency.products.kultur-turlari.itinerary', $tour) }}">
                        <i class="fas fa-list me-2"></i>Program
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="{{ route('agency.products.kultur-turlari.pricing', $tour) }}">
                        <i class="fas fa-dollar-sign me-2"></i>Fiyatlandırma
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ route('agency.products.kultur-turlari.dates', $tour) }}">
                        <i class="fas fa-calendar me-2"></i>Tarihler
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ route('agency.products.kultur-turlari.media', $tour) }}">
                        <i class="fas fa-images me-2"></i>Görseller
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="{{ route('agency.products.kultur-turlari.settings', $tour) }}">
                        <i class="fas fa-cog me-2"></i>Ayarlar
                    </a>
                </li>
            </ul>

            <!-- Tab Content -->
            <div class="tab-content">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h4 class="mb-0">Fiyatlandırma</h4>
                    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addPricingModal">
                        <i class="fas fa-plus me-2"></i>Fiyat Ekle
                    </button>
                </div>

                <!-- Pricings -->
                @if($pricings->count() > 0)
                    @foreach($pricings as $pricing)
                        <div class="pricing-card {{ $pricing->is_default ? 'default' : '' }}">
                            <div class="pricing-header">
                                <div>
                                    <div class="pricing-name">
                                        {{ $pricing->pricing_name }}
                                        @if($pricing->is_default)
                                            <span class="pricing-badge">Varsayılan</span>
                                        @endif
                                    </div>
                                    @if($pricing->description)
                                        <div class="pricing-description">{{ $pricing->description }}</div>
                                    @endif
                                </div>
                                <div class="pricing-actions">
                                    @if(!$pricing->is_default)
                                        <button type="button" class="btn btn-sm btn-success" onclick="setDefault({{ $pricing->id }})">
                                            <i class="fas fa-star"></i>
                                        </button>
                                    @endif
                                    <button type="button" class="btn btn-sm btn-outline-primary" onclick="editPricing({{ $pricing->id }})">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-outline-danger" onclick="deletePricing({{ $pricing->id }})">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </div>
                            </div>

                            <div class="pricing-details">
                                <div class="price-item">
                                    <div class="price-label">Yetişkin Fiyatı</div>
                                    <div class="price-value">{{ number_format($pricing->adult_price, 2) }} {{ $pricing->currency }}</div>
                                </div>
                                @if($pricing->child_price)
                                    <div class="price-item">
                                        <div class="price-label">
                                            Çocuk Fiyatı
                                            @if($pricing->child_min_age || $pricing->child_max_age)
                                                ({{ $pricing->child_min_age ?? 0 }}-{{ $pricing->child_max_age ?? 12 }} yaş)
                                            @endif
                                        </div>
                                        <div class="price-value">{{ number_format($pricing->child_price, 2) }} {{ $pricing->currency }}</div>
                                    </div>
                                @endif
                                @if($pricing->infant_price)
                                    <div class="price-item">
                                        <div class="price-label">
                                            Bebek Fiyatı
                                            @if($pricing->infant_max_age)
                                                (0-{{ $pricing->infant_max_age }} yaş)
                                            @endif
                                        </div>
                                        <div class="price-value">{{ number_format($pricing->infant_price, 2) }} {{ $pricing->currency }}</div>
                                    </div>
                                @endif
                            </div>
                        </div>
                    @endforeach
                @else
                    <div class="empty-state">
                        <i class="fas fa-dollar-sign fa-3x mb-3 text-muted"></i>
                        <h5>Henüz fiyatlandırma eklenmemiş</h5>
                        <p class="text-muted">Başlamak için yukarıdaki "Fiyat Ekle" butonuna tıklayın</p>
                    </div>
                @endif
            </div>
        </div>
    </div>

    <!-- Add/Edit Pricing Modal -->
    <div class="modal fade" id="addPricingModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modalTitle">Fiyat Ekle</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form id="pricingForm">
                    <div class="modal-body">
                        <input type="hidden" id="pricing_id">
                        <div class="mb-3">
                            <label for="pricing_name" class="form-label">Fiyatlandırma Adı <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="pricing_name" name="pricing_name" required placeholder="Örn: Standart, Premium">
                        </div>
                        <div class="mb-3">
                            <label for="description" class="form-label">Açıklama</label>
                            <textarea class="form-control" id="description" name="description" rows="2"></textarea>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="adult_price" class="form-label">Yetişkin Fiyatı <span class="text-danger">*</span></label>
                                <input type="number" step="0.01" class="form-control" id="adult_price" name="adult_price" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="currency" class="form-label">Para Birimi <span class="text-danger">*</span></label>
                                <select class="form-select" id="currency" name="currency" required>
                                    <option value="TRY">TRY (₺)</option>
                                    <option value="USD">USD ($)</option>
                                    <option value="EUR">EUR (€)</option>
                                    <option value="GBP">GBP (£)</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="child_price" class="form-label">Çocuk Fiyatı</label>
                                <input type="number" step="0.01" class="form-control" id="child_price" name="child_price">
                            </div>
                            <div class="col-md-3 mb-3">
                                <label for="child_min_age" class="form-label">Min Yaş</label>
                                <input type="number" class="form-control" id="child_min_age" name="child_min_age" placeholder="0">
                            </div>
                            <div class="col-md-3 mb-3">
                                <label for="child_max_age" class="form-label">Max Yaş</label>
                                <input type="number" class="form-control" id="child_max_age" name="child_max_age" placeholder="12">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="infant_price" class="form-label">Bebek Fiyatı</label>
                                <input type="number" step="0.01" class="form-control" id="infant_price" name="infant_price">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="infant_max_age" class="form-label">Bebek Max Yaş</label>
                                <input type="number" class="form-control" id="infant_max_age" name="infant_max_age" placeholder="2">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>
                        <button type="submit" class="btn btn-primary"><i class="fas fa-save me-2"></i>Kaydet</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        const tourId = {{ $tour->id }};
        document.getElementById('pricingForm').addEventListener('submit', function(e) { e.preventDefault(); savePricing(); });
        document.getElementById('addPricingModal').addEventListener('hidden.bs.modal', resetForm);

        function editPricing(id) {
            fetch(`/agency/products/kultur-turlari/${tourId}/pricing/${id}/edit`).then(r => r.json()).then(d => {
                if (d.success) {
                    document.getElementById('modalTitle').textContent = 'Fiyat Düzenle';
                    document.getElementById('pricing_id').value = d.pricing.id;
                    document.getElementById('pricing_name').value = d.pricing.pricing_name;
                    document.getElementById('description').value = d.pricing.description || '';
                    document.getElementById('adult_price').value = d.pricing.adult_price;
                    document.getElementById('child_price').value = d.pricing.child_price || '';
                    document.getElementById('infant_price').value = d.pricing.infant_price || '';
                    document.getElementById('child_min_age').value = d.pricing.child_min_age || '';
                    document.getElementById('child_max_age').value = d.pricing.child_max_age || '';
                    document.getElementById('infant_max_age').value = d.pricing.infant_max_age || '';
                    document.getElementById('currency').value = d.pricing.currency;
                    new bootstrap.Modal(document.getElementById('addPricingModal')).show();
                }
            });
        }

        function savePricing() {
            const id = document.getElementById('pricing_id').value;
            const data = Object.fromEntries(new FormData(document.getElementById('pricingForm')));
            fetch(id ? `/agency/products/kultur-turlari/${tourId}/pricing/${id}` : `/agency/products/kultur-turlari/${tourId}/pricing`, {
                method: id ? 'PUT' : 'POST',
                headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content },
                body: JSON.stringify(data)
            }).then(r => r.json()).then(d => {
                AgencyPanel.showNotification(d.message, d.success ? 'success' : 'error');
                if (d.success) setTimeout(() => location.reload(), 1000);
            });
        }

        function deletePricing(id) {
            if (!confirm('Bu fiyatlandırmayı silmek istediğinize emin misiniz?')) return;
            fetch(`/agency/products/kultur-turlari/${tourId}/pricing/${id}`, {
                method: 'DELETE',
                headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
            }).then(r => r.json()).then(d => {
                AgencyPanel.showNotification(d.message, d.success ? 'success' : 'error');
                if (d.success) setTimeout(() => location.reload(), 1000);
            });
        }

        function setDefault(id) {
            fetch(`/agency/products/kultur-turlari/${tourId}/pricing/${id}/set-default`, {
                method: 'POST',
                headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
            }).then(r => r.json()).then(d => {
                AgencyPanel.showNotification(d.message, d.success ? 'success' : 'error');
                if (d.success) setTimeout(() => location.reload(), 1000);
            });
        }

        function resetForm() {
            document.getElementById('modalTitle').textContent = 'Fiyat Ekle';
            document.getElementById('pricingForm').reset();
            document.getElementById('pricing_id').value = '';
        }
    </script>
@endpush
