@extends('layouts.admin')

@section('content')
<div class="container-fluid">
    <div class="card">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <h4 class="card-title mb-0">Affiliates</h4>
            </div>
        </div>
        <div class="card-body">
            @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            @endif

            @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                {{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            @endif

            @if($errors->any())
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <ul class="mb-0">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            @endif
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Code</th>
                            <th>User</th>
                            <th>Balance</th>
                            <th>Approved</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($affiliates as $affiliate)
                        <tr>
                            <td>{{ $affiliate->code }}</td>
                            <td>{{ $affiliate->user?->name ?? 'N/A' }}</td>
                            <td>{{ number_format($affiliate->balance, 2) }}</td>
                            <td>
                                <span class="badge bg-{{ $affiliate->is_approved ? 'success' : 'warning' }}">
                                    {{ $affiliate->is_approved ? 'Approved' : 'Pending' }}
                                </span>
                            </td>
                            <td>
                                <a href="{{ route('admin.affiliates.show', $affiliate) }}" class="btn btn-sm btn-info">View</a>
                                @if(!$affiliate->is_approved)
                                <form action="{{ route('admin.affiliates.approve', $affiliate) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('PATCH')
                                    <button type="submit" class="btn btn-sm btn-success">Approve</button>
                                </form>
                                @endif
                                <form action="{{ route('admin.affiliates.destroy', $affiliate) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</button>
                                </form>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            {{ $affiliates->links('pagination::bootstrap-5') }}
        </div>
    </div>
</div>
@endsection
