@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 Penyelesaian Pem.</li>
                    </ol>
                  </nav>

                <div class="card-body text-center">
                    @if (\Session::has('type') == true && Session::get('type') == 'unavailable')
                    <script>
                        Swal.fire(
                            "{!! \Session::get('message') !!}",
                            "{{ \Session::get('norujukan') }}",
                            'warning'
                        )

                    </script>
                    @elseif(\Session::has('type') == true && Session::get('type') == 'scanned')
                    <div class="alert alert-success text-bold d-flex mx-auto justify-content-center w-50">
                        {!! \Session::get('message') !!} <br> {{ \Session::get('norujukan') }}
                    </div>
                    @endif
                    <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') }}" 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" required/>
                            <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">Penyelesaian Pem.</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>Teller</th>
                                        <th>Transaksi</th>
                                        <th>Nilai Marhun</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($listserah as $ls)
                                    <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>
                                    @php
                                    $marhun = Http::withOptions(['verify' => config('app.api_verify')])->get($api_url . '/api/marhun/' . $ls['norujukan'] )->json();
                                    // dd($marhun);
                                    @endphp

                                    <a href="#" style="user-select: none;" data-toggle="modal" data-marhun="{{ json_encode($marhun) }}" data-norujukan="{{$ls['norujukan']}}" data-kodcaw="{{ $ls['kodcaw'] }}"
                                                data-target="#marhunModal">  {{$ls['norujukan']}} </a></td>
                                    <td>{{ $ls['teller'] }}</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::withOptions(['verify' => config('app.api_verify')])->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="{{ route('laporanscantebusbarcode') }}" class="btn btn-primary">Cetak</a>
                    </div>
                </div>

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

<script>

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

    var cawangan = "{{ Auth::user()->cawangan }}";


    const formatter = new Intl.NumberFormat('en-US', {
        minimumFractionDigits: 2
    });


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

    var pusher = new Pusher("{{ env('PUSHER_APP_KEY') }}", {
        cluster: 'ap1',
        encrypted: true
    });

    var channel = pusher.subscribe('tebus-notify');
    channel.bind('trigger-tebus', function(data) {

        if(cawangan == data.cawangan)
        {
            fetchTebusEverytime();
        }

    });

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

    $(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 fetchTebusEverytime(){

        table_gadaian_semasa.clear().draw();

        $.ajax({
            method:'get',
            url: api_url + '/api/listSerahMarhunController/' + cawangan,
            success:function(data){
                var count = 0;
                $.each(data,function(index,tebusmasuk){
                    $.ajax({
                        url: api_url + '/api/marhun/' + tebusmasuk['norujukan'],
                        method:'get',
                        success:function(rekodmarhun){

                            count++;
                            table_gadaian_semasa.row.add([
                                count,
                                '<a style="user-select: none;" href="#" data-toggle="modal" data-marhun=' + JSON.stringify(rekodmarhun) + ' data-norujukan=' + tebusmasuk['norujukan'] + ' data-kodcaw=' + tebusmasuk['kodcaw'] + ' data-target="#marhunModal"> ' + tebusmasuk['norujukan'] + '</a>',
                                tebusmasuk['teller'],
                                tebusmasuk['jenistebus'],
                                tebusmasuk['nilaimarhun']
                            ]).draw();
                        }
                    });
                });
            }
        });
    }

    // 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
