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

@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">Credit Package Requests</h5>
                </div>
                <div class="card-body">
                    @if(session('success'))
                        <div class="alert alert-success">{{ session('success') }}</div>
                    @endif
                    
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Reseller</th>
                                    <th>Package</th>
                                    <th>Credits</th>
                                    <th>Amount Paid</th>
                                    <th>Payment Method</th>
                                    <th>Reference</th>
                                    <th>Proof of Payment</th>
                                    <th>Status</th>
                                    <th>Requested</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($requests as $request)
                                    <tr>
                                        <td>{{ $request->id }}</td>
                                        <td>{{ $request->reseller->name ?? 'N/A' }}</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="mdi mdi-image"></i> View
                                                </a>
                                            @else
                                                <span class="text-muted">-</span>
                                            @endif
                                        </td>
                                        <td>
                                            @if($request->status == 'pending')
                                                <span class="badge bg-warning">Pending</span>
                                            @elseif($request->status == 'approved')
                                                <span class="badge bg-success">Approved</span>
                                            @else
                                                <span class="badge bg-danger">Rejected</span>
                                            @endif
                                        </td>
                                        <td>{{ $request->created_at->format('M d, Y H:i') }}</td>
                                        <td>
                                            @if($request->status == 'pending')
                                                <button class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#approveModal{{ $request->id }}">
                                                    <i class="mdi mdi-check"></i>
                                                </button>
                                                <button class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#rejectModal{{ $request->id }}">
                                                    <i class="mdi mdi-close"></i>
                                                </button>
                                            @else
                                                <span class="text-muted">-</span>
                                            @endif
                                        </td>
                                    </tr>

                                    <!-- Approve Modal -->
                                    <div class="modal fade" id="approveModal{{ $request->id }}" tabindex="-1">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <form action="{{ route('admin.credit.requests.approve', $request->id) }}" method="POST">
                                                    @csrf
                                                    <div class="modal-header">
                                                        <h5 class="modal-title">Approve Request</h5>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <p>Approve this credit request for <strong>{{ $request->reseller->name ?? 'N/A' }}</strong>?</p>
                                                        <p>Package: <strong>{{ $request->creditPackage->name ?? 'N/A' }}</strong></p>
                                                        <p>Credits: <strong>{{ number_format($request->creditPackage->credits ?? 0, 2) }}</strong></p>
                                                        <div class="mb-3">
                                                            <label class="form-label">Notes (optional)</label>
                                                            <textarea name="admin_notes" class="form-control" placeholder="Optional notes"></textarea>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-success">Approve</button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Reject Modal -->
                                    <div class="modal fade" id="rejectModal{{ $request->id }}" tabindex="-1">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <form action="{{ route('admin.credit.requests.reject', $request->id) }}" method="POST">
                                                    @csrf
                                                    <div class="modal-header">
                                                        <h5 class="modal-title">Reject Request</h5>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <p>Reject this credit request for <strong>{{ $request->reseller->name ?? 'N/A' }}</strong>?</p>
                                                        <div class="mb-3">
                                                            <label class="form-label">Reason (optional)</label>
                                                            <textarea name="admin_notes" class="form-control" placeholder="Reason for rejection"></textarea>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-danger">Reject</button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                @empty
                                    <tr>
                                        <td colspan="11" class="text-center">No credit requests found.</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection