@extends('layouts.app')
@section('content')
<!-- The Modal -->
<div class="modal fade" id="marhunModal">
    <div class="modal-dialog modal-xl modal-dialog-centered">
        <div class="modal-content">

          <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title"></h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

          <!-- Modal body -->
            <div class="modal-body">
                <h4>Marhun</h4>
                <table id="biasa" class="table table-striped table-bordered text-center" style="width:100%">
                    <thead>
                        <tr>
                            <th>Bil</th>
                            <th>Jenis Marhun</th>
                            <th>Berat</th>
                            <th>Karat</th>
                            <th>Nilai Marhun</th>
                            <th>Nota</th>
                        </tr>
                    </thead>
                    <tbody id="tablemarhun">
                    </tbody>
                </table>
          </div>

          <!-- Modal footer -->
          <div class="modal-footer">
              <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
          </div>

      </div>
  </div>
</div>


<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
              
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb pb-3">
                      <li class="breadcrumb-item"><a href="{{ route('home') }}">Home</a></li>
                      <li class="breadcrumb-item"><a href="{{ url('menu') }}">Khazanah</a></li>
                      <li class="breadcrumb-item active" aria-current="page">Kelulusan Tebus</li>
                    </ol>
                  </nav>

                <div class="card-body text-center">
                    <div class="row mb-4">
                        <div class="col-2 justify-content-start">
                        </div>
                        <div class="col-8 justify-content-center">
                        <form action="{{ route('barcodetebus.scanned') }}" onsubmit="return checkserah(this)" method="post">
                                @csrf
                            <label>Scan Barcode Keluar Marhun Dari Peti Besi </label>
                            <input type="text" class="form-control col-6 mx-auto" name="norujukan_barcode" autocomplete="off" id="norujukan_barcode"  placeholder="SILA SCAN BARCODE PADA SAG"/>
                            <input type="submit" id="submit_button" name="submit_button" hidden>
                        </form>

                        </div>
                        <div class="col-2">
                            {{-- <h4>Jumlah SAG : <span class="badge badge-secondary">{{ count($listserah) + count($senaraitebus) }} </span></h4> --}}

                        </div>
                    </div>

                    <ul class="nav nav-tabs" id="myTab" role="tablist">
                        <li class="nav-item">
                            <a class="nav-link active" id="gadaian-semasa-tab" data-toggle="tab" href="#gadaian-semasa" role="tab" aria-controls="gadaian-semasa" aria-selected="true">Penebusan</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" id="gadaian-belum-serah-tab" data-toggle="tab" href="#gadaian-belum-serah" role="tab" aria-controls="gadaian-belum-serah" aria-selected="false">Rekod</a>
                        </li>
                    </ul>

                    <div class="tab-content" id="myTabContent">
                        <div class="tab-pane fade show active" id="gadaian-semasa" role="tabpanel" aria-labelledby="gadaian-semasa-tab">
                
                            <table class='table table-striped table-hover' id='table-gadaian-semasa'>
                                <thead class="thead-dark">
                                    <tr>
                                        <th>Bil</th>
                                        <th>No Rujukan</th>
                                        <th>Penilai</th>
                                        <th>Transaksi</th>
                                        <th>Nilai Marhun</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($listserah as $ls)
                                    <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td> 
                                    @php
                                    $marhun = Http::get($api_url . '/api/marhun/' . $ls['norujukan'] )->json();
                                    // dd($marhun);
                                    @endphp
                                    
                                    <a href="#" data-toggle="modal" data-marhun="{{ json_encode($marhun) }}" data-norujukan="{{$ls['norujukan']}}" data-kodcaw="{{ $ls['kodcaw'] }}" 
                                                data-target="#marhunModal">  {{$ls['norujukan']}} </a></td>
                                    <td></td>
                                    <td>{{ $ls['jenistebus'] }}</td>
                                        <td>{{ $ls['nilaimarhun'] }}</td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <div class="tab-pane fade" id="gadaian-belum-serah" role="tabpanel" aria-labelledby="gadaian-belum-serah-tab">
                        
                             
                            <table class='table table-striped table-hover' id='table-gadaian-belum-serah'>
                                <thead class="thead-dark">
                                    <tr>
                                        <th>Bil</th>
                                        <th>No Rujukan</th>
                                        <th>PO / PPO</th>
                                        <th>Transaksi</th>
                                        <th>Nilai Marhun</th>
                                    </tr>
                                </thead>
                                <tbody>
                                       @foreach($senaraitebus as $index)
                                    <tr>
                                        <td>{{$loop->iteration}}</td>
                                        <td> 
                                            
                                        @php
                                        $marhun = Http::get($api_url . '/api/marhun/' . $index['norujukan'] )->json();
                                        @endphp
                                        
                                        <a href="#" data-toggle="modal" data-marhun="{{ json_encode($marhun) }}" data-norujukan="{{$index['norujukan']}}" data-kodcaw="{{ $index['kodcaw'] }}" 
                                                    data-target="#marhunModal">  {{$index['norujukan']}} </a></td>
                                        <td></td>
                                        <td>{{$index['jenistebus']}}</td>
                                        <td>{{$index['nilaimarhun']}}</td>
                                    </tr>
                                   @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="row mt-5">
                    <div class="mx-auto">
                      
                    </div>
                    <div class="form-group d-flex justify-content-end mr-4">
                        <a href="{{ url('menu') }}" class="btn btn-primary">Cetak</a>
                    </div>
                </div>
               
                </div>
            </div>
        </div>
    </div>
