

<?php $__env->startSection('title', ' CRM'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid crm-page">
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-start flex-wrap gap-3">
                <div>
                    <h2 class="mb-1 d-flex align-items-center gap-2">
                        <i class="bi bi-person-lines-fill"></i>
                        <span> CRM</span>
                    </h2>
                    <p class="text-muted mb-0">Oyuncu detaylarını hızlı inceleyin.</p>
                </div>

            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <form method="GET" action="<?php echo e(route('backoffice.crm.index')); ?>">
                <div class="row g-3 align-items-end">
                    <div class="col-lg-8">
                        <label class="form-label">Oyuncu Ara</label>
                        <input
                            type="text"
                            id="crmSearchInput"
                            name="q"
                            class="form-control"
                            value="<?php echo e($search); ?>"
                            placeholder="Üye Adı"
                            autocomplete="off"
                        >
                    </div>
                    <div class="col-lg-4">
                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary flex-fill">
                                <i class="bi bi-search me-1"></i>Ara
                            </button>
                            <a href="<?php echo e(route('backoffice.crm.index')); ?>" class="btn btn-light border flex-fill">
                                <i class="bi bi-arrow-counterclockwise me-1"></i>Temizle
                            </a>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <?php if($search !== ''): ?>
        <div class="row g-4">
            <div class="col-xxl-4 col-xl-5">
                <div class="card border-0 shadow-sm h-100">
                    <div class="card-header bg-white d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">Eşleşen Profiller</h5>
                        <span class="badge bg-light text-dark border"><?php echo e($profiles->count()); ?></span>
                    </div>
                    <div class="card-body p-0">
                        <?php if($profiles->isEmpty()): ?>
                            <div class="p-4 text-muted">Eşleşen profil bulunamadı.</div>
                        <?php else: ?>
                            <div class="crm-profile-list">
                                <?php $__currentLoopData = $profiles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $profile): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php
                                        $isActive = (string) request('profile_type') === (string) $profile['selector_type']
                                            && (string) request('profile_value') === (string) $profile['selector_value'];
                                    ?>
                                    <a
                                        href="<?php echo e(route('backoffice.crm.index', ['q' => $search, 'profile_type' => $profile['selector_type'], 'profile_value' => $profile['selector_value']])); ?>"
                                        class="crm-profile-card <?php echo e($isActive ? 'is-active' : ''); ?>"
                                    >
                                        <div class="d-flex justify-content-between align-items-start gap-3">
                                            <div>
                                                <div class="fw-semibold"><?php echo e($profile['display_name']); ?></div>
                                                <?php if(!empty($profile['names']) && $profile['display_name'] !== ($profile['names'][0] ?? null)): ?>
                                                    <div class="small text-muted"><?php echo e($profile['names'][0]); ?></div>
                                                <?php endif; ?>
                                            </div>
                                            <span class="badge bg-dark-subtle text-dark-emphasis"><?php echo e($profile['transactions_count']); ?></span>
                                        </div>
                                        <?php if(!empty($profile['account_holders'])): ?>
                                            <div class="small text-muted mt-2">Hesap Adı: <?php echo e(collect($profile['account_holders'])->take(2)->implode(', ')); ?></div>
                                        <?php endif; ?>
                                        <div class="crm-profile-stats">
                                            <span class="crm-stat-chip success">Y: <?php echo e(number_format($profile['deposit_total'], 2, ',', '.')); ?></span>
                                            <span class="crm-stat-chip danger">Ç: <?php echo e(number_format($profile['withdrawal_total'], 2, ',', '.')); ?></span>
                                            <span class="crm-stat-chip neutral">Auto: <?php echo e($profile['auto_transactions_count']); ?></span>
                                        </div>
                                    </a>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <div class="col-xxl-8 col-xl-7">
                <?php if(empty($profileDetails)): ?>
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body d-flex align-items-center justify-content-center text-center text-muted min-vh-25">
                            Soldan bir profil seçerek detayları görüntüleyin.
                        </div>
                    </div>
                <?php else: ?>
                    <div class="d-grid gap-4">
                        <div class="card border-0 shadow-sm">
                            <div class="card-header bg-white d-flex justify-content-between align-items-start flex-wrap gap-3">
                                <div>
                                    <h5 class="mb-1"><?php echo e($profileDetails['display_name']); ?></h5>
                                    <div class="small text-muted">
                                        <?php if($profileDetails['customer_player_ids']->isNotEmpty()): ?>
                                            Oyuncu ID: <?php echo e($profileDetails['customer_player_ids']->implode(', ')); ?>

                                        <?php else: ?>
                                            Profil: <?php echo e($profileDetails['selector_value']); ?>

                                        <?php endif; ?>
                                    </div>
                                </div>
                                <div class="small text-muted text-end">
                                    <div>İlk işlem: <?php echo e(optional($profileDetails['first_transaction_at'])->format('d.m.Y H:i') ?: '-'); ?></div>
                                    <div>Son işlem: <?php echo e(optional($profileDetails['last_transaction_at'])->format('d.m.Y H:i') ?: '-'); ?></div>
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="row g-3 crm-summary-grid">
                                    <div class="col-md-6 col-xl-3">
                                        <div class="crm-summary-card">
                                            <div class="crm-summary-label">Toplam İşlem</div>
                                            <div class="crm-summary-value"><?php echo e($profileDetails['total_transactions']); ?></div>
                                        </div>
                                    </div>
                                    <div class="col-md-6 col-xl-3">
                                        <div class="crm-summary-card success">
                                            <div class="crm-summary-label">Toplam Yatırım</div>
                                            <div class="crm-summary-value"><?php echo e(number_format($profileDetails['deposit_total'], 2, ',', '.')); ?> <?php echo e($profileDetails['currency']); ?></div>
                                            <div class="crm-summary-sub"><?php echo e($profileDetails['deposit_count']); ?> işlem</div>
                                        </div>
                                    </div>
                                    <div class="col-md-6 col-xl-3">
                                        <div class="crm-summary-card danger">
                                            <div class="crm-summary-label">Toplam Çekim</div>
                                            <div class="crm-summary-value"><?php echo e(number_format($profileDetails['withdrawal_total'], 2, ',', '.')); ?> <?php echo e($profileDetails['currency']); ?></div>
                                            <div class="crm-summary-sub"><?php echo e($profileDetails['withdrawal_count']); ?> işlem</div>
                                        </div>
                                    </div>
                                    <div class="col-md-6 col-xl-3">
                                        <div class="crm-summary-card neutral">
                                            <div class="crm-summary-label">Auto / Bekleyen</div>
                                            <div class="crm-summary-value"><?php echo e($profileDetails['total_auto_transactions']); ?> / <?php echo e($profileDetails['pending_count']); ?></div>
                                            <div class="crm-summary-sub">Onaylı toplam: <?php echo e(number_format($profileDetails['approved_total'], 2, ',', '.')); ?> <?php echo e($profileDetails['currency']); ?></div>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>

                        <div class="row g-4">
                            <div class="col-xl-7">
                                <div class="card border-0 shadow-sm h-100">
                                    <div class="card-header bg-white">
                                        <h5 class="mb-0">Son Auto İşlemler</h5>
                                    </div>
                                    <div class="card-body p-0">
                                        <div class="table-responsive">
                                            <table class="table table-sm align-middle mb-0">
                                                <thead class="table-light">
                                                    <tr>
                                                        <th>Tarih</th>
                                                        <th>Tip</th>
                                                        <th>Durum</th>
                                                        <th>Tutar</th>
                                                        <th>Site</th>
                                                        <th>Finans</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $__empty_1 = true; $__currentLoopData = $profileDetails['recent_auto_transactions']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transaction): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                                        <tr>
                                                            <td><?php echo e(optional($transaction->created_at)->format('d.m.Y H:i')); ?></td>
                                                            <td><?php echo e($transaction->type === 'deposit' ? 'Yatırım' : 'Çekim'); ?></td>
                                                            <td><?php echo e($transaction->status); ?></td>
                                                            <td><?php echo e(number_format((float) $transaction->amount, 2, ',', '.')); ?> <?php echo e($transaction->currency); ?></td>
                                                            <td><?php echo e($transaction->site?->name ?? '-'); ?></td>
                                                            <td><?php echo e($transaction->financeGroup?->name ?? '-'); ?></td>
                                                        </tr>
                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                                        <tr>
                                                            <td colspan="6" class="text-center text-muted py-4">Auto işlem bulunmuyor.</td>
                                                        </tr>
                                                    <?php endif; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-xl-5">
                                <div class="card border-0 shadow-sm h-100">
                                    <div class="card-header bg-white">
                                        <h5 class="mb-0">IP Adresleri</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="crm-ip-list">
                                            <?php $__empty_1 = true; $__currentLoopData = $profileDetails['ip_breakdown']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ip): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                                <div class="crm-ip-item">
                                                    <div class="d-flex justify-content-between gap-3">
                                                        <div class="fw-semibold"><?php echo e($ip['ip_address']); ?></div>
                                                        <span class="badge bg-light text-dark border"><?php echo e($ip['count']); ?></span>
                                                    </div>
                                                </div>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                                <div class="text-muted">IP kaydı bulunmuyor.</div>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row g-4">
                            <div class="col-xl-6">
                                <div class="card border-0 shadow-sm h-100">
                                    <div class="card-header bg-white"><h5 class="mb-0">Site Dağılımı</h5></div>
                                    <div class="card-body">
                                        <div class="crm-tag-stack">
                                            <?php $__empty_1 = true; $__currentLoopData = $profileDetails['site_breakdown']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $site): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                                <div class="crm-stack-row">
                                                    <div>
                                                        <div class="fw-semibold"><?php echo e($site['name']); ?></div>
                                                        <small class="text-muted"><?php echo e($site['count']); ?> işlem</small>
                                                    </div>
                                                    <span><?php echo e(number_format($site['amount'], 2, ',', '.')); ?></span>
                                                </div>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                                <div class="text-muted">Site verisi bulunmuyor.</div>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-6">
                                <div class="card border-0 shadow-sm h-100">
                                    <div class="card-header bg-white"><h5 class="mb-0">Finans Grupları</h5></div>
                                    <div class="card-body">
                                        <div class="crm-tag-stack">
                                            <?php $__empty_1 = true; $__currentLoopData = $profileDetails['finance_group_breakdown']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $group): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                                <div class="crm-stack-row">
                                                    <div>
                                                        <div class="fw-semibold"><?php echo e($group['name']); ?></div>
                                                        <small class="text-muted"><?php echo e($group['count']); ?> işlem</small>
                                                    </div>
                                                    <span><?php echo e(number_format($group['amount'], 2, ',', '.')); ?></span>
                                                </div>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                                <div class="text-muted">Finans grup verisi bulunmuyor.</div>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('styles'); ?>
