 @extends('admin.layouts.main-layout')
 @section('content')
<div class="card">
        <div class="card-header">
            <h6 class="card-title">Winner Prediction</h6>
        </div>
         <div class="card-body">
        
       </div>

        <!-- /.card-header -->
        <div class="card-body">
           
            {{-- Result Form --}}
        <form action="" method="POST">
            @csrf

            <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="{{ 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" required>
                        <option value="">Select an option</option>

                        @foreach($stores as $game)
                            <option value="{{ $game->id }}">
                                {{ $game->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <!-- Session -->
                <div class="col-md-4 mb-3">
                    <label><strong>Session</strong></label>
                    <select name="type" class="form-control" required>
                        <option value="">Select Session</option>
                        <option value="open">Open</option>
                        <option value="close">Close</option>
                    </select>
                </div>

                <!-- Pana -->
                <div class="col-md-3 mb-3">
                    <label><strong>Open Pana</strong></label>
                    <select name="open_panna" class="form-control select2" required>
                        <option value="">Select an Open Pana</option>

                        @foreach(pannaList() as $pana)
                            <option value="{{ $pana }}">
                                {{ $pana }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3 mb-3">
                    <label><strong>Open Digit</strong></label>
                    <input type="text" 
                           name="opendigit" 
                           class="form-control"
                           placeholder=""
                           id="open_digit_sum"
                           readonly
                           >
                </div>
                <div class="col-md-3 mb-3">
                    <label><strong>Close Digit</strong></label>
                    <input type="text" 
                           name="closedigit" 
                           class="form-control"
                           placeholder=""
                           id="close_digit_sum"
                           readonly
                           >
                </div>
                <div class="col-md-3 mb-3">
                    <label><strong>Close Pana</strong></label>
                    <select name="close_panna" class="form-control select2" required>
                        <option value="">Select an Close Pana</option>

                        @foreach(pannaList() as $pana)
                            <option value="{{ $pana }}">
                                {{ $pana }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <!-- Save Button -->
                <div class="col-md-12 mt-2">
                    
                    <button type="button"
                            class="btn btn-info px-4"
                            id="show_winners">
                        Show Winners
                    </button>
                </div>

            </div>
        </form>
           
            <div id="example1_wrapper" class=" dt-bootstrap4 mt-4">
         
                <div class="row">
                    <div class="col-sm-12">
                          <div class='tablescroll'>
                            <div class="table-responsive" id="winnerModalBody">
                        
                            </div>
                          </div>
                  </div>
                </div>
              </div>
            </div>
          </div>


 @endsection
@section('js-content')
<script>
$(document).on('change', 'select[name="open_panna"]', function () {

    let val = $(this).val() || ""; // panna like 127

    let sum = 0;

    for (let i = 0; i < val.length; i++) {
        sum += parseInt(val[i]);
    }

    let lastDigit = sum % 10;

    $('#open_digit_sum').val(val ? lastDigit : '');
});
$(document).on('change', 'select[name="close_panna"]', function () {

    let val = $(this).val() || ""; // panna like 127

    let sum = 0;

    for (let i = 0; i < val.length; i++) {
        sum += parseInt(val[i]);
    }

    let lastDigit = sum % 10;

    $('#close_digit_sum').val(val ? lastDigit : '');
});

</script>
<script>
$('#show_winners').click(function () {

    $.ajax({
        url: "{{ url('admin/declare-result') }}",
        type: "POST",
        data: {
            _token: "{{ csrf_token() }}",
            date: $('[name="date"]').val(),
            stores_id: $('[name="stores_id"]').val(),
            type: $('[name="type"]').val(),
            open_panna: $('[name="open_panna"]').val(),
            close_panna: $('[name="close_panna"]').val(),
            btn_type: "show_winner"
        },
        beforeSend: function () {
            $('#winnerModalBody').html('<p>Loading...</p>');
        },
        success: function (response) {
            $('#winnerModalBody').html(response);
        },
        error: function () {
            $('#winnerModalBody').html('<p>Error loading data</p>');
        }
    });

});

</script>



@endsection