@extends('layouts.app')
@section('content')
@include('modal.tambahserahan')


@include('modal.denominasimodal')

@if($countproses > 0)
<script>
    Swal.fire({
        title: 'Tunggu Pengesahan Daripada Khazanah.',
        icon: 'info',
        width: 600,
        padding: '3em',
        background: '#fffff',
        showCancelButton: false,
        showConfirmButton: false,
        allowOutsideClick: false,
        allowEscapeKey: false,
        allowEnterKey: false
    })
</script>
@endif


<div class="container">
    <div class="row justify-content-center">
        @php $auth_user = Auth::user();@endphp
        <input type="hidden" id="api" value="{{ $api_url }}"/>
        <div class="col-md-12">
          

            <nav aria-label="breadcrumb">
                <ol class="breadcrumb pb-3">
                  <li class="breadcrumb-item"><a href="{{ route('home') }}">Home</a></li>
                  <li class="breadcrumb-item active" aria-current="page">Aliran Tunai</li>
                </ol>
            </nav>

            
        <div class="row justify-content-center">  


            <div class="justify-content-center col-4">

                <div class="col-12">
                    <div class="small-box bg-info">
                        <div class="inner">
                            <h4>RM {{ number_format($alirantunai['bakiawal'],2) }}</h4>
                            <p>Baki Awal Tunai</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-money-bill-alt"></i>
                        </div>
                    </div>
                </div>

                <div class="col-12">
                    <div class="small-box bg-info">
                        <div class="inner">
                            <h4 id="bakiakhir">RM {{ number_format($alirantunai['baki'],2) }}</h4>
                            <p>Baki Tunai Semasa</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-cash-register"></i>
                        </div>
                    </div>
                </div>

            </div>


            <div class="justify-content-center col-4">
                <div class="col-12">
                    <div class="small-box bg-success">
                        <div class="inner">
                            <h4>RM {{ number_format($alirantunai['tambah'],2) }}</h4>

                            <p>Pendahuluan</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-sign-in-alt"></i>
                        </div>
                    </div>
                </div>
                <div class="col-12">
                    <div class="small-box bg-danger">
                        <div class="inner">
                            <h4>RM {{ number_format($alirantunai['kurang'],2) }}</h4>

                            <p>Serahan</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-sign-out-alt"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="justify-content-center col-4">
                <div class="col-12">
         

                    <div class="small-box bg-success">
                        <div class="inner">
                            <h4>RM {{ number_format($alirantunai['masuk'],2) }}</h4>
                            {{-- <h3>{{ $GT['tcount'] }}</h3> --}}
                            <p>Jumlah Penyelesaian Pem.</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-plus-square"></i>
                        </div>
                    </div>

                </div>
                <div class="col-12">
                 
                    <div class="small-box bg-danger">
                        <div class="inner">
                            <h4>RM {{ number_format($alirantunai['keluar'],2) }}</h4>
                            {{-- <h3>{{ $GT['gcount'] }}</h3> --}}
                            <p>Jumlah Pembiayaan</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-minus-square"></i>
                        </div>
                    </div>

                </div>
            </div>

        </div>

            <div class="row">
                <div class="col-md-12 text-center">
                    <input type="button" class="btn btn-lg btn-primary" value="TAMBAH MODAL" data-kodcaw="{{ $auth_user['cawangan'] }}" data-teller="{{ $auth_user['name'] }}" id="tambahmodal" onclick="serahankemaskini(this)">
                    <input type="button" class="btn btn-lg btn-primary" value="SERAHAN MODAL" data-kodcaw="{{ $auth_user['cawangan'] }}" data-teller="{{ $auth_user['name'] }}" id="serahanmodal" onclick="serahankemaskini(this)">
                </div>
            </div>
            <div class="row mt-3">
                <div class="card col-12">
                    <div class="card-body">


                        <button type="button" onclick="periksaalirantunai();" class="btn btn-primary float-right">Kemaskini</button>
                        <button type="button" onclick="cetaklaporanalirantunai()" class="btn btn-primary float-right mr-1">Aliran Tunai<i class='fas fa-print ml-2'></i></button>
                        <button type="button" onclick="cetakserahankeseluruhan()" class="btn btn-primary float-right mr-1">Serahan<i class='fas fa-print ml-2'></i></button>
                        <nav>
                            <div class="nav nav-tabs mb-4" id="nav-tab" role="tablist">
                              <a class="nav-item nav-link" id="nav-rekod-tab" data-toggle="tab" href="#nav-rekod" role="tab" aria-controls="nav-rekod" aria-selected="false">Rekod</a>
                              <a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">Tunai</a>
                              <a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">Pembiayaan / Penyelesaian Pem.</a>
                              <a class="nav-item nav-link" id="nav-contact-tab" data-toggle="tab" href="#nav-contact" role="tab" aria-controls="nav-contact" aria-selected="false">Tuntut Baki</a>
                              {{-- <a class="nav-item nav-link" id="nav-upah-tab" data-toggle="tab" href="#nav-upah" role="tab" aria-controls="nav-upah" aria-selected="false">Upah Lama</a> --}}
                            </div>
                          </nav>
                          <div class="tab-content" id="nav-tabContent">
                            <div class="tab-pane fade" id="nav-rekod" role="tabpanel" aria-labelledby="nav-rekod-tab">

                                {{-- <div class="row justify-content-end">
                                    <div class="col-3 text-right" style="margin-bottom:-20px;">
                                        <span class="font-weight-bold">Tarikh:</span>
                                        <input type="text" class="col-9" id="tarikhaddtunai" name="tarikhaddtunai" autocomplete="off"  aria-describedby="basic-addon1">
                                    </div>
                                </div>
                                <br>  --}}

                                <table class="table table-striped table-hover  text-center" id="tablerekod">
                                    <thead class="thead-dark">
                                        <tr>
                                            <th>Tarikh</th>
                                            <th>C/F</th>
                                            <th>Pendahuluan</th>
                                            <th>Serahan</th>
                                            <th>Pembiayaan</th>
                                            <th>Penebusan</th>
                                            <th>B/F</th>
                                            <th>Tindakan</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if(is_object($tunaipast) || is_array($tunaipast))
                                        @foreach($tunaipast as $tp)
                                        <tr>
                                            <td>{{ $Tarikh = date("d-m-Y", strtotime($tp['tarikh']))  }}</td>
                                            <td>{{ number_format($tp['bakiawal'],2) }}</td>
                                            <td>{{ number_format($tp['tambah'],2) }}</td>
                                            <td>{{ number_format($tp['kurang'],2) }}</td>
                                            <td>{{ number_format($tp['keluar'],2) }}</td>
                                            <td>{{ number_format($tp['masuk'],2) }}</td>
                                            <td>{{ number_format($tp['baki'],2) }}</td>
                                        <td><btn class="btn btn-primary"><i class="fas fa-print" onclick="rec_acc_statement({{ strtotime($tp['tarikh']) }})"></i></btn></td>
                                        </tr>
                                        @endforeach
                                        @endif
                                    </tbody>
                                </table>

                            </div>

                            <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">

                            
                                <div class="row justify-content-end">
                                    <div class="col-3 text-right" style="margin-bottom:-20px;">
                                        <span class="font-weight-bold">Tarikh:</span>
                                        <input type="text" class="col-9" id="tarikhaddtunai" name="tarikhaddtunai" autocomplete="off"  aria-describedby="basic-addon1">
                                    </div>
                                </div>
                                <br> 

                                <table class="table table-striped table-hover  text-center" id="tableaddtunai">
                                    <thead class="thead-dark">
                                        <tr>
                                            <th>Bil</th>
                                            <th>Tarikh</th>
                                            <th>Debit (RM)</th>
                                            <th>Kredit (RM)</th>
                                            <th>Nota</th>
                                            <th>Tindakan</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if(is_object($addtunai) || is_array($addtunai))
                                        @foreach($addtunai as $at)
                                        <tr>
                                        <td> {{ $loop->iteration }} </td>
                                        <td> {{ $at['tarikh'] }} </td>
                                        <td> {{ number_format($at['tambah'],2) }} </td>
                                        <td> {{ number_format($at['kurang'],2) }} </td>
                                        <td> {{ $at['nota'] }}</td>
                                        <td><btn class="btn btn-primary"><i class="fas fa-print" onclick="rekodprint({{ $at['recno'] }})"></i></btn></td>
                                        </tr>
                                        @endforeach
                                        @endif
                                    </tbody>
                                </table>

                            </div>
                            <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">
                                
                                <table class="table table-striped table-hover text-center" id="tablegadaitebus">
                                    <thead class="thead-dark">
                                        <tr>
                                            <th>Bil</th>
                                            <th>No Rujukan</th>
                                            <th>Pembiayaan (RM)</th>
                                            <th>Nilai Marhun (RM)</th>
                                            <th>Jenis Transaksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if(is_object($gadai_tebus) || is_array($gadai_tebus))
                                        @foreach($gadai_tebus as $gt)
                                        <tr>
                                        <td> {{ $loop->iteration }} </td>
                                        <td> {{ $gt['norujukan'] }} </td>
                                        <td> {{ number_format($gt['pinjaman'],2) }} </td>
                                        <td> {{ number_format($gt['nilaimarhun'],2) }} </td>
                                        <td> @php if($gt['jenistebus'] == ''){
                                                echo '<h4><span class="badge badge-success">Pembiayaan</span></h4>';
                                        }else{
                                            echo '<h4><span class="badge badge-danger">Penyelesaian Pembiayaan</span></h4>';
                                        }  
                                        @endphp </td>
                                        </tr>
                                        @endforeach
                                        @endif
                                    </tbody>
                                </table>

                            </div>
                            <div class="tab-pane fade" id="nav-contact" role="tabpanel" aria-labelledby="nav-contact-tab">

                                <table class="table table-striped table-hover text-center" id="tabletuntutbaki">
                                    <thead class="thead-dark">
                                        <tr>
                                            <th>Bil</th>
                                            <th>Tarikh</th>
                                            <th>Caj Lelong (RM)</th>
                                            <th>Baki Lelong (RM)</th>
                                            <th>Jenis</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if(is_object($tuntutbaki) || is_array($tuntutbaki))
                                        @php  $totaltuntutbaki = 0; $totalcajlelong = 0; @endphp
                                        @foreach($tuntutbaki as $tb)
                                        @php $totaltuntutbaki += $tb['jumlah'];$totalcajlelong += $tb['cajlelong']; @endphp
                                        <tr>
                                        <td> {{ $loop->iteration }} </td>
                                        <td> {{ $tb['tarikh'] }} </td>
                                        <td> {{ number_format($tb['cajlelong'],2) }} </td>
                                        <td> {{ number_format($tb['jumlah'],2) }} </td>
                                        <td> @php if($tb['jenis'] == 'T'){
                                                echo '<h4><span class="badge badge-success">Baki Lelong</span></h4>';
                                        }else{
                                            echo '<h4><span class="badge badge-danger">Rugi Lelong</span></h4>';
                                        }  
                                        @endphp </td>
                                        </tr>
                                        @endforeach
                                        @endif
                                        <tfoot class="table-dark">
                                            <td colspan='2' class=" font-weight-bold">JUMLAH</td>
                                            <td>{{ number_format($totalcajlelong,2)}}</td>
                                            <td>{{ number_format($totaltuntutbaki,2)}}</td>
                                            <td></td>
                                            
                                        </tfoot>
                                    </tbody>
                                </table>

                            </div>
                            <div class="tab-pane fade" id="nav-upah" role="tabpanel" aria-labelledby="nav-upah-tab">

                                <table class="table table-striped table-hover  text-center" id="tableupahlama">
                                    <thead class="thead-dark">
                                        <tr>
                                            <th>Bil</th>
                                            <th>Tarikh</th>
                                            <th>Norujukan</th>
                                            <th>Upah Dibayar (RM)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {{-- {{ dd($advansur) }} --}}
                                        @if(is_object($advansur) || is_array($advansur))
                                        @foreach($advansur as $ansur)
                                        <tr>
                                            <td> {{ $loop->iteration }} </td>
                                            <td> {{ $ansur['tarikh'] }} </td>
                                            <td> {{ $ansur['norujukan'] }} </td>
                                            <td> {{ number_format($ansur['upahDibayar'],2) }} </td>
                                        </tr>
                                        @endforeach
                                        @endif
                                    </tbody>
                                </table>

                            </div>
                          </div>

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

    var api_url = "<?php echo $api_url ?>";
    var kodcaw = "<?php echo $cawangan_info['kodcaw'] ?>";

    var tableaddtunai = $('#tableaddtunai').DataTable({
        "bPaginate": true,
        "bLengthChange": false,
        "bFilter": false,
        "bInfo": true
    });

    var today = new Date();
    $('#tarikhaddtunai').datepicker({
        format: 'dd-mm-yyyy',
        autoclose:true,
        endDate: "today",
        maxDate: today
    });

    var tablerekod = $('#tablerekod').DataTable({
    //   "scrollX": true
    });

    var tablegadaitebus = $('#tablegadaitebus').DataTable({
    //   "scrollX": true
    });

    var tabletuntutbaki = $('#tabletuntutbaki').DataTable({
    //   "scrollX": true
    });

    var tableupahlama = $('#tableupahlama').DataTable({
    //   "scrollX": true
    });

    var tele = "{{ $auth_user['name'] }}";

    function serahankemaskini(data)
    {
        
        Swal.fire({
            title: 'Memuatkan...',
            allowEscapeKey : false,
            allowOutsideClick: false,
            didOpen: function () {
                Swal.showLoading();
            }
        });

        $.ajax({
                type: 'get',
                url: "{{ route('periksa.alirantunai') }}",
                success: function(balasbalik){
                    if(balasbalik == 'sama')
                    {
                        openuptambahdanserah(data);
                    }else{

                        $.ajax({
                            type: 'get',
                            url: "{{ route('kemaskini.alirantunai') }}",
                            data: { "bakiakhir" : balasbalik['bakisemasa'],
                                    "pendahuluan" : balasbalik['pendahuluan'],
                                    "serahan" : balasbalik['serahan'],
                                    "gadaian" : balasbalik['gadaian'],
                                    "penebusan" : balasbalik['penebusan']
                            },
                            success: function(balas){
                                if(balas == 'berjaya')
                                {
                                    Swal.fire({
                                        icon: 'success',
                                        title: 'Berjaya',
                                        text: 'Kemaskini Berjaya!',
                                        closeOnClickOutside: false
                                    }).then(function() {
                                        location.reload();

                                        Swal.fire({
                                            title: 'Memuatkan...',
                                            allowEscapeKey : false,
                                            allowOutsideClick: false,
                                            didOpen: function () {
                                                Swal.showLoading();
                                            }
                                        });
                                    });

                                }else{
                                    Swal.fire({
                                        icon: 'danger',
                                        title: 'Tidak Berjaya',
                                        text: 'Kemaskini tidak berjaya!',
                                        closeOnClickOutside: false
                                    }).then(function() {
                                        location.reload();

                                        Swal.fire({
                                            title: 'Memuatkan...',
                                            allowEscapeKey : false,
                                            allowOutsideClick: false,
                                            didOpen: function () {
                                                Swal.showLoading();
                                            }
                                        });
                                    });
                                }
                            }
                        });
                    }
                }
        });
    }

    function periksaalirantunai()
    {
      
        Swal.fire({
            title: 'Memuatkan...',
            allowEscapeKey : false,
            allowOutsideClick: false,
            didOpen: function () {
                Swal.showLoading();
            }
        });

        $.ajax({
                type: 'get',
                url: "{{ route('periksa.alirantunai') }}",
                success: function(balasbalik){
                    if(balasbalik == 'sama')
                    {
                        Swal.fire({
                            icon: 'warning',
                            title: 'Peringatan',
                            text: 'Aliran tunai selari dengan sistem!'
                        }).then(function() {
                            location.reload();

                            Swal.fire({
                                title: 'Memuatkan...',
                                allowEscapeKey : false,
                                allowOutsideClick: false,
                                didOpen: function () {
                                    Swal.showLoading();
                                }
                            });
                        });

                    }else{

                        $.ajax({
                            type: 'get',
                            url: "{{ route('kemaskini.alirantunai') }}",
                            data: { "bakiakhir" : balasbalik['bakisemasa'],
                                    "pendahuluan" : balasbalik['pendahuluan'],
                                    "serahan" : balasbalik['serahan'],
                                    "gadaian" : balasbalik['gadaian'],
                                    "penebusan" : balasbalik['penebusan']
                            },
                            success: function(balas){

                                if(balas == 'berjaya')
                                {
                                    Swal.fire({
                                        icon: 'success',
                                        title: 'Berjaya',
                                        text: 'Kemaskini Berjaya!',
                                        closeOnClickOutside: false
                                    }).then(function() {
                                        location.reload();

                                        Swal.fire({
                                            title: 'Memuatkan...',
                                            allowEscapeKey : false,
                                            allowOutsideClick: false,
                                            didOpen: function () {
                                                Swal.showLoading();
                                            }
                                        });
                                    });

                                }else{
                                    Swal.fire({
                                        icon: 'danger',
                                        title: 'Tidak Berjaya',
                                        text: 'Kemaskini tidak berjaya!',
                                        closeOnClickOutside: false
                                    }).then(function() {
                                        location.reload();

                                        Swal.fire({
                                            title: 'Memuatkan...',
                                            allowEscapeKey : false,
                                            allowOutsideClick: false,
                                            didOpen: function () {
                                                Swal.showLoading();
                                            }
                                        });
                                    });
                                }

                            }
                        });
                    }
                }
        });
    }

    function cetaklaporanalirantunai()
    {
        let teller = "{{ Auth::user()->name }}";

        let urlroute = '{{ route("laporan.alirantunai", ":id") }}';
        urlroute = urlroute.replace(':id', teller);

        window.open(urlroute, '_blank');

    }

    function cetakserahankeseluruhan()
    {
        let urlroute = '{{ route("laporan.serahanteller") }}';

        window.open(urlroute, '_blank');
    }

    $('#tarikhaddtunai').change(function(){
        var input_date = this.value;
        var split_date = input_date.split('-');
        var today_date = split_date[2] + '-' +split_date[1]+'-'+split_date[0];
        $.ajax({
            method:'get',
            url: api_url + '/api/getAddTunaiDate/' + kodcaw,
            data:{ 'tarikh' : today_date },
            success: function(addtunai){
                gadaitable(addtunai);
            }
        });
    });

    function gadaitable(data){
        var counter = 1;

        tableaddtunai.clear().draw();
        $.each(data,function(index,addtunai){
                 
            tableaddtunai.row.add([
                counter,
                addtunai['tarikh'],
                formatter.format(addtunai['tambah']),
                formatter.format(addtunai['kurang']),
                addtunai['nota']
            ]).draw();
            
            counter = counter + 1;
        });   
    }

    function rekodprint(recno){

        let urlroute = '{{ route("laporan.tunaiteller") }}';
        urlroute += '?recno=' + encodeURIComponent(recno);
        window.open(urlroute, '_blank');
    }

    function rec_acc_statement(tarikh){
        console.log(tarikh);
        let urlroute = '{{ route("laporan.acc_statement" )}}';
        urlroute += '?tarikh=' + tarikh;

        window.open(urlroute, '_blank');
    }

    let pusherenv = "{{ env('PUSHER_APP_KEY') }}";
    let laporanroute = "{{ route('laporan.serahantellerberkala') }}";
    let event_tellercash = "{{ route('event.tellercash') }}";
</script>
<script src="{{ asset('js/main.js') }}"></script>
@endsection