<style>
    #crmSearchInput {
        text-transform: uppercase;
    }
    .crm-profile-list {
        display: grid;
        gap: 1px;
        background: #eef2f7;
    }
    .crm-profile-card {
        display: block;
        padding: 1rem 1.1rem;
        background: #fff;
        color: inherit;
        text-decoration: none;
        transition: background 0.15s ease, border-color 0.15s ease;
        border-left: 3px solid transparent;
    }
    .crm-profile-card:hover {
        background: #f8fbff;
        border-left-color: #0d6efd;
    }
    .crm-profile-card.is-active {
        background: #eef6ff;
        border-left-color: #0d6efd;
    }
    .crm-profile-stats {
        display: flex;
        flex-wrap: wrap;
        gap: 0.45rem;
        margin-top: 0.85rem;
    }
    .crm-stat-chip,
    .crm-tag {
        display: inline-flex;
        align-items: center;
        border-radius: 999px;
        padding: 0.28rem 0.65rem;
        font-size: 0.78rem;
        font-weight: 600;
    }
    .crm-stat-chip.success,
    .crm-tag {
        background: #e8f7ef;
        color: #166534;
    }
    .crm-stat-chip.danger {
        background: #fdecec;
        color: #b91c1c;
    }
    .crm-stat-chip.neutral,
    .crm-tag.crm-tag-alt {
        background: #eef2f7;
        color: #334155;
    }
    .crm-summary-card {
        height: 100%;
        border: 1px solid #e5e7eb;
        border-radius: 14px;
        padding: 1rem;
        background: #fff;
    }
    .crm-summary-card.success {
        background: linear-gradient(180deg, #f5fff8 0%, #ecfdf3 100%);
    }
    .crm-summary-card.danger {
        background: linear-gradient(180deg, #fff8f8 0%, #fff1f1 100%);
    }
    .crm-summary-card.neutral {
        background: linear-gradient(180deg, #fafcff 0%, #f3f7fb 100%);
    }
    .crm-summary-label {
        font-size: 0.82rem;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: #64748b;
        margin-bottom: 0.35rem;
    }
    .crm-summary-value {
        font-size: 1.2rem;
        font-weight: 700;
        color: #0f172a;
    }
    .crm-summary-sub {
        margin-top: 0.25rem;
        font-size: 0.82rem;
        color: #64748b;
    }
    .crm-detail-block {
        border: 1px solid #e5e7eb;
        border-radius: 12px;
        padding: 1rem;
        background: #fff;
        height: 100%;
    }
    .crm-tag-list {
        display: flex;
        flex-wrap: wrap;
        gap: 0.45rem;
    }
    .crm-ip-list,
    .crm-tag-stack {
        display: grid;
        gap: 0.75rem;
    }
    .crm-ip-item,
    .crm-stack-row {
        border: 1px solid #e5e7eb;
        border-radius: 12px;
        padding: 0.8rem 0.9rem;
        background: #fff;
    }
    .crm-stack-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
    }
    .min-vh-25 {
        min-height: 25vh;
    }
</style>
<?php $__env->stopPush(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
    const searchInput = document.getElementById('crmSearchInput');
    if (!searchInput) {
        return;
    }

    function normalizeCrmSearchValue(value) {
        return String(value || '')
            .normalize('NFD')
            .replace(/[\u0300-\u036f]/g, '')
            .replace(/[^A-Za-z\s]/g, '')
            .replace(/\s+/g, ' ')
            .trimStart()
            .toUpperCase();
    }

    function syncSearchValue() {
        const normalized = normalizeCrmSearchValue(searchInput.value);
        if (searchInput.value !== normalized) {
            const end = normalized.length;
            searchInput.value = normalized;
            searchInput.setSelectionRange(end, end);
        }
    }

    searchInput.addEventListener('input', syncSearchValue);
    searchInput.addEventListener('paste', function () {
        requestAnimationFrame(syncSearchValue);
    });
    syncSearchValue();
});
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('backoffice.layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/cmummhol/public_html/zoom/resources/views/backoffice/crm/index.blade.php ENDPATH**/ ?>