@extends('layouts.admin.app')

@section('title', 'Bonus logs')

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title mr-3">
                <span class="page-header-icon">
                    <img src="{{asset('public/assets/admin/img/customer-loyalty.png')}}" class="w--26" alt="">
                </span>
                <span>Bonus logs</span>
            </h1>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form action="{{ route('admin.users.customer.bonus-logs') }}" method="get">
                    <div class="row g-3 align-items-end">
                        <div class="col-md-3">
                            <label class="input-label">Bonus type</label>
                            <select class="form-control" name="bonus_type">
                                <option value="">All</option>
                                <option value="signup" {{ request('bonus_type') === 'signup' ? 'selected' : '' }}>Signup</option>
                                <option value="review" {{ request('bonus_type') === 'review' ? 'selected' : '' }}>Review</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">Status</label>
                            <select class="form-control" name="status">
                                <option value="">All</option>
                                <option value="granted" {{ request('status') === 'granted' ? 'selected' : '' }}>Granted</option>
                                <option value="blocked" {{ request('status') === 'blocked' ? 'selected' : '' }}>Blocked</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">Block reason</label>
                            <select class="form-control" name="reason">
                                <option value="">All</option>
                                @foreach($reasonOptions as $reason)
                                    <option value="{{ $reason }}" {{ request('reason') === $reason ? 'selected' : '' }}>
                                        {{ str_replace('_', ' ', $reason) }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">Search</label>
                            <input type="text" class="form-control" name="search" value="{{ request('search') }}" placeholder="Email, phone, device, customer">
                        </div>
                        <div class="col-12">
                            <div class="btn--container justify-content-end">
                                <a href="{{ route('admin.users.customer.bonus-logs') }}" class="btn btn--reset">Reset</a>
                                <button type="submit" class="btn btn--primary">Filter</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="table-responsive">
                <table class="table table-hover table-borderless table-thead-bordered table-nowrap table-align-middle card-table">
                    <thead class="thead-light">
                        <tr>
                            <th>ID</th>
                            <th>Attempted</th>
                            <th>Type</th>
                            <th>Customer</th>
                            <th>Email</th>
                            <th>Phone</th>
                            <th>Device</th>
                            <th>Bonus</th>
                            <th>Status</th>
                            <th>Block reason</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($logs as $log)
                            <tr>
                                <td>{{ $log->id }}</td>
                                <td>{{ $log->attempted_at ? $log->attempted_at->format('Y-m-d H:i') : $log->created_at?->format('Y-m-d H:i') }}</td>
                                <td>{{ ucfirst($log->bonus_type) }}</td>
                                <td>
                                    @if($log->user)
                                        <a href="{{ route('admin.users.customer.view', $log->user_id) }}">
                                            {{ trim(($log->user->f_name ?? '').' '.($log->user->l_name ?? '')) ?: '#'.$log->user_id }}
                                        </a>
                                    @else
                                        #{{ $log->user_id }}
                                    @endif
                                </td>
                                <td>{{ $log->email ?? '-' }}</td>
                                <td>{{ $log->phone ?? '-' }}</td>
                                <td class="text-truncate" style="max-width: 180px;" title="{{ $log->device_identifier }}">{{ $log->device_identifier ?? '-' }}</td>
                                <td>
                                    @if($log->bonus_type === 'signup')
                                        {{ \App\CentralLogics\Helpers::format_currency($log->bonus_amount) }}
                                    @else
                                        {{ $log->bonus_points }} points
                                    @endif
                                </td>
                                <td>
                                    @if((int) $log->granted_status === 1)
                                        <span class="badge badge-soft-success">Granted</span>
                                    @else
                                        <span class="badge badge-soft-danger">Blocked</span>
                                    @endif
                                </td>
                                <td>{{ $log->block_reason ? str_replace('_', ' ', $log->block_reason) : '-' }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="10" class="text-center py-5">No bonus logs found</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($logs->hasPages())
                <div class="card-footer">
                    {{ $logs->links() }}
                </div>
            @endif
        </div>
    </div>
@endsection
