@extends('layouts.reseller')

@section('title', 'Dashboard')

@section('content')
<div class="content-wrapper">
    <div class="row">
        <div class="col-sm-12">
            <h4 class="mb-4">Welcome, {{ $reseller->name }}!</h4>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row">
        <div class="col-md-4 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="text-muted mb-2">Total Users</p>
                            <h3 class="mb-0">{{ $totalUsers }}</h3>
                        </div>
                        <div class="text-primary">
                            <i class="mdi mdi-account-group mdi-36px"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="text-muted mb-2">Active Users</p>
                            <h3 class="mb-0">{{ $activeUsers }}</h3>
                        </div>
                        <div class="text-success">
                            <i class="mdi mdi-account-check mdi-36px"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="text-muted mb-2">Total Revenue</p>
                            <h3 class="mb-0">{{ App\CPU\Helpers::formatCurrency($totalRevenue) }}</h3>
                        </div>
                        <div class="text-warning">
                            <i class="mdi mdi-currency-usd mdi-36px"></i> <!-- Consider dynamic icon if needed -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Balance & Commission -->
    <div class="row">
        <div class="col-md-6 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Wallet Balance</h4>
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <div>
                            <h2 class="mb-0">{{ App\CPU\Helpers::formatCurrency($reseller->balance) }}</h2>
                        </div>
                        <div>
                            <span class="badge badge-success">Commission: {{ $reseller->commission_rate }}%</span>
                        </div>
                    </div>
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addBalanceModal">
                        Add Balance
                    </button>
                </div>
            </div>
        </div>
        <div class="col-md-6 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Quick Stats</h4>
                    <div class="row">
                        <div class="col-6">
                            <p class="text-muted">Company</p>
                            <h5>{{ $reseller->company_name ?? 'N/A' }}</h5>
                        </div>
                        <div class="col-6">
                            <p class="text-muted">Phone</p>
                            <h5>{{ $reseller->phone ?? 'N/A' }}</h5>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Orders -->
    <div class="row">
        <div class="col-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Recent Orders</h4>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Order ID</th>
                                    <th>User</th>
                                    <th>Amount</th>
                                    <th>Status</th>
                                    <th>Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($recentOrders as $order)
                                <tr>
                                    <td>#{{ $order->id }}</td>
                                    <td>{{ $order->user->name ?? 'N/A' }}</td>
                                    <td>{{ App\CPU\Helpers::formatCurrency($order->price) }}</td>
                                    <td>
                                        @if($order->status == 'active')
                                        <span class="badge badge-success">Active</span>
                                        @elseif($order->status == 'expired')
                                        <span class="badge badge-danger">Expired</span>
                                        @else
                                        <span class="badge badge-warning">{{ $order->status }}</span>
                                        @endif
                                    </td>
                                    <td>{{ $order->created_at->format('Y-m-d') }}</td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="5" class="text-center">No orders found</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Add Balance Modal -->
<div class="modal fade" id="addBalanceModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Balance</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="{{ route('reseller.addBalance') }}" method="POST">
                @csrf
                <div class="modal-body">
                    <div class="form-group">
                        <label>Amount ({{ App\CPU\Helpers::getCurrencySymbol() }})</label>
                        <input type="number" name="amount" class="form-control" min="1" step="0.01" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Add Balance</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection