 @extends('admin.layouts.main-layout')
 @section('content')
<div class="card">
      <div class="card-body">
        
        <h1>Bit Revert</h1>
        
       <p class="badge badge-warning" style="
                    font-size: 19px;
                    padding: 13px;white-space: normal;
                ">Bet Revert - This page allows you to revert bets, Once bets are reverted, they will be deleted parmanently and cannot be recovered, Any deleted bets amount will be credited back to user's wallet</p>
        </div>
    
        
        <div class="card-body">
            
            <form action="" method="GET">

            <div class="row">

                <!-- Date -->
                <div class="col-md-4 mb-3">
                    <label><strong>Date</strong></label>
                    <input type="date" 
                           name="date" 
                           class="form-control"
                           value="{{ request('date') ?? date('Y-m-d') }}">
                </div>

                <!-- Game -->
                <div class="col-md-4 mb-3">
                    <label><strong>Game</strong></label>
                    <select name="stores_id" class="form-control select2">
                        <option value="">Select an option</option>

                        @foreach(storeList() as $game)
                            <option value="{{ $game->id }}" {{request('stores_id') == $game->id ? 'selected' :'' }}>
                                {{ $game->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <!-- Game -->
                <div class="col-md-4 mb-3">
                    <label><strong>User</strong>(optional)</label>
                    <select name="user_id" class="form-control select2">
                        <option value="">Select User</option>

                        @foreach(userList() as $game)
                            <option value="{{ $game->id }}" {{request('user_id') == $game->id ? 'selected' :'' }}>
                                {{ $game->name }} ({{$game->mobile_number}})
                            </option>
                        @endforeach
                    </select>
                </div>
                <!-- Game -->
                <div class="col-md-4 mb-3">
                    <label><strong>Bit Type</strong></label>
                    <select name="game_id" class="form-control select2">
                        <option value="">Select Bit Type</option>

                        @foreach([
                'singledigit',
                'pannafamily',
                'singlepanna',
                'doublepanna',
                'triplepanna',
                'singlepatti',
                'doublepatti',
                'triplepatti',
                'fullsangam',
                'halfsangam',
                'cyclepatti',
                'jodi',
                'jodifamily',
                'redjodi',
                'redjodifamily',
                'spmotor',
                'dpmotor'
            ] as $game)
                            <option value="{{ $game }}" {{request('game_id') == $game ? 'selected' :'' }}>
                                {{ $game }}
                            </option>
                        @endforeach
                    </select>
                </div>


                <!-- Digit -->
                <div class="col-md-4 mb-3">
                    <label><strong>Min Amount</strong></label>
                    <input type="number" 
                           name="min_amount" 
                           class="form-control"
                           placeholder="Enter Min Amount"
                           id="min_amount"
                           value="{{request('min_amount')}}"
                           >
                </div>

                <!-- Save Button -->
                <div class="col-md-12 mt-2">
                    <button type="submit" class="btn btn-primary px-4">
                        Apply Filters
                    </button>
                    <a href="{{url('admin/bidrevert')}}">
                    <button type="button" class="btn btn-primary px-4">
                        Reset Filters
                    </button>
                    </a>
                </div>

            </div>
        </form>
        
        
            
            @if(session()->has('message'))
                <div class="alert alert-success">
                    {{ session()->get('message') }}
                </div>
            @endif 
            <form action="{{ url('admin/bid-revert-all') }}" method="POST">
    @csrf
    
    <button type="submit"
        id="get-selected"
        class="btn btn-danger mt-3"
        style="display:none;"
        name="type"
        value="revert_select"
        onclick="return confirm('Are you sure you want to revert the selected bets? This action cannot be undone.')">
    Revert Selected Bets
</button>
            <div id="example1_wrapper" class=" dt-bootstrap4">
                <div class="row">
                    <div class="col-sm-12">
                         <div class='tablescroll'>
                            <div class="table-responsive">
                               
                        <table class="table table-bordered text-center mt-4">
                            <thead>
                                <tr role="row">
                                            <th style="width: 60px;">
                                                <input type="checkbox" id="select-all">
                                            </th>
                                            <th>#</th>
                                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                                Mobile Number
                                            </th>
                                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                                User Name
                                            </th>
                                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                                Market
                                            </th>
                                            
                                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                              Game Name
                                            </th>
                                            <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                             Session
                                            </th>
                                             <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                             Digits
                                            </th>
                                             <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                             Amount
                                            </th>
                                             <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                             Game Status
                                            </th>
                                             <th class="sorting" tabindex="0" aria-controls="example1" rowspan="1" colspan="1"
                                                aria-label="Browser: activate to sort column ascending" style="width: 207px;">
                                             Placed On
                                            </th>
                                            
                                            
                                          
                                           
                                            
                                        </tr>
                            </thead>
                            <tbody>
                                         
                              @forelse($lists as $key => $list)
                            
                                    <tr>
                                        <td>
                                            <input type="checkbox"
                                               class="row-checkbox"
                                               name="bids[]"
                                               value='@json([
                                                   "id" => $list->id,
                                                   "game_id" => $list->bit_details->bit->game_id ?? null,
                                                   "userwin" => $list->userwin ?? null
                                               ])'>
                                        </td>
                                      <td>{{ $key+1 }}</td>
                                      <td>
                                        <a>{{isset($list->username) ? $list->username->mobile_number : ''}}</a>
                                      </td>
                                      <td>
                                        <a>{{isset($list->username) ? $list->username->name : ''}}</a>
                                      </td>
                                      <td>
                                        <a>{{isset($list->bit_details) ? $list->bit_details->bit->store->name : ''}}</a>
                                      </td>
                                     
                                      <td>
                                        <a>{{isset($list->bit_details) ? $list->bit_details->bit->game->name : ''}}</a>
                                      </td>
                                      
                                      <td>
                                        <a>{{isset($list->bit_details) ? $list->bit_details->bit->type : ''}}</a>
                                      </td>
                                      <td>
                                          @if(isset($list->bit_details))
                                            @if(isset($list->bit_details->bit_place))
                                                <a>{{isset($list->bit_details) ? $list->bit_details->bit_place : ''}}</a>
                                            @endif
                                            @if(isset($list->bit_details->bit_place_panna))
                                               @if($list->bit_details->bit->type == 'open')
                                                <a>{{$list->bit_details->bit_place_panna}}-{{$list->bit_details->bit_place_single}}</a>
                                                @else
                                                
                                                <a>{{$list->bit_details->bit_place_single}}-{{$list->bit_details->bit_place_panna}}</a>
                                                @endif
                                            @endif
                                          @endif
                                        
                                      </td>
                                      
                                      <td>
                                            
                                        <a>{{$list->amount}}</a>
                                             
                                      </td>
                                      
                                      <td>
                                            
                                        <a>{{isset($list->userwin) ? 'Win' : 'Loss'}}</a>
                                             
                                      </td>
                                      <td>
                                            
                                        <a>{{$list->created_at}}</a>
                                             
                                      </td>
                                    
                                    </tr>
                                     @empty
                              <tr>
                                <td colspan="6">Record not found </td>
                              </tr>
                              @endforelse
                                        </tbody>
                        </table>
                    </div>
                   </div>
                   
                    <div class="row">
                      <div class="col-md-6 text-left" style="margin: 20px 0px;">
                        Showing  {{$lists->firstItem()}} to {{$lists->lastItem()}} of {{$lists->total()}}
                      </div>
                      <div class="col-md-6 text-right">
                        {!! $lists->appends(request()->input())->links() !!}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              
              <div class="row">
                  @foreach(request()->query() as $key => $value)
                    <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                @endforeach
                @if(count($lists) > 0)
                      <button type="submit" class="btn btn-danger btn-block" name="type" value="revert_all"  onclick="return confirm('Are you sure you want to revert the all bets? This action cannot be undone.')">
                            Bid Revert
                        </button>
                        @endif
                     
              </div>
              
                        </form>
            </div>
          </div>


 @endsection
@section('js-content')
<script>
$(document).ready(function () {

    function toggleRevertButton() {
        if ($('.row-checkbox:checked').length > 0) {
            $('#get-selected').show();
        } else {
            $('#get-selected').hide();
        }
    }

    $('#select-all').on('change', function () {
        $('.row-checkbox').prop('checked', $(this).prop('checked'));
        toggleRevertButton();
    });

    $('.row-checkbox').on('change', function () {
        $('#select-all').prop(
            'checked',
            $('.row-checkbox').length === $('.row-checkbox:checked').length
        );

        toggleRevertButton();
    });

    toggleRevertButton();
});
</script>
@endsection