@extends('admin.layouts.app')

@section('admin_content')
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                @include('admin.alerts')
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">Registered Users List</h3>

                                <!--                    <div class="card-tools">
                                                        <div class="input-group input-group-sm" style="width: 150px;">
                                                            <input type="text" name="table_search" class="form-control float-right"
                                                                   placeholder="Search">

                                                            <div class="input-group-append">
                                                                <button type="submit" class="btn btn-default">
                                                                    <i class="fas fa-search"></i>
                                                                </button>
                                                            </div>
                                                        </div>
                                                    </div>-->
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body table-responsive p-0">
                                <table class="table table-hover text-nowrap">
                                    <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>First Name</th>
                                        <th>Last Name</th>
                                        <th>Wallet Balance</th>
                                        <th>Profit</th>
                                        <th>Action</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    @forelse($users as $user)
                                        <tr>
                                            <td>{{$user->id}}</td>
                                            <td>{{$user->first_name}}</td>
                                            <td>{{$user->last_name}}</td>
                                            <td>$ {{$user->wallet_balance}}</td>
                                            <td>$ {{$user->profit}}</td>
                                            <td>
                                                <div class="row">
                                                    <div class="btn-group btn-group-toggle" data-toggle="buttons">
                                                        <label class="btn btn-primary">
                                                            <input type="radio" name="status" id="option_a1"
                                                                   autocomplete="off" class="user-status"
                                                                   value="activate" data-user="{{$user->id}}"
                                                                   checked="{{$user->trashed() ? '':'checked'}}"> Active
                                                        </label>
                                                        <label class="btn btn-danger">
                                                            <input type="radio" name="status" id="option_a2"
                                                                   autocomplete="off" class="user-status"
                                                                   value="deactivate" data-user="{{$user->id}}"
                                                                {{$user->trashed() ? 'checked':''}}>
                                                            Inactive
                                                        </label>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="4">No records found!</td>
                                        </tr>
                                    @endforelse
                                    </tbody>
                                </table>
                            </div>
                            <!-- /.card-body -->
                        </div>
                        <!-- /.card -->
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('admin-custom-scripts')
    <script>
        $(document).ready(function () {
            $('.user-status').change(function () {
                var status = $(this).val();
                var userId = $(this).data('user');
                var url = `{{url('admin/users')}}/${userId}`;
                $.ajax({
                    url: url,
                    type: "PUT",
                    dataType: 'JSON',
                    data: {
                        status: status
                    },
                    success: function (output) {
                        toastr.success(output.message)
                    },
                    error: function (error) {
                        if (error.status === 422) {
                            toastr.error(error.response.data.errors.wallet_amount[0])
                        } else {
                            toastr.error('Something went wrong. Please try again!')
                        }
                    }
                });
            })
        });
    </script>
@endpush