</div>

<script>

var api_url = "<?php echo $api_url ?>";


    var table_gadaian_semasa = $('#table-gadaian-semasa').DataTable({
        "ordering": true
    });
    var table_gadaian_belum_serah = $('#table-gadaian-belum-serah').DataTable({
        "ordering": true
    });

    $(document).ready(function(){
        $('#norujukan_barcode').focus();
    })

    //modal
    $('#marhunModal').on('show.bs.modal', function(event) {
        var button = $(event.relatedTarget); // button yg trigger
        var arraymarhun = button.data('marhun');
        var norujukan = button.data('norujukan');
        var kodcaw = button.data('kodcaw');

        $('#tablemarhun').empty();
        $.each(arraymarhun,function(index,marhun){
            $.each(marhun, function(index,marhun_detail){
                var marhun_name;
                $.ajax({
                    method:'GET',
                    url: api_url + '/api/jemas/nama_marhun',
                    dataType: 'json',
                    data:{
                        marhun:marhun_detail['marhun']
                    },
                    success:function(marhun_ajax){
                        $("#tablemarhun").append("<tr><td>" + marhun_detail['bil'] + "</td><td>" + marhun_ajax['descpt'] +"</td><td>" + marhun_detail['berat'] +"</td><td>" + marhun_detail['karat'] +"</td><td>RM " + marhun_detail['n_marhun'].toLocaleString(undefined,{minimumFractionDigits:2}) +"</td><td>"+ marhun_detail['nota'] +"</td></tr>");
                    }
                });
            });
        });
    });

    function checkserah(func){
        event.preventDefault();
        var norujukan = $('#norujukan_barcode').val();
        $.ajax({
            method:'get',
            url: api_url + '/api/check/serah/tebus',
            datatype:'json',
            data:{
                'kodcaw': "{{ $cawangan_info['kodcaw'] }}",
                'norujukan': norujukan
            },
            success:function(data){
                console.log(data);
                if(data == 'true'){
                    Swal.fire({
                        icon: 'error',
                        title: 'Amaran',
                        text: 'Penebusan Sudah Scan'
                    });
                    event.preventDefault();
                    return false;
                }else if(data == 'false'){
                    $(func).attr("onSubmit","return true;");
                    $(func).submit();
                    return true;
                }
            }
        });
    }

</script>
@endsection
