@extends('layouts.app')
@section('title', 'Agents')
@section('content')
    <!-- Start Main Content -->
    <div class="d-flex justify-content-end p-1">
        <a class="btn btn-primary" href="{{ route('agents.create') }}">Create</a>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4>Agents</h4>
                    <div class="card-header-form">
                        <x-search-bar route="{{ route('agents.index') }}" />
                    </div>
                </div>
                <div class="card-body p-2">
                    <div class="table-responsive">
                        <table id="myTable" class="table table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Name</th>
                                    <th>Phone</th>
                                    <th>Blocked</th>
                                    <th>Balance (₹)</th>
                                    <th>Total Betting Amount (₹)</th>
                                    <th>Total Winning Amount (₹)</th>
                                    <th>Total Commission Amount (₹)</th>
                                    <th>Created At</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($agents as $key => $agent)
                                    <tr>
                                        <td> <span class="badge badge-primary">{{ $agent->id }}</span></td>
                                        <td>
                                            <a href="{{ route('bettings.index', ['search' => $agent->id]) }}"
                                                class="link-primary">
                                                {{ $agent->name }}
                                            </a>
                                        </td>
                                        <td>{{ $agent->phone }}</td>
                                        <td>
                                            <div class="pretty p-switch p-fill  ">
                                                <input onchange="toggleBlock({{ $agent->id }})" type="checkbox"
                                                    @checked($agent->is_blocked)>
                                                <div class="state">
                                                    <label></label>
                                                </div>
                                            </div>
                                        </td>
                                        <td><span class="badge badge-info">{{ number_format($agent->balance) }}</span>
                                        </td>
                                        <td>{{ number_format($agent->totalBettingAmount()) }}</td>
                                        <td>{{ number_format($agent->totalWinningAmount()) }}</td>
                                        <td>{{ number_format($agent->totalCommissionAmount()) }}</td>
                                        <td>{{ $agent->created_at->format('h:m a d-m-Y') }}</td>
                                        <td>
                                            <div class="d-flex">
                                                <a href="{{ route('agent-balance.update', $agent->id) }}"
                                                    class="btn btn-primary">
                                                    Balance
                                                </a> |
                                                <a href="{{ route('agents.edit', $agent->id) }}" class="btn btn-primary">
                                                    Edit
                                                </a> |
                                                <form action="{{ route('agents.destroy', $agent->id) }}" method="post">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button class="btn btn-danger">
                                                        Delete
                                                    </button>
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                        {{ $agents->links() }}
                        @unless (filled($agents))
                            <div class="mx-4 my-4 d-flex justify-content-center">
                                <h5> No agents added yet.</h5>
                            </div>
                        @endunless
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- End Main Content -->
    @push('scripts')
        <script>
            function toggleBlock(id) {
                $.ajax({
                    type: 'POST',
                    url: '/agents/' + id + '/toggle-block',
                    dataType: 'json',
                    data: {
                        _token: '{{ csrf_token() }}',
                    },
                    success: function(response) {
                        console.log(response);
                    },
                    error: function(xhr, status, error) {
                        console.error('Error updating block status:', error);
                    }
                });

            }
        </script>
    @endpush
@endsection
