@extends('layouts.app')

@section('content')
<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">Serahan</li>
                    </ol>
                  </nav>

                <div class="card-body text-center">

                    @if(session()->has('message'))
                        <div class="alert alert-danger">
                            {{ session()->get('message') }}
                        </div>
                    @endif

                    
                    <div class="row mb-4">
                        <div class="col-2 justify-content-start">
                        </div>
                        <div class="col-8 mt-3 justify-content-center">
                        </div>
                        <div class="col-2">
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-4">
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text" id="basic-addon1">Tarikh Mula</span>
                                </div>
                                <input type="text" class="form-control" placeholder="Pilih Tarikh" aria-label="Tarikh" id="tarikhmula" name="tarikhmula" aria-describedby="basic-addon1">
                            </div>
                        </div>

                        <div class="col-4">
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text" id="basic-addon1">Tarikh Hingga</span>
                                </div>
                                <input type="text" class="form-control" placeholder="Pilih Tarikh" aria-label="Tarikh" id="tarikhhingga" name="tarikhhingga" aria-describedby="basic-addon1">
                            </div>
                        </div>

                        {{-- <div class="col-4">
                            <input type="submit" class="btn btn-primary ml-md-5" id="submit_button" name="submit_button" value="Hantar">
                        </div> --}}

                    </div>


                     <table class='table table-striped table-hover' id='table-serahan' style="width:100%;">
                        <thead class="thead-dark">
                            
                                <th>Bil</th>
                                <th>Teller ID</th>
                                <th>Cawangan</th>
                                <th>Modal</th>
                                <th>Serahan</th>
                                <th>Tarikh</th>
                                <th>Masa</th>
                                <th>Tindakan</th>
                            
                        </thead>
                        <tbody>                           
                        </tbody>
                    </table>
                
<br>
            
                </div>
            </div>
        </div>
    </div>
</div>


       <!-- Modal body -->
       
       <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">
                <div class="row">
                    <div class="col-md-12">
                        <h4>Pendahuluan/Serahan (RM)</h4>
                        <table id="biasa" class="table table-striped table-bordered text-center" style="width:50%">
                            <thead>
                                <tr>
                                   <td> Tarikh</td><td> </td>
                                </tr>
                                <tr>
                                   <td>Teller ID</td><td> </td>
                                </tr>

                                <tr>
                                   <td>AMAUN (RM)</td><td> </td>
                                </tr>
                            </thead>
                            <tbody id="tablemarhun">
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        Jumlah Berat Marhun (gm)
                    </div>
                    <div class="col-md-4">
                        <input type="text" readonly id="jumlah_berat" name="jumlah_berat" class="form-control">
                    </div>
                </div>
            </div>

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

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

<script>
    var api_url = "<?php echo $api_url ?>";
    var kodcaw = "{{ Auth::user()->cawangan }}";

    $(document).ready(function(){

        $('#tarikhmula').datepicker({
            format: 'dd-mm-yyyy'
        });
        $('#tarikhhingga').datepicker({
            format: 'dd-mm-yyyy'
        });

        function getData(){
            $.ajax({
                type: 'get',
                url: api_url + '/api/requestTeller/get/' + kodcaw,
                success: function(data){
                    gettable(data);
                }
            });
        }
        getData();
        // setInterval(function () {
        //     getData(); 
        // }, 10000);  

    });

var tableserahan = $('#table-serahan').DataTable({
      "scrollX": true
    });

    $('#tarikhmula').change(function(){

        var input_date = this.value;
        var split_date = input_date.split('-');
        var tarikhmula = split_date[2] + '-' +split_date[1]+'-'+split_date[0];

        let tarikhhingga = $('#tarikhhingga').val();

        if(tarikhhingga == '')
        {
            var current = new Date();
            var current_date = current.getDate();
            var current_month = current.getMonth()+1;
            var current_year = current.getFullYear();
            var today_date = current_year + '-' +current_month+'-'+current_date;

            tarikhhingga = today_date;
            
        }else{
            var input_hingga = tarikhhingga;
            var split_date = input_hingga.split('-');
            tarikhhingga = split_date[2] + '-' +split_date[1]+'-'+split_date[0];
        }

        $.ajax({
            method:'get',
            url: api_url + '/api/requestTeller/get/' + kodcaw + '/date',
            data: { 
                'mula' : tarikhmula,
                'hingga' : tarikhhingga
                },
            success: function(data){
                gettable(data);
            }
        });
    });

$('#tarikhhingga').change(function(){

    var input_date = this.value;
    var split_date = input_date.split('-');
    var tarikhhingga = split_date[2] + '-' +split_date[1]+'-'+split_date[0];

    let tarikhmula = $('#tarikhmula').val();

    if(tarikhmula == '')
    {
        tarikhmula = '2018-01-01';
    }else{
        var input_mula = tarikhmula;
        var split_date = input_mula.split('-');
        tarikhmula = split_date[2] + '-' +split_date[1]+'-'+split_date[0];
    }

    $.ajax({
        method:'get',
        url: api_url + '/api/requestTeller/get/' + kodcaw + '/date',
        data: { 
            'mula' : tarikhmula,
            'hingga' : tarikhhingga
            },
        success: function(data){
            gettable(data);
        }
    });
});

function gettable(datala)
{
    var counter = 1;
    tableserahan.clear().draw();
    $.each(datala, function(index,arr){
    
        if(arr['serahan'] > 0 && arr['modal'] == 0)
        {

        var urlroute = "{{ route('khazanah.serah', ':no_id') }}";

        urlroute = urlroute.replace(':no_id', arr['no']);

        tableserahan.row.add([
                    counter,
                    arr['kodlaluan'],
                    arr['kodcaw'],
                    'RM ' + arr['modal'],
                    'RM ' + arr['serahan'],
                    arr['tarikh'],
                    arr['masa'],
                    '<a href='+ urlroute + ' class="btn btn-info"><span class="fas fa-plus"></span></a>'
                ]).draw();
                counter = counter + 1;
        }
    });
}

</script>
@endsection
