<div class="modal fade" id="targetSettingsModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content border-0 shadow-lg" style="border-radius: 24px;">
            <div class="modal-header border-0 pb-0 pt-4 px-4">
                <h5 class="modal-title fw-bolder text-dark">Satış Hedeflerini Yapılandır</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body p-4">
                <form action="<?php echo e(route('agency.sales.targets.store')); ?>" method="POST" id="targetSettingsForm">
                    <?php echo csrf_field(); ?>
                    <div class="row g-4 mb-4">
                        <div class="col-md-3">
                            <label class="form-label text-muted fw-bold small mb-1">Hedef Periyodu:</label>
                            <select name="period" id="modalTargetPeriod" class="form-select shadow-none" style="border-radius: 12px; padding: 0.75rem; font-weight: 600;">
                                <option value="year" <?php echo e($targetPeriod === 'year' ? 'selected' : ''); ?>>Yıllık Hedef</option>
                                <option value="month" <?php echo e($targetPeriod === 'month' ? 'selected' : ''); ?>>Aylık Hedef</option>
                            </select>
                        </div>
                        <div class="col-md-3 <?php echo e($targetPeriod !== 'month' ? 'd-none' : ''); ?>" id="modalMonthContainer">
                            <label class="form-label text-muted fw-bold small mb-1">Ay Seçin:</label>
                            <select name="month" id="modalTargetMonth" class="form-select shadow-none" style="border-radius: 12px; padding: 0.75rem;">
                                <?php $__currentLoopData = range(1, 12); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $m): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($m); ?>" <?php echo e(($currentMonth ?? now()->month) == $m ? 'selected' : ''); ?>>
                                        <?php echo e(\Carbon\Carbon::create(null, $m)->translatedFormat('F')); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label text-muted fw-bold small mb-1">Yıl:</label>
                            <select name="year" id="modalTargetYear" class="form-select shadow-none" style="border-radius: 12px; padding: 0.75rem;">
                                <?php for($y = date('Y') + 1; $y >= 2024; $y--): ?>
                                    <option value="<?php echo e($y); ?>" <?php echo e($y == $currentYear ? 'selected' : ''); ?>><?php echo e($y); ?></option>
                                <?php endfor; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label text-muted fw-bold small mb-1">Ölçüm Birimi:</label>
                            <select name="type" class="form-select shadow-none" id="targetTypeSelector" style="border-radius: 12px; padding: 0.75rem; font-weight: 700;">
                                <option value="revenue" <?php echo e($targetType === 'revenue' ? 'selected' : ''); ?>>Toplam Satış Cirosu (₺)</option>
                                <option value="quantity" <?php echo e($targetType === 'quantity' ? 'selected' : ''); ?>>Satış Adedi (Sayı)</option>
                            </select>
                        </div>
                    </div>

                    <div class="target-inputs-list px-1 mt-4">
                        <div class="d-flex justify-content-between align-items-center mb-4 pb-2 border-bottom">
                            <h6 class="fw-bold text-dark mb-0">Ürün Tiplerine Göre Hedefler</h6>
                            <div id="modalLoadingSpinner" class="spinner-border spinner-border-sm text-primary d-none" role="status"></div>
                        </div>
                        <div class="row g-3">
                            <?php $__currentLoopData = $productTypes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $pt): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="col-md-6">
                                    <div class="p-3 bg-light" style="border-radius: 16px; border: 1px solid #f1f5f9;">
                                        <div class="d-flex justify-content-between align-items-center mb-2">
                                            <div class="d-flex align-items-center gap-2">
                                                <i class="fas <?php echo e($pt->icon ?? 'fa-circle'); ?> text-primary opacity-50"></i>
                                                <span class="fw-bold text-dark small"><?php echo e($pt->name); ?></span>
                                            </div>
                                        </div>
                                        <div class="input-group">
                                            <span class="input-group-text bg-white border-end-0 unit-symbol" style="border-radius: 10px 0 0 10px; font-weight: 700; color: #64748b; width: 45px; justify-content: center;">
                                                <?php echo e($targetType === 'revenue' ? '₺' : '#'); ?>

                                            </span>
                                            <input type="text" name="targets[<?php echo e($pt->id); ?>]" 
                                                   data-id="<?php echo e($pt->id); ?>"
                                                   class="form-control border-start-0 shadow-none target-input" 
                                                   placeholder="0"
                                                   value="<?php echo e(number_format((int)($salesTargets[$pt->id]['target'] ?? 0), 0, ',', '.')); ?>"
                                                   style="border-radius: 0 10px 10px 0; font-weight: 800; text-align: right; padding: 0.6rem;">
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </div>
                    </div>

                    <div class="mt-5 pt-4 border-top d-flex justify-content-between align-items-center">
                        <span class="text-muted small italic">Seçtiğiniz dönem için hedeflerinizi girin ve kaydedin.</span>
                        <div class="d-flex gap-3">
                            <button type="button" class="btn btn-link text-muted px-4 text-decoration-none fw-bold" data-bs-dismiss="modal">İptal</button>
                            <button type="submit" class="btn btn-primary px-5 shadow-sm" id="saveTargetsBtn" style="border-radius: 12px; font-weight: 700; background: linear-gradient(135deg, #2563eb 0%, #1e40af 100%); border: none; padding: 0.75rem;">
                                Ayarları Kaydet
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    const targetTypeSelector = document.getElementById('targetTypeSelector');
    const periodSelector = document.getElementById('modalTargetPeriod');
    const monthSelector = document.getElementById('modalTargetMonth');
    const yearSelector = document.getElementById('modalTargetYear');
    const monthContainer = document.getElementById('modalMonthContainer');
    const spinner = document.getElementById('modalLoadingSpinner');

    // Binlik Ayraç Formatlama
    const formatNumber = (val) => {
        let value = val.toString().replace(/\D/g, '');
        return value ? parseInt(value).toLocaleString('tr-TR') : '0';
    };

    // Mevcut Hedefleri Getir
    const fetchExistingTargets = async () => {
        spinner.classList.remove('d-none');
        try {
            const params = new URLSearchParams({
                period: periodSelector.value,
                year: yearSelector.value,
                month: monthSelector.value,
                type: targetTypeSelector.value
            });
            const response = await fetch(`<?php echo e(route('agency.sales.targets.json')); ?>?${params}`);
            const data = await response.json();
            
            document.querySelectorAll('.target-input').forEach(input => {
                const id = input.dataset.id;
                input.value = formatNumber(data[id] || 0);
            });
        } catch (error) {
            console.error('Hedefler yüklenemedi:', error);
        }
        spinner.classList.add('d-none');
    };

    // Dinamik Değişimler
    targetTypeSelector?.addEventListener('change', function() {
        const isRevenue = this.value === 'revenue';
        document.querySelectorAll('.unit-symbol').forEach(el => {
            el.textContent = isRevenue ? '₺' : '#';
        });
        fetchExistingTargets();
    });

    periodSelector?.addEventListener('change', function() {
        if (this.value === 'month') {
            monthContainer.classList.remove('d-none');
        } else {
            monthContainer.classList.add('d-none');
        }
        fetchExistingTargets();
    });

    [monthSelector, yearSelector].forEach(el => el?.addEventListener('change', fetchExistingTargets));

    document.querySelectorAll('.target-input').forEach(input => {
        input.addEventListener('input', function(e) {
            this.value = formatNumber(this.value);
        });
    });

    // Form Gönderilmeden Önce Temizlik
    document.querySelector('#targetSettingsForm')?.addEventListener('submit', function(e) {
        this.querySelectorAll('.target-input').forEach(input => {
            input.value = input.value.replace(/\./g, '');
        });
    });
</script>

<?php /**PATH C:\xampp\htdocs\acente\resources\views/agency/sales/partials/target_modal.blade.php ENDPATH**/ ?>