@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <h3 class="text-center text-uppercase mb-5">LELONGAN</h3>
            <div class="form-group row col-12">

                <div class="col-6">
                    <div class="form-group row">
                        <div class="col-4">
                            NAMA
                        </div>
                        <div class="col-8">
                        <input type="text" value="{{ $nama }}" class="form-control" readonly/>
                        </div>
                    </div>
                </div>
                <div class="col-6">
                    <div class="form-group row">
                        <div class="col-4">
                            NO PELANGGGAN
                        </div>
                        <div class="col-8">
                            <input type="text" value="{{ $gadai['nopelanggan'] }}" class="form-control" readonly/>
                        </div>
                    </div>
                </div>
                <div class="col-6">
                    <div class="form-group row">
                        <div class="col-4">
                            NO RUJUKAN
                        </div>
                        <div class="col-8">
                            <input type="text" id="norujukan" value="{{ $gadai['norujukan'] }}" class="form-control" readonly/>
                        </div>
                    </div>
                </div>
                <div class="col-6">
                    <div class="form-group row">
                        <div class="col-4">
                            NO KAD PENGENALAN
                        </div>
                        <div class="col-8">
                            <input type="text" value="{{ $gadai['nokp'] }}" class="form-control" readonly/>
                        </div>
                    </div>
                </div>
                <div class="col-8">
                    <div class="form-group row">
                        <div class="col-3">
                            JENIS LELONG
                        </div>
                        <div class="col-8">
                            <div class="custom-control custom-radio custom-control-inline">
                                <input type="radio" class="custom-control-input" id="radbakilelong" name="radlelong" value="baki" disabled>
                                <label class="custom-control-label" style="color:green;" for="radbakilelong" >Baki Lelong</label>
                              </div>
                              <div class="custom-control custom-radio custom-control-inline">
                                <input type="radio" class="custom-control-input" id="radrugilelong" name="radlelong" value="rugi" disabled>
                                <label class="custom-control-label" style="color:red;" for="radrugilelong" >Rugi Lelong</label>
                              </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-4">
                    <div class="form-group row">
                        <div class="col-2">
                            WAKIL
                        </div>
                        <div class="col-8">
                            <input type="text" class="form-control" name="wakil" id="wakiltxt" />
                        </div>
                        <div class="col-2">
                            <button class="btn btn-primary" id="caribtn" onclick="wakilfunction(this)">Cari</button>
                        </div>
                    </div>
                </div>
            </div>

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

                    <div class="col-12 form-group row">
                        <div class="col-6" id="divbakihargajualan">

                            <div class="form-group row">
                                <div class="col-4">
                                   TARIKH
                                </div>
                                <div class="col-8">
                                    <input type="text" value="{{ $lelong['t_lelong'] }}" class="form-control" readonly/>
                                </div>
                            </div>
                            
                            <div class="form-group row">
                                <div class="col-4">
                                   NO SIRI
                                </div>
                                <div class="col-8">
                                    <input type="text" value="{{ $lelong['sirilel'] }}" class="form-control" readonly/>
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="col-4">
                                    HARGA JUAL (RM)
                                </div>
                                <div class="col-8">
                                    <input type="text" id="hargajual" value="{{ number_format($lelong['hargajual'],2) }}" class="form-control" readonly/>
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="col-4">
                                    PEMBIAYAAN (RM)
                                </div>
                                <div class="col-8">
                                    <input type="text" id="pembiayaan" value="{{ number_format($lelong['pinjaman'],2) }}" class="form-control" readonly/>
                                </div>
                            </div>

                        </div>

                        <div class="col-6">

                            <div class="form-group row">
                                <div class="col-4">
                                   BATCH LELONG
                                </div>
                                <div class="col-8">
                                    <input type="text" id="batch" value="{{ $lelong['batchno'] }}" class="form-control" readonly/>
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="col-4">
                                    KEUNTUNGAN (RM)
                                </div>
                                <div class="col-8">
                                    <input type="text" id="keuntungan" value="{{ number_format($lelong['bakiupah'],2) }}" class="form-control" readonly/>
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="col-4">
                                    CAJ LELONG (RM)
                                </div>
                                <div class="col-8">
                                    <input type="text" id="cajlelong" value="{{ number_format($lelong['cajlelong'],2) }}" class="form-control" readonly/>
                                </div>
                            </div>

                            <div class="form-group row">
                                <div class="col-4">
                                    JUMLAH (RM)
                                </div>
                                <div class="col-8">
                                    <input type="text" id="jumlah" value="{{ number_format($lelong['baki'],2) }}" id="jumlah" class="form-control" readonly/>
                                </div>
                            </div>

                            

                        </div>

                        <div class="col-12 mt-3">
                            <input type="button" class="float-right btn btn-danger mr-2" value="BATAL" onclick="window.location.replace('{{  route('customer_info') }}')">
                            <input type="button" id="terima_transaksi" class="float-right btn btn-primary mr-2" value="TERIMA" onclick="bayarantuntubaki()">
                        </div>

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



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

