@extends('layouts.reseller')

@section('title', 'Orders')

@section('content')
<div class="content-wrapper">
    <div class="row">
        <div class="col-sm-12">
            <h4 class="mb-4">My Orders</h4>
        </div>
    </div>

    <!-- Tabs -->
    <ul class="nav nav-tabs mb-4" id="orderTabs" role="tablist">
        <li class="nav-item">
            <button class="nav-link active" id="user-orders-tab" data-bs-toggle="tab" data-bs-target="#user-orders" type="button">
                <i class="fa-solid fa-users"></i> User Orders
            </button>
        </li>
        <li class="nav-item">
            <button class="nav-link" id="credit-requests-tab" data-bs-toggle="tab" data-bs-target="#credit-requests" type="button">
                <i class="fa-solid fa-credit-card"></i> Credit Requests
                @if($creditRequests->where('status', 'pending')->count() > 0)
                <span class="badge bg-warning ms-1">{{ $creditRequests->where('status', 'pending')->count() }}</span>
                @endif
            </button>
        </li>
    </ul>

    <div class="tab-content" id="orderTabsContent">
        <!-- User Orders Tab -->
        <div class="tab-pane fade show active" id="user-orders" role="tabpanel">
            <div class="row">
                <div class="col-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <!-- Search -->
                            <form method="GET" class="mb-4">
                                <div class="row">
                                    <div class="col-md-4">
                                        <input type="text" name="search" class="form-control" placeholder="Search by user name or email" value="{{ request('search') }}">
                                    </div>
                                    <div class="col-md-2">
                                        <button type="submit" class="btn btn-primary">Search</button>
                                    </div>
                                </div>
                            </form>

                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>Order ID</th>
                                            <th>User</th>
                                            <th>Plan</th>
                                            <th>Amount</th>
                                            <th>Payment Method</th>
                                            <th>Status</th>
                                            <th>Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @forelse($orders as $order)
                                        <tr>
                                            <td>#{{ $order->id }}</td>
                                            <td>{{ $order->user->name ?? 'N/A' }}<br><small>{{ $order->user->email ?? '' }}</small></td>
                                            <td>{{ $order->plan->name ?? 'N/A' }}</td>
                                            <td>{{ App\CPU\Helpers::formatCurrency($order->price) }}</td>
                                            <td>{{ $order->payment_method ?? 'N/A' }}</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="7" class="text-center">No user orders found</td>
                                        </tr>
                                        @endforelse
                                    </tbody>
                                </table>
                            </div>

                            <div class="mt-4">
                                {{ $orders->links() }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Credit Requests Tab -->
        <div class="tab-pane fade" id="credit-requests" role="tabpanel">
            <div class="row">
                <div class="col-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Package</th>
                                            <th>Credits</th>
                                            <th>Amount</th>
                                            <th>Payment Method</th>
                                            <th>Reference</th>
                                            <th>Proof</th>
                                            <th>Status</th>
                                            <th>Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @forelse($creditRequests as $request)
                                        <tr>
                                            <td>#{{ $request->id }}</td>
                                            <td>{{ $request->creditPackage->name ?? 'N/A' }}</td>
                                            <td>{{ number_format($request->creditPackage->credits ?? 0, 2) }}</td>
                                            <td>{{ App\CPU\Helpers::formatCurrency($request->amount_paid) }}</td>
                                            <td>{{ $request->payment_method ?? '-' }}</td>
                                            <td>{{ $request->payment_reference ?? '-' }}</td>
                                            <td>
                                                @if($request->proof_of_payment)
                                                    <a href="/pixy/public/storage/proof_of_payment/{{ basename($request->proof_of_payment) }}" target="_blank" class="btn btn-sm btn-info">
                                                        <i class="fa-solid fa-image"></i>
                                                    </a>
                                                @else
                                                    <span class="text-muted">-</span>
                                                @endif
                                            </td>
                                            <td>
                                                @if($request->status == 'pending')
                                                <span class="badge badge-warning">Pending</span>
                                                @elseif($request->status == 'approved')
                                                <span class="badge badge-success">Approved</span>
                                                @else
                                                <span class="badge badge-danger">Rejected</span>
                                                @endif
                                            </td>
                                            <td>{{ $request->created_at->format('Y-m-d') }}</td>
                                        </tr>
                                        @empty
                                        <tr>
                                            <td colspan="9" class="text-center">No credit requests found</td>
                                        </tr>
                                        @endforelse
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection