@extends('layouts.app_operasi')

@section('content')

<div class="modal fade" id="kelulusanmodal">
    <div class="modal-dialog modal-lg modal-dialog-centered">
      <div class="modal-content">
        <!-- Modal Header -->
        <div class="modal-header">
            <h4 class="text-center">Maklumat Lanjut</h4>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <!-- Modal body -->
        <div class="modal-body">
          
        <div class="row col-12 text-center">
            <div class="row col-7">   
                <label>Nama : </label>
                <span id="namalanjut"> </span>
             </div>

            <div class="row col-5">   
                <label>Kad Pengenalan : </label>
                <span id="nokplanjut"> </span>
            </div>
        </div>

        <div class="row mt-3">

          <div class="col-lg-6 col-xl-4 col-12">
            <!-- small box -->
            <div class="small-box bg-info">
              <div class="inner">
                <h4 id="pinjterkumpul"></h4>
                <p>Pembiayaan Terkumpul</p>
              </div>
              <div class="icon">
                <i class="fas fa-money-bill-wave"></i>
              </div>
            </div>
          </div>

          <div class="col-lg-6 col-xl-4 col-12">
            <!-- small box -->
            <div class="small-box bg-success">
              <div class="inner">
                <h4 id="bilaktif"></h4>
                <p>Pembiayaan Belum Selesai</p>
              </div>
              <div class="icon">
                <i class="fas fa-thumbs-up"></i>
              </div>
            </div>
          </div>


          <div class="col-lg-6 col-xl-4 col-12">
            <!-- small box -->
            <div class="small-box bg-danger">
              <div class="inner">
                <h4 id="billelong"></h4>
                <p>Bilangan Lelong</p>
              </div>
              <div class="icon">
                <i class="fas fa-gavel"></i>
              </div>
            </div>
          </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>


<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="{{ url('/') }}">Home</a></li>
                      <li class="breadcrumb-item active" aria-current="page">Kelulusan</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 mt-3 justify-content-center">
                        </div>
                        <div class="col-2">
                        </div>
                    </div>

                    <table class='table mt-2 table-striped table-hover' id='table-kelulusan' style="width:100%;">
                        <thead class="thead-dark">
                            <tr>
                                <th>No Rujukan</th>
                                <th>Cawangan</th>
                                <th>Teller</th>
                                <th>Komen</th>
                                <th>Status</th>
                                <th>Nilai Pembiayaan</th>
                                <th>Nilai Marhun</th>
                                <th>Berat</th>
                                <th>Margin</th>
                                <th>Tindakan</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($arraykelulusan as $index=>$k)
                            <tr>    
                                <td><a href="#" data-toggle="modal" data-target="#kelulusanmodal" data-kodcawangan="{{ $k['cawangankod'] }}" data-norujukan="{{ $k['norujukan'] }}">{{ $k['norujukan'] }} </a></td>
                                <td>{{ $k['cawangan'] }}</td>
                                <td>{{ $k['namateller'] }}</td>
                                <th>{{ $k['komen'] }}</th>
                                <td>{{ $k['status'] }}</td>
                                <td>{{ number_format($k['pinjaman'],2) }}</td>
                                <td>{{ $k['nilaimarhun'] }}</td>
                                <td>{{ $k['berat'] }}</td>
                                <td>{{ $k['margin'] }}</td>
                                <td><button class="btn btn-primary mr-2" onclick="updatelulus(this)" data-kodcaw="{{ $k['cawangankod'] }}" data-pelulus="{{ $k['pelulus'] }}"  data-norujukan="{{ $k['norujukan'] }}" data-siri="{{ $k['siri'] }}"><i class="fas fa-thumbs-up"></i></button>
                                    <button class="btn btn-danger" onclick="updatebatal(this)" data-kodcaw="{{ $k['cawangankod'] }}" data-norujukan="{{ $k['norujukan'] }}" data-pelulus="{{ $k['pelulus'] }}" data-siri="{{ $k['siri'] }}" ><i class="fas fa-thumbs-down"></i></button></td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>

                    <div class="row mt-4 d-flex justify-content-end">
                
                        <a href="{{ route ('operasi.rekodlepas') }}" class="btn btn-info mr-3">Rekod Lulus</a>

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

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

    let api = "{{ config('api.url') }}";

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

	var audio = new Audio("{{ asset('sound/3.wav') }}");

	if(tablekelulusan.data().any()){
		audio.play();
		audio.addEventListener('ended', function() {
			this.currentTime = 0;
			this.play();
		}, false);
	}else{
		audio.loop = false;
		audio.muted = true;
	}


    $('#kelulusanmodal').on('show.bs.modal', function(event) {
        let button = $(event.relatedTarget);
        let norujukan = $(button).data('norujukan');
        let kodcaw = $(button).data('kodcawangan');

        $.ajax({
          method:'GET',
          url: api + '/api/gadai/norujukan/' + norujukan,
          dataType: 'json',
          success:function(datagadaiwithnorujukan){

            let nokp = datagadaiwithnorujukan[0][0].nokp;

            $.ajax({
                method:'GET',
                url: api + '/api/customers/' + nokp,
                dataType: 'json',
                success:function(datagadaiwithnokp){

                let nama = datagadaiwithnokp.nama;

                $('#namalanjut').text(nama);
                $('#nokplanjut').text(nokp);

                $.ajax({
                    method:'GET',
                    url: api + '/api/gadai/customers/' + nokp + '/outstandingsemasa/' + kodcaw,
                    dataType: 'json',
                    success:function(outstandingsemasa){

                        $('#pinjterkumpul').text('RM' + formatter.format(outstandingsemasa));

                    },error: function(xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        alert(xhr.responseText);
                    }
                });


                }
            });

            $.ajax({
            method:'GET',
            url: api + '/api/customers/' + nokp + '/gadai/belum_tebus',
            dataType: 'json',
            success:function(data){
                let bilanganaktif = 0;
                if(data == null)
                {
                    bilanganaktif = 0;
                }else{
                    bilanganaktif = data[0].length;
                }

                $('#bilaktif').text(bilanganaktif);


            }
            });


            $.ajax({
            method:'GET',
            url: api + '/api/customers/' + nokp + '/gadai/lelong',
            dataType: 'json',
            success:function(data){
                
                let bilanganlelong = 0;

                if(data == null)
                {
                    bilanganlelong = 0;
                }else{
                    bilanganlelong = data.length;
                }


                $('#billelong').text(bilanganlelong);
            }
            });


          }
        });

    });


    async function updatelulus(e)
    {

        const { value: katalaluan } = await Swal.fire({
            title: 'Masukkan Katalaluan Semula',
            input: 'password',
            inputPlaceholder: 'Katalaluan',
            confirmButtonText: 'Hantar',
            showCancelButton:true,
            cancelButtonText:'Batal',
            allowOutsideClick: true
        })

        let dataminta = '';

        $.ajax({
            method:'get',
            url: "{{ route('operasi.getmintasemulakelulusan') }}",
            async:false,
            data:{ passsemula : katalaluan },
            success:function(data)
            {
                if(data == "False")
                {
                    dataminta = data;
                }
            }
        });

        if(dataminta == 'False')
        {
            Swal.fire({
                    icon: 'error',
                    title: 'Pengesahan Katalaluan Tidak Berjaya'
                })
            event.preventDefault();
            return false;
        }

        // var { value: komenkelulusan } = await Swal.fire({
        //     title: 'Masukkan Komen!',
        //     input: 'radio',
        //     customClass: {
        //         input: 'custom-radio'
        //     },
        //     inputOptions: {
        //         'Pelanggan Baru': 'Pelanggan Baru',
        //         'Pelanggan Tetap': 'Pelanggan Tetap',
        //         'Rekod Gadaian Baik': 'Rekod Gadaian Baik',
        //         'Lain2': 'Lain-Lain'
        //     },
        //     confirmButtonText: 'Hantar',
        //     showCancelButton:false,
        //     allowOutsideClick: false
        // })

           // if(komenkelulusan === 'Lain2')
        // {
        //     var { value: komenkelulusan } = await Swal.fire({
        //     title: 'Masukkan Komen!',
        //     input: 'text',
        //     inputPlaceholder: 'Komen',
        //     confirmButtonText: 'Hantar',
        //     showCancelButton:false,
        //     allowOutsideClick: false
        //     });
        // }

        const { value: komenkelulusan } = await Swal.fire({
            title: 'Masukkan Komen!',
            input: 'text',
            inputPlaceholder: 'Komen',
            confirmButtonText: 'Hantar',
            showCancelButton:false,
            allowOutsideClick: false
        })

        let norujukan = $(e).data('norujukan');
        let cawangankod = $(e).data('kodcaw');
        let siri = $(e).data('siri');
        let namaoperasi = "{{ Auth::user()->name }}";
        let teller = $(e).closest('tr').children('td:eq(2)').text();

        teller = String(teller);

        $.ajax({
            method:'put',
            url: "{{ route('operasi.updatekelulusan') }}",
            data: {
                "_token": "{{ csrf_token() }}",
                "norujukan" : norujukan,
                "kodcaw" : cawangankod,
                "komenkelulusan" : komenkelulusan,
                "siri" : siri,
                'namaoperasi' : namaoperasi,
                'teller' : teller
            },
            success:function(data){
                console.log(data);
                Swal.fire({
                    icon: 'success',
                    title: 'Pengesahan Berjaya'
                }).then(function(){
                    location.reload();
                });

            },error: function(xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(xhr.responseText);
            }
        });  

    }

    async function updatebatal(e)
    {
        const { value: katalaluan } = await Swal.fire({
            title: 'Masukkan Katalaluan Semula',
            input: 'password',
            inputPlaceholder: 'Katalaluan',
            confirmButtonText: 'Hantar',
            showCancelButton:true,
            cancelButtonText:'Batal',
            allowOutsideClick: true
        })

        let dataminta = '';

        $.ajax({
            method:'get',
            url: "{{ route('operasi.getmintasemulakelulusan') }}",
            async:false,
            data:{ passsemula : katalaluan },
            success:function(data)
            {
                if(data == "False")
                {
                    dataminta = data;
                }
            }
        });

        if(dataminta == 'False')
        {
            Swal.fire({
                    icon: 'error',
                    title: 'Pengesahan Katalaluan Tidak Berjaya'
                })
            event.preventDefault();
            return false;
        }

        const { value: komenkelulusan } = await Swal.fire({
            title: 'Masukkan Komen',
            input: 'text',
            inputPlaceholder: 'Komen',
            confirmButtonText: 'Hantar',
            showCancelButton:false,
            allowOutsideClick: false
        })

        let norujukan = $(e).data('norujukan');
        let siri = $(e).data('siri');
        let cawangankod = $(e).data('kodcaw');
        let namaoperasi = "{{ Auth::user()->name }}";
        let teller = $(e).closest('tr').children('td:eq(2)').text();

        teller = String(teller);

        $.ajax({
            method:'put',
            url: "{{ route('operasi.updatekelulusan') }}",
            data: {
                "_token": "{{ csrf_token() }}",
                "norujukan" : norujukan,
                "siri" : siri,
                "kodcaw" : cawangankod,
                "komenkelulusan" : komenkelulusan,
                "pembatalan" : "batal",
                'namaoperasi' : namaoperasi,
                'teller' : teller
            },
            success:function(data){
                Swal.fire({
                    icon: 'success',
                    title: 'Pengesahan Berjaya'
                }).then(function(){
                    location.reload();
                    
                });

            },error: function(xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(xhr.responseText);
            }
        });  
    }

</script>
@endsection
