@extends('layouts.marketer')

@section('title')
My Marketers Dashboard
@endsection

@section('content')
<div class="dashboard-container">
    <div class="dashboard-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="dashboard-title">Marketers Dashboard</h1>
                <p class="dashboard-subtitle">Team Network Overview</p>
            </div>
        </div>
    </div>

    <x-alert/>

    <!-- 8 Stat Cards Grid -->
    <div class="stats-grid-6">
        <!-- Total Team -->
        <div class="stat-card stat-primary">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
                    <circle cx="9" cy="7" r="4"></circle>
                    <path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">Total Team Members</p>
                <h3 class="stat-value">{{ number_format(count($team)) }}</h3>
                <!--<span class="stat-badge stat-badge-primary">5 Generations</span>-->
            </div>
        </div>

        <!-- Rebate Balance -->
        <div class="stat-card stat-success">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M12 2v20M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">Rebate Balance</p>
                <h3 class="stat-value">${{ number_format(auth()->user()->rebates, 2) }}</h3>
                <a href="#" class="stat-link" onclick="event.preventDefault(); document.getElementById('move-rebate-form').submit();">Move to Wallet →</a>
            </div>
        </div>

        <!-- Active Affiliates -->
        <div class="stat-card stat-info">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">Active Affiliates</p>
                <h3 class="stat-value">{{ number_format($activeMembers) }}</h3>
                <span class="stat-badge stat-badge-info">With Deposits</span>
            </div>
        </div>

        <!-- Total Rebates Earned -->
        <div class="stat-card stat-warning">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <line x1="12" y1="1" x2="12" y2="23"></line>
                    <path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">Total Rebates Earned</p>
                <h3 class="stat-value">${{ number_format($totalRebatesEarned, 2) }}</h3>
                <span class="stat-badge stat-badge-warning">All Time</span>
            </div>
        </div>

        <!-- Email Verified (Clickable) -->
        <a href="{{ route('marketer.email-verified') }}" class="stat-card stat-clickable stat-email-verified" target="_blank">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>
                    <polyline points="22 4 12 14.01 9 11.01"></polyline>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">Email Verified</p>
                <h3 class="stat-value">{{ number_format($emailVerifiedCount) }}</h3>
                <span class="stat-link-inline">View List →</span>
            </div>
        </a>

        <!-- Email Unverified (Clickable) -->
        <a href="{{ route('marketer.email-unverified') }}" class="stat-card stat-clickable stat-email-unverified" target="_blank">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="12" cy="12" r="10"></circle>
                    <line x1="15" y1="9" x2="9" y2="15"></line>
                    <line x1="9" y1="9" x2="15" y2="15"></line>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">Email Unverified</p>
                <h3 class="stat-value">{{ number_format($emailUnverifiedCount) }}</h3>
                <span class="stat-link-inline">View List →</span>
            </div>
        </a>

        <!-- KYC Verified (Clickable) -->
        <a href="{{ route('marketer.kyc-verified') }}" class="stat-card stat-clickable stat-kyc-verified" target="_blank">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
                    <circle cx="8.5" cy="7" r="4"></circle>
                    <polyline points="17 11 19 13 23 9"></polyline>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">KYC Verified</p>
                <h3 class="stat-value">{{ number_format($kycVerifiedCount) }}</h3>
                <span class="stat-link-inline">View List →</span>
            </div>
        </a>

        <!-- KYC Unverified (Clickable) -->
        <a href="{{ route('marketer.kyc-unverified') }}" class="stat-card stat-clickable stat-kyc-unverified" target="_blank">
            <div class="stat-icon">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
                    <circle cx="8.5" cy="7" r="4"></circle>
                    <line x1="23" y1="11" x2="17" y2="11"></line>
                </svg>
            </div>
            <div class="stat-content">
                <p class="stat-label">KYC Unverified</p>
                <h3 class="stat-value">{{ number_format($kycUnverifiedCount) }}</h3>
                <span class="stat-link-inline">View List →</span>
            </div>
        </a>
    </div>

    <!-- Single Downline Card (All 5 Generations) -->
    <div class="downline-single-container">
        <h2 class="section-title">All Generations Performance</h2>
        <div class="downline-single-card">
            <div class="downline-single-header">
                <div class="downline-single-title">
                    <h3>Complete Network Overview</h3>
                    <span class="downline-single-badge">{{ number_format(count($team)) }} Total Members</span>
                </div>
                <div class="downline-single-icon">
                    <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
                        <circle cx="9" cy="7" r="4"></circle>
                        <path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
                    </svg>
                </div>
            </div>
            <div class="downline-single-body">
                <div class="downline-single-stat">
                    <div class="downline-stat-icon stat-icon-success">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <line x1="12" y1="5" x2="12" y2="19"></line>
                            <polyline points="19 12 12 19 5 12"></polyline>
                        </svg>
                    </div>
                    <div class="downline-stat-content">
                        <span class="downline-stat-label">Total Deposits</span>
                        <span class="downline-stat-value text-success">${{ number_format($allGensDeposits, 2) }}</span>
                    </div>
                </div>

                <div class="downline-single-stat">
                    <div class="downline-stat-icon stat-icon-danger">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <line x1="12" y1="19" x2="12" y2="5"></line>
                            <polyline points="5 12 12 5 19 12"></polyline>
                        </svg>
                    </div>
                    <div class="downline-stat-content">
                        <span class="downline-stat-label">Total Withdrawals</span>
                        <span class="downline-stat-value text-danger">${{ number_format($allGensWithdrawals, 2) }}</span>
                    </div>
                </div>

                <div class="downline-single-stat">
                    <div class="downline-stat-icon stat-icon-info">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <polyline points="23 6 13.5 15.5 8.5 10.5 1 18"></polyline>
                            <polyline points="17 6 23 6 23 12"></polyline>
                        </svg>
                    </div>
                    <div class="downline-stat-content">
                        <span class="downline-stat-label">Net Flow</span>
                        <span class="downline-stat-value {{ ($allGensDeposits - $allGensWithdrawals) >= 0 ? 'text-success' : 'text-danger' }}">
                            ${{ number_format($allGensDeposits - $allGensWithdrawals, 2) }}
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Search Section -->
    <div class="search-filter-section">
        <h2 class="section-title">Team Members ({{ $members->total() }} total)</h2>
        <div class="search-filter-container">
            <form method="get" action="{{ route('marketer') }}" class="search-form">
                <div class="search-wrapper">
                    <div class="search-input-group">
                        <select name="filter" class="filter-select">
                            <option value="email" {{ request('filter') == 'email' ? 'selected' : '' }}>Email</option>
                            <option value="username" {{ request('filter') == 'username' ? 'selected' : '' }}>Store Name</option>
                        </select>
                        <input type="text" name="value" class="search-input" placeholder="Search members..." value="{{ request('value') }}">
                        <button type="submit" class="search-button">
                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <circle cx="11" cy="11" r="8"></circle>
                                <path d="m21 21-4.35-4.35"></path>
                            </svg>
                            Search
                        </button>
                    </div>
                    @if(request('filter') && request('value'))
                        <a href="{{ route('marketer') }}" class="clear-filter-btn">
                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <line x1="18" y1="6" x2="6" y2="18"></line>
                                <line x1="6" y1="6" x2="18" y2="18"></line>
                            </svg>
                            Clear Filters
                        </a>
                    @endif
                </div>
            </form>
        </div>
    </div>

    <!-- Members Table -->
    <div class="tabs-container">
        <div class="tabs-content">
            <div class="tab-pane active">
                <!-- Mobile Cards View -->
                <div class="mobile-cards mobile-only">
                    @forelse($members as $member)
                    <div class="member-mobile-card">
                        <div class="member-mobile-header">
                            <div class="member-avatar">
                                {{ strtoupper(substr($member->username ?? $member->first_name, 0, 2)) }}
                            </div>
                            <div class="member-mobile-info">
                                <h4>
                                    {{ ucfirst($member->username ?? $member->first_name . ' ' . $member->last_name) }}
                                </h4>
                                <p>{{ $member->email }}</p>
                                @if(isset($memberStats[$member->id]) && $memberStats[$member->id]['deposits'] > 0)
                                    <span class="status-badge status-active">Active</span>
                                @else
                                    <span class="status-badge status-inactive">Inactive</span>
                                @endif
                            </div>
                        </div>
                        <div class="member-mobile-stats">
                            <div class="mobile-stat">
                                <span class="mobile-stat-label">Balance</span>
                                <span class="mobile-stat-value">${{ number_format($member->balance, 2) }}</span>
                            </div>
                            <div class="mobile-stat">
                                <span class="mobile-stat-label">Deposits</span>
                                <span class="mobile-stat-value text-success">${{ number_format($memberStats[$member->id]['deposits'] ?? 0, 2) }}</span>
                            </div>
                            <div class="mobile-stat">
                                <span class="mobile-stat-label">Withdrawals</span>
                                <span class="mobile-stat-value text-danger">${{ number_format($memberStats[$member->id]['withdrawals'] ?? 0, 2) }}</span>
                            </div>
                            <div class="mobile-stat">
                                <span class="mobile-stat-label">Referrals</span>
                                <span class="team-size-badge">{{ number_format($member->referrals_count) }}</span>
                            </div>
                        </div>
                        @if($member->username)
                        <div class="member-mobile-footer">
                            <a href="{{ route('member', $member->username) }}" class="action-btn action-btn-primary" target="_blank">
                                View Details
                            </a>
                        </div>
                        @endif
                    </div>
                    @empty
                    <div class="empty-state">
                        <svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1">
                            <circle cx="12" cy="12" r="10"></circle>
                            <line x1="12" y1="8" x2="12" y2="12"></line>
                            <line x1="12" y1="16" x2="12.01" y2="16"></line>
                        </svg>
                        <h3>No Records Found</h3>
                        <p>No team members match your search criteria</p>
                    </div>
                    @endforelse
                </div>
                
                <!--Desktop view-->
                <div class="table-container desktop-only">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>Member</th>
                                <th>Email</th>
                                <th>Balance</th>
                                <th>Deposits</th>
                                <th>Withdrawals</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($members as $member)
                            <tr>
                                <td>
                                    <div class="member-cell">
                                        <div class="member-avatar">{{ strtoupper(substr($member->username ?? $member->first_name, 0, 2)) }}</div>
                                        <span class="member-name">{{ ucfirst($member->username ?? $member->first_name) }}</span>
                                    </div>
                                </td>
                                <td class="text-muted">{{ $member->email }}</td>
                                <td class="text-bold">${{ number_format($member->balance, 2) }}</td>
                                <td class="text-success">${{ number_format($memberStats[$member->id]['deposits'] ?? 0, 2) }}</td>
                                <td class="text-danger">${{ number_format($memberStats[$member->id]['withdrawals'] ?? 0, 2) }}</td>
                                <td>
                                    @if(isset($memberStats[$member->id]) && $memberStats[$member->id]['deposits'] > 0)
                                        <span class="status-badge status-active">Active</span>
                                    @else
                                        <span class="status-badge status-inactive">Inactive</span>
                                    @endif
                                </td>
                                <td>
                                    @if($member->username)
                                        <a href="{{ route('member', $member->username) }}" class="action-btn action-btn-primary" target="_blank">View</a>
                                    @else
                                        <span class="text-muted">N/A</span>
                                    @endif
                                </td>
                            </tr>
                            @empty
                            <tr><td colspan="7" class="text-center"><div class="empty-state"><p>No team members found</p></div></td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                @if($members->hasPages())
                <div class="pagination-wrapper">{{ $members->appends(request()->query())->links('pagination::bootstrap-4') }}</div>
                @endif
            </div>
        </div>
    </div>
</div>

<form id="move-rebate-form" action="{{ route('rebate_to_balance') }}" method="post" style="display:none;">
    @csrf
    <input type="hidden" name="amount" value="{{ auth()->user()->rebates }}">
</form>

@include('marketer-styles')
@endsection