<script>

    $(document).ready(function() {
        let jumlahbaki = parseFloat($('#jumlah').val().replace(/[^0-9.-]+/g,""));

        if(jumlahbaki > 0)
        {
            $("#radbakilelong").prop("checked", true);
            $("#radrugilelong").prop("checked", false);
        }else{
            $("#radbakilelong").prop("checked", false);
            $("#radrugilelong").prop("checked", true);
        }
    });

    function bayarantuntubaki()
    {
        const swalWithBootstrapButtons = Swal.mixin({
            customClass: {
                confirmButton: 'btn btn-success',
                cancelButton: 'btn btn-danger'
            },
            buttonsStyling: false
        });

        let jumlah = $('#jumlah').val();let hargajual = $('#hargajual').val();

        swalWithBootstrapButtons.fire({
            title: 'Dapatkan Pengesahan Khazanah',
            text: "Tuntutan Baki Pelanggan!",
            icon: 'info',
            showCancelButton: true,
            confirmButtonText: 'Hantar',
            cancelButtonText: 'Batal',
            reverseButtons: true
        }).then((result) => {
            if(result.isConfirmed) {
                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
                    })

                    let mohon = "Tuntut Baki Lelongan";

                    
                    let norujukan = $('#norujukan').val();
                    let api = "{{ config('api.url') }}";
                    let nowakil = 0;
                    let jenis = '';

                    if($("input[name='radlelong']:checked").val() == 'baki')
                    {
                        jenis = 'T';
                    }else{
                        jenis = 'B';
                    }

                    
                    let batchno = $('#batch').val();let teller = "{{ Auth::user()->name }}";let bakipjm = $('#pembiayaan').val().replace(/[^0-9.-]+/g,"");let bakiupah = $('#keuntungan').val();
                    let cajlain = 0.00; let cajlelong = $('#cajlelong').val();
                    let jumlah = $('#jumlah').val().replace(/[^0-9.-]+/g,"");let hargajual = $('#hargajual').val().replace(/[^0-9.-]+/g,"");

                        $.ajax({
                            method:'post',
                            url: '{{ route("teller.createkelulusan") }}',
                            data: {
                                "_token": "{{ csrf_token() }}",
                                "norujukan" : norujukan,
                                "mohon" : mohon
                            },
                            success:function(data){
                            },error: function(xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                alert(xhr.responseText);
                            }
                        }); 

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

                        var channel = pusher.subscribe('notice-ubahhapus');
                        channel.bind('my-ubahhapus', function(data) {


                            if(data.norujukan == norujukan)
                            {
                                if(data.kelulusan == 'lulus')
                                {
                                    Swal.fire({
                                        icon: 'success',
                                        title: 'Telah Disahkan'
                                    }).then(function(){

                                        $.ajax({
                                            method: 'put',
                                            url: "{{ route('lelong.tuntutbaki') }}",
                                            data: { 
                                            "_token": "{{ csrf_token() }}",
                                            'batchno' : batchno,
                                            'jenis' : jenis,
                                            'norujukan' : norujukan,
                                            'teller' : teller,
                                            'bakipjm' : bakipjm,
                                            'bakiupah' : bakiupah,
                                            'cajlain' : cajlain,
                                            'cajlelong' : cajlelong,
                                            'jumlah' : jumlah,
                                            'nowakil' : nowakil,
                                            'hargajual' : hargajual
                                            },
                                            success:function(data){
                                                let urlroute = '{{ route("resit.tuntutbaki", ":id") }}';
                                                urlroute = urlroute.replace(':id', norujukan);
                                                Swal.fire(
                                                        'Berjaya!',
                                                        'Tuntut Baki Berjaya',
                                                        'success'
                                                    ) .then(function() {
                                                        window.open(urlroute, '_blank');
                                                        window.location.replace("{{  route('customer_info') }}");
                                                    })
                                            },error: function(xhr, status, error) {
                                                        var err = eval("(" + xhr.responseText + ")");
                                                        alert(xhr.responseText);
                                                    }
                                        });
                                        return true;
                                    });
                                }
                                else if(data.kelulusan == 'batal')
                                {
                                    Swal.fire({
                                        icon: 'warning',
                                        title: 'Telah Dibatalkan',
                                        text: `Komen : ${data.komen}`
                                    }).then(function(){
                                        return false;
                                    });
                                }
                            }
                        });


            }else if (result.dismiss === Swal.DismissReason.cancel){
                swalWithBootstrapButtons.fire(
                    'Batal',
                    'Tidak Berjaya Mendaptkan Pengesahan Khazanah',
                    'error'
                )
            }
        });
    }

</script>
@endsection