@extends('layouts.admin')

@section('title', 'Admin Dashboard')
@section('header_title', 'Platform Overview & Analytics')

@section('content')
    <!-- Stat Grid -->
    <div class="grid-stats">
        <div class="stat-card">
            <div class="stat-icon"><i class="fa-solid fa-store"></i></div>
            <div>
                <div style="color: var(--text-muted); font-size: 0.9rem;">Total Vendors</div>
                <div class="stat-val">{{ $totalVendors }}</div>
                <div style="color: var(--success); font-size: 0.8rem; margin-top: 2px;">{{ $activeVendors }} Active</div>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="background: rgba(236, 72, 153, 0.15); color: #EC4899;"><i class="fa-solid fa-indian-rupee-sign"></i></div>
            <div>
                <div style="color: var(--text-muted); font-size: 0.9rem;">Total Revenue</div>
                <div class="stat-val">₹{{ number_format($totalRevenue, 0) }}</div>
                <div style="color: var(--text-muted); font-size: 0.8rem; margin-top: 2px;">Via Razorpay</div>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="background: rgba(16, 185, 129, 0.15); color: #10B981;"><i class="fa-solid fa-glasses"></i></div>
            <div>
                <div style="color: var(--text-muted); font-size: 0.9rem;">Catalog Inventory</div>
                <div class="stat-val">{{ $totalFrames + $totalLenses }}</div>
                <div style="color: var(--text-muted); font-size: 0.8rem; margin-top: 2px;">{{ $totalFrames }} Frames / {{ $totalLenses }} Lenses</div>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="background: rgba(245, 158, 11, 0.15); color: #F59E0B;"><i class="fa-solid fa-users"></i></div>
            <div>
                <div style="color: var(--text-muted); font-size: 0.9rem;">App Customers</div>
                <div class="stat-val">{{ $totalCustomers }}</div>
                <div style="color: var(--text-muted); font-size: 0.8rem; margin-top: 2px;">Mobile Users</div>
            </div>
        </div>
    </div>

    <!-- Recent Vendors -->
    <div class="card">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
            <h3><i class="fa-solid fa-shop"></i> Recent Optical Store Registrations</h3>
            <a href="{{ route('admin.vendors') }}" class="btn btn-primary" style="font-size: 0.8rem;">View All Vendors</a>
        </div>

        <table>
            <thead>
                <tr>
                    <th>Vendor Name</th>
                    <th>Contact Person</th>
                    <th>City / Location</th>
                    <th>Active Plan</th>
                    <th>Plan Expiry</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach($recentVendors as $vendor)
                    <tr>
                        <td>
                            <strong style="color: #fff;">{{ $vendor->vendorProfile->vendor_name ?? $vendor->name }}</strong>
                            <div style="font-size: 0.8rem; color: var(--text-muted);">{{ $vendor->unique_token }}</div>
                        </td>
                        <td>
                            {{ $vendor->name }}
                            <div style="font-size: 0.8rem; color: var(--text-muted);">{{ $vendor->phone }}</div>
                        </td>
                        <td>{{ $vendor->vendorProfile->city ?? 'N/A' }}</td>
                        <td>
                            @if($vendor->activeSubscription)
                                <span class="badge badge-success">{{ $vendor->activeSubscription->package->name ?? 'Active' }}</span>
                            @else
                                <span class="badge badge-danger">Expired / None</span>
                            @endif
                        </td>
                        <td>
                            @if($vendor->activeSubscription)
                                {{ $vendor->activeSubscription->expires_at->format('d M, Y') }}
                            @else
                                -
                            @endif
                        </td>
                        <td>
                            <span class="badge {{ $vendor->status ? 'badge-success' : 'badge-danger' }}">
                                {{ $vendor->status ? 'Active' : 'Deactivated' }}
                            </span>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>

    <!-- Recent Transactions -->
    <div class="card">
        <h3><i class="fa-solid fa-receipt"></i> Recent Subscription Transactions</h3>
        <table>
            <thead>
                <tr>
                    <th>Txn ID</th>
                    <th>Optical Store</th>
                    <th>Package</th>
                    <th>Amount</th>
                    <th>Status</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                @forelse($recentTransactions as $txn)
                    <tr>
                        <td><code>{{ $txn->txn_id }}</code></td>
                        <td>{{ $txn->user->vendorProfile->vendor_name ?? $txn->user->name }}</td>
                        <td>{{ $txn->package->name ?? 'Package' }}</td>
                        <td>₹{{ number_format($txn->amount, 2) }}</td>
                        <td>
                            <span class="badge {{ $txn->status === 'success' ? 'badge-success' : 'badge-danger' }}">
                                {{ ucfirst($txn->status) }}
                            </span>
                        </td>
                        <td>{{ $txn->created_at->format('d M Y, h:i A') }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" style="text-align: center; color: var(--text-muted); padding: 2rem;">No recent payment transactions recorded.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
@endsection
