@extends('backend.layout.app')

@section('content')
<div class="container">

    <div class="row">
        @if(optional($settings)->boost_social)
        <div class="width-50">
            <div class="card cron-card">
                <h3 class="cron-title">Send orders</h3>
                <p class="text-warning">Run every minute</p>
                <div class="input-group">
                    <input type="text" readonly class="form-control cron-input-field" value="{{ $cronSendOrders }}">
                    <div class="input-group-append cron-input">
                        <button type="button" class="js-copy-cron-url"><i class="las la-copy"></i> Copy url</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="width-50">
            <div class="card cron-card">
                <h3 class="cron-title">Sync status</h3>
                <p class="text-warning">Run every minute</p>
                <div class="input-group">
                    <input type="text" readonly class="form-control cron-input-field" value="{{ $cronSyncStatus }}">
                    <div class="input-group-append cron-input">
                        <button type="button" class="js-copy-cron-url"><i class="las la-copy"></i> Copy url</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="width-50">
            <div class="card cron-card">
                <h3 class="cron-title">Sync services</h3>
                <p class="text-warning">Run every 5–15 minutes</p>
                <div class="input-group">
                    <input type="text" readonly class="form-control cron-input-field" value="{{ $cronSyncServices }}">
                    <div class="input-group-append cron-input">
                        <button type="button" class="js-copy-cron-url"><i class="las la-copy"></i> Copy url</button>
                    </div>
                </div>
            </div>
        </div>
        @endif
    </div>

    <div class="row">
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Total users</p>
                    <h2>{{ $totalUsers }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-user"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Active users</p>
                    <h2>{{ $activeUsers }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-user-check"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Inactive users</p>
                    <h2>{{ $inactiveUsers }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-user-times"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Total user balance</p>
                    <h2>{{ number_format($totalUserBalance, 2) }} {{ optional($settings)->website_currency ?? 'USD' }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-wallet"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Total deposits</p>
                    <h2>{{ number_format($totalDeposits, 2) }} {{ optional($settings)->website_currency ?? 'USD' }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-money-bill-wave-alt"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Today's deposits</p>
                    <h2>{{ number_format($todayDeposits, 2) }} {{ optional($settings)->website_currency ?? 'USD' }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-calendar-day"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Total withdrawn</p>
                    <h2>{{ number_format($totalWithdrawn, 2) }} {{ optional($settings)->website_currency ?? 'USD' }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-hand-holding-usd"></i>
                </div>
            </div>
        </div>
        @if(optional($settings)->buy_number)
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Total numbers</p>
                    <h2>{{ $totalNumbers }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-sim-card"></i>
                </div>
            </div>
        </div>
        @endif
        @if(optional($settings)->boost_social)
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Total SMM orders</p>
                    <h2>{{ $totalSmmOrders }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-shopping-cart"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>SMM revenue</p>
                    <h2>{{ number_format($totalSmmRevenue, 2) }} {{ optional($settings)->website_currency ?? 'USD' }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-chart-line"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>Pending SMM orders</p>
                    <h2>{{ $pendingSmmOrders }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-clock"></i>
                </div>
            </div>
        </div>
        <div class="width-25">
            <div class="card card-stats">
                <div class="card-stats-text">
                    <p>SMM providers</p>
                    <h2>{{ $totalSmmProviders }}</h2>
                </div>
                <div class="card-stats-icon">
                    <i class="las la-server"></i>
                </div>
            </div>
        </div>
        @endif
    </div>


    <div class="card table-card">
        <div class="table-header">
            <h2>All users</h2>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>Sl</th>
                        <th>Full name</th>
                        <th>Email</th>
                        <th>Balance</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($recentUsers as $index => $user)
                        <tr>
                            <td>{{ $index + 1 }}</td>
                            <td>{{ $user->fname }} {{ $user->lname }}</td>
                            <td>{{ $user->email }}</td>
                            <td>{{ number_format($user->balance, 2) }} {{ optional($settings)->website_currency ?? 'USD' }}</td>
                            <td>
                                <span class="badge {{ $user->status ? 'active' : 'inactive' }}">
                                    {{ $user->status ? 'Active' : 'Inactive' }}
                                </span>
                            </td>
                            <td>
                                <div class="table-actions">
                                    <a href="{{ route('backend.users.details', $user->id) }}" class="table-btn"><i class="las la-pen"></i></a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center">No users found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

@push('script')
<script>
(function () {
    document.querySelectorAll('.js-copy-cron-url').forEach(function (btn) {
        btn.addEventListener('click', function () {
            var card = this.closest('.cron-card');
            var input = card ? card.querySelector('.cron-input-field') : null;
            var url = input ? input.value : '';
            if (!url) return;
            if (navigator.clipboard && navigator.clipboard.writeText) {
                navigator.clipboard.writeText(url).then(function () {
                    if (typeof iziToast !== 'undefined') {
                        iziToast.success({ message: 'URL copied to clipboard', position: 'topRight' });
                    } else {
                        alert('URL copied.');
                    }
                }).catch(function () {
                    fallbackCopy(url);
                });
            } else {
                fallbackCopy(url);
            }
        });
    });
    function fallbackCopy(text) {
        var ta = document.createElement('textarea');
        ta.value = text;
        ta.style.position = 'fixed';
        ta.style.opacity = '0';
        document.body.appendChild(ta);
        ta.select();
        try {
            document.execCommand('copy');
            if (typeof iziToast !== 'undefined') {
                iziToast.success({ message: 'URL copied to clipboard', position: 'topRight' });
            } else {
                alert('URL copied.');
            }
        } catch (e) {}
        document.body.removeChild(ta);
    }
})();
</script>
@endpush
@endsection
