@extends('layout')

@section('title', __('store/reseller.dashboard'))

@section('content')
<div class="container py-5">
    <x-clientarea.page-header
        icon="fas fa-store"
        :title="__('store/reseller.title') . ($reseller->brand_name ? ' — ' . $reseller->brand_name : '')"
    />


    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    
    <div class="row g-3 mb-4">
        <div class="col-md-4 col-lg">
            <div class="card shadow-sm border-0 text-center">
                <div class="card-body py-3">
                    <div class="fs-3 fw-bold text-primary">{{ $stats['total_clients'] }}</div>
                    <div class="text-muted small">{{ __('store/reseller.stat_total_clients') }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-4 col-lg">
            <div class="card shadow-sm border-0 text-center">
                <div class="card-body py-3">
                    <div class="fs-3 fw-bold text-info">{{ $stats['total_orders'] }}</div>
                    <div class="text-muted small">{{ __('store/reseller.stat_total_orders') }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-4 col-lg">
            <div class="card shadow-sm border-0 text-center">
                <div class="card-body py-3">
                    <div class="fs-3 fw-bold text-success">{{ number_format($stats['total_revenue'], 2) }}</div>
                    <div class="text-muted small">{{ __('store/reseller.stat_total_revenue') }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-lg">
            <div class="card shadow-sm border-0 text-center">
                <div class="card-body py-3">
                    <div class="fs-3 fw-bold text-warning">{{ number_format($stats['pending_commissions'], 2) }}</div>
                    <div class="text-muted small">{{ __('store/reseller.stat_pending_commissions') }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-lg">
            <div class="card shadow-sm border-0 text-center">
                <div class="card-body py-3">
                    <div class="fs-3 fw-bold text-success">{{ number_format($stats['total_commissions'], 2) }}</div>
                    <div class="text-muted small">{{ __('store/reseller.stat_total_commissions') }}</div>
                </div>
            </div>
        </div>
    </div>

    
    @if(!empty($monthlyCommissions))
    @php $maxCommission = max(array_map(fn($m) => $m['total'], $monthlyCommissions)) ?: 1; @endphp
    <div class="card shadow-sm border-0 mb-4">
        <div class="card-header"><h6 class="mb-0"><i class="fas fa-chart-column me-2"></i>Son 6 Ay Komisyon</h6></div>
        <div class="card-body">
            <div class="d-flex align-items-end justify-content-between gap-2" style="height: 140px;">
                @foreach($monthlyCommissions as $m)
                <div class="d-flex flex-column align-items-center flex-fill" style="min-width: 0;">
                    <div class="small text-muted mb-1">{{ number_format($m['total'], 0) }}</div>
                    <div class="w-100 bg-success rounded-top" style="height: {{ max(4, (int) round($m['total'] / $maxCommission * 100)) }}px; max-width: 48px; margin: 0 auto;"
                         title="{{ $m['label'] }}: {{ number_format($m['total'], 2) }}"></div>
                    <div class="small text-muted mt-1 text-truncate w-100 text-center">{{ $m['label'] }}</div>
                </div>
                @endforeach
            </div>
        </div>
    </div>
    @endif

    <div class="row g-4">
        
        <div class="col-lg-6">
            <div class="card shadow-sm border-0">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h6 class="mb-0"><i class="fas fa-users me-2"></i>{{ __('store/reseller.my_clients') }}</h6>
                    <a href="{{ route('store.auth.reseller.clients') }}" class="btn btn-sm btn-outline-primary">
                        {{ __('store/reseller.clients_list') }}
                    </a>
                </div>
                <div class="card-body p-0">
                    @if($recentClients->isEmpty())
                        <div class="text-center py-4 text-muted">{{ __('store/reseller.no_clients') }}</div>
                    @else
                        <div class="table-responsive">
                            <table class="table table-sm mb-0">
                                <thead><tr>
                                    <th>{{ __('store/reseller.client_name') }}</th>
                                    <th>{{ __('store/reseller.client_services') }}</th>
                                    <th>{{ __('store/reseller.client_joined') }}</th>
                                </tr></thead>
                                <tbody>
                                @foreach($recentClients as $c)
                                    <tr>
                                        <td>{{ $c->first_name }} {{ $c->last_name }}</td>
                                        <td>{{ $c->products_count }}</td>
                                        <td>{{ $c->created_at->format('d.m.Y') }}</td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    @endif
                </div>
            </div>
        </div>

        
        <div class="col-lg-6">
            <div class="card shadow-sm border-0">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h6 class="mb-0"><i class="fas fa-shopping-cart me-2"></i>{{ __('store/reseller.my_orders') }}</h6>
                    <a href="{{ route('store.auth.reseller.orders') }}" class="btn btn-sm btn-outline-primary">
                        {{ __('store/reseller.orders_list') }}
                    </a>
                </div>
                <div class="card-body p-0">
                    @if($recentOrders->isEmpty())
                        <div class="text-center py-4 text-muted">{{ __('store/reseller.no_orders') }}</div>
                    @else
                        <div class="table-responsive">
                            <table class="table table-sm mb-0">
                                <thead><tr>
                                    <th>{{ __('store/reseller.order_number') }}</th>
                                    <th>{{ __('store/reseller.order_client') }}</th>
                                    <th>{{ __('store/reseller.order_total') }}</th>
                                    <th>{{ __('store/reseller.order_date') }}</th>
                                </tr></thead>
                                <tbody>
                                @foreach($recentOrders as $order)
                                    <tr>
                                        <td>{{ $order->order_number }}</td>
                                        <td>{{ $order->client?->first_name }} {{ $order->client?->last_name }}</td>
                                        <td>{{ number_format($order->total, 2) }}</td>
                                        <td>{{ $order->created_at->format('d.m.Y') }}</td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    
    <div class="row g-3 mt-2">
        <div class="col-md">
            <a href="{{ route('store.auth.reseller.sites.index') }}" class="card shadow-sm border-0 text-decoration-none">
                <div class="card-body text-center py-3">
                    <i class="fas fa-globe fa-2x text-purple mb-2"></i>
                    <div>Sitelerim</div>
                </div>
            </a>
        </div>
        <div class="col-md">
            <a href="{{ route('store.auth.reseller.clients') }}" class="card shadow-sm border-0 text-decoration-none">
                <div class="card-body text-center py-3">
                    <i class="fas fa-users fa-2x text-primary mb-2"></i>
                    <div>{{ __('store/reseller.my_clients') }}</div>
                </div>
            </a>
        </div>
        <div class="col-md">
            <a href="{{ route('store.auth.reseller.orders') }}" class="card shadow-sm border-0 text-decoration-none">
                <div class="card-body text-center py-3">
                    <i class="fas fa-shopping-cart fa-2x text-info mb-2"></i>
                    <div>{{ __('store/reseller.my_orders') }}</div>
                </div>
            </a>
        </div>
        <div class="col-md">
            <a href="{{ route('store.auth.reseller.commissions') }}" class="card shadow-sm border-0 text-decoration-none">
                <div class="card-body text-center py-3">
                    <i class="fas fa-coins fa-2x text-success mb-2"></i>
                    <div>{{ __('store/reseller.commissions') }}</div>
                </div>
            </a>
        </div>
        <div class="col-md">
            <a href="{{ route('store.auth.reseller.pricing') }}" class="card shadow-sm border-0 text-decoration-none">
                <div class="card-body text-center py-3">
                    <i class="fas fa-tags fa-2x text-warning mb-2"></i>
                    <div>Fiyat & Komisyon</div>
                </div>
            </a>
        </div>
    </div>
</div>
@endsection
