@extends('layouts.app')
@section('title', 'Credit Packages')

@section('content')
<div class="container-fluid py-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Available Credit Packages</h5>
                </div>
                <div class="card-body">
                    @if(session('success'))
                        <div class="alert alert-success">{{ session('success') }}</div>
                    @endif
                    
                    @if($packages->isEmpty())
                        <div class="text-center py-5">
                            <p class="text-muted">No credit packages available at the moment.</p>
                        </div>
                    @else
                        <div class="row g-4">
                            @foreach($packages as $package)
                                <div class="col-md-4">
                                    <div class="card h-100 border-primary">
                                        <div class="card-header bg-primary text-white">
                                            <h5 class="mb-0">{{ $package->name }}</h5>
                                        </div>
                                        <div class="card-body text-center">
                                            <h2 class="text-primary mb-3">{{ number_format($package->credits, 2) }}</h2>
                                            <p class="text-muted">Credits</p>
                                            <hr>
                                            <h3 class="mb-3">{{ App\CPU\Helpers::formatCurrency($package->price) }}</h3>
                                            @if($package->description)
                                                <p class="text-muted small">{{ $package->description }}</p>
                                            @endif
                                            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#requestModal{{ $package->id }}">
                                                <i class="mdi mdi-cart-plus"></i> Request Package
                                            </button>
                                        </div>
                                    </div>
                                </div>

                                <!-- Request Modal -->
                                <div class="modal fade" id="requestModal{{ $package->id }}" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <form action="{{ route('reseller.credit.packages.request', $package->id) }}" method="POST" enctype="multipart/form-data">
                                                @csrf
                                                <div class="modal-header">
                                                    <h5 class="modal-title">Request: {{ $package->name }}</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="alert alert-info">
                                                        <strong>Package:</strong> {{ $package->name }}<br>
                                                        <strong>Credits:</strong> {{ number_format($package->credits, 2) }}<br>
                                                        <strong>Price:</strong> {{ App\CPU\Helpers::formatCurrency($package->price) }}
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label">Payment Method</label>
                                                        <select name="payment_method" class="form-select" required>
                                                            <option value="">Select Payment Method</option>
                                                            <option value="gcash">GCash</option>
                                                            <option value="paymaya">PayMaya</option>
                                                            <option value="bank_transfer">Bank Transfer</option>
                                                            <option value="crypto">Cryptocurrency</option>
                                                            <option value="other">Other</option>
                                                        </select>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label">Payment Reference / Transaction ID</label>
                                                        <input type="text" name="payment_reference" class="form-control" placeholder="Enter your payment reference or transaction ID" required>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label">Proof of Payment (Screenshot/Receipt)</label>
                                                        <input type="file" name="proof_of_payment" class="form-control" accept="image/*,.pdf">
                                                        <small class="text-muted">Upload a screenshot or photo of your payment receipt (optional but recommended)</small>
                                                    </div>
                                                    <p class="text-muted small">After submitting, wait for admin approval. Your credits will be added once approved.</p>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" class="btn btn-primary">Submit Request</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection