@extends('layouts.app_callcenter')

@section('content')
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-md-12 col-lg-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"><a href="{{ route('arcc.notisperingatankedua') }}">Notis Peringatan Kedua</a></li>
                      <li class="breadcrumb-item"><a href="{{ route('arcc.rekodnotisperingatankedua') }}">Rekod Notis Peringatan Kedua</a></li>
                      <li class="breadcrumb-item active" aria-current="page">Mengikut Cawangan</li>
                      <li class="breadcrumb-item active" aria-current="page">{{$cawangan_info['details_caw']}}</li>
                      <li class="breadcrumb-item active" aria-current="page">Notis JBG</li>
                </ol>
            </nav>
            <form id="kemaskiniform" action="" target="_blank" method="POST">
                            @csrf
                            <input type="hidden" name="type" id="typedonwload" value="">
                            <input type='hidden' name="radaktif" value="aktif">
                            <input type='hidden' name="notisid" id="notisid" value="{{$notisid}}">
                            <input type='hidden' name="kodcaw" id="kodcaw" value="{{$kodcaw}}">
                            <input type='hidden' name="nokp" id="nokp" value="{{$nokp}}">
            <div class="form-group row col-md-12">
                <div class="col-md-6">
                    <div class="form-group row">
                        <div class="col-md-4">
                            <b>Nama</b>
                        </div>
                        <div class="col-md-8">
                            <input type="text" class="form-control" name="name" id="name" readonly value="{{ $bysagnotisjbgCustomer['nama'] }}">
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group row">
                        <div class="col-md-4">
                            <b>No Kad Pengenalan</b>
                        </div>
                        <div class="col-md-8">
                            @php
                            if($bysagnotisjbgCustomer['kpbaru'] != null){
                            $noic = $bysagnotisjbgCustomer['kpbaru'];
                            }else{
                            $noic = $bysagnotisjbgCustomer['kplama'];
                            }
                            @endphp
                            <input type="text" class="form-control" name="noic" id="noic" readonly value="{{ $noic }}">
                        </div>
                    </div>
                </div>
                <div class="col-md-6">

                    <div class="form-group row">
                        <div class="col-md-4">
                            <b>No Telefon</b>
                        </div>
                        <div class="col-md-8">
                            <input type="text" class="form-control" name="telefon" id="telefon" readonly value="{{ $bysagnotisjbgCustomer['telefon'] ? $bysagnotisjbgCustomer['telefon'] : '' }}">
                        </div>
                    </div>   
                    
                    <div class="form-group row">
                        <div class="col-md-4">
                            <b>Status Panggilan</b>
                        </div>
                        <div class="col-md-7">
                        <select name="statuscall" class="form-control" id="statuscall">

                            @foreach ($bysagnotisjbg['getstatusremark'] as $index => $remark) 
                                @if ($remark['statuscall'] == 0)
                                    <option value="0" selected>Belum Hubungi</option>
                                    <option value="1">Berjaya Hubungi</option>
                                    <option value="9">Tidak Dapat Hubungi</option>
                                @elseif($remark['statuscall'] == 1)
                                    <option value="0" >Belum Hubungi</option>
                                    <option value="1" selected>Berjaya Hubungi</option>
                                    <option value="9">Tidak Dapat Hubungi</option>
                                @else
                                    <option value="0" >Belum Hubungi</option>
                                    <option value="1">Berjaya Hubungi</option>
                                    <option value="9" selected>Tidak Dapat Hubungi</option>
                                @endif
                            @endforeach
                        </select>
                        
                        
                        </div>
                    </div>

                </div>

                <div class="col-md-6">

                    <div class="form-group row">
                        <div class="col-md-4">
                            <b>Alamat </b>
                        </div>
                        <div class="col-md-8">
                            <input type="text" class="form-control" name="alamat" id="alamat" readonly value="{{$bysagnotisjbgCustomer['alamat1'] }}">
                        </div>
                    </div>

                    <div class="form-group row">
                        <div class="col-md-4">
                            <b>Nota</b>
                        </div>
                        <div class="col-md-8">
                            @foreach($bysagnotisjbg['getstatusremark'] as $index => $remark)
                            <textarea type="text" class="form-control" name="remark" id="remark">{{ $remark['remark1'] }}</textarea>
                            @endforeach
                        </div>
                    </div>

                </div>               
            </div>

            <div class="card">
                <div class="card-body">
                    <div class="container-fluid">
                        
                            <table class="table table-striped table-hover" id="details">
                                <thead>
                                    <th>Bil</th>
                                    <!-- <th>Status</th> -->
                                    <th>Norujukan</th>
                                    <th>Tempoh</th>
                                    <th>Baki Pembiayaan (RM)</th>
                                    <th>Keuntungan 6Bulan (RM)</th>
                                    <th>Amaun Telah Bayar (RM)</th>
                                    <th>Keuntungan Semasa (RM)</th>
                                    <th>Tarikh Matang</th>
                                    <th>Tarikh Tamat Kontrak</th>
                                    <th>Tarikh Panggilan</th>
                                    <th>Nota</th>

                                </thead>
                                @php
                                $counter = 1;
                                $totalpinjaman = 0;
                                @endphp
                                @foreach ($bysagnotisjbg['rekodnotis'] as $index => $item)
                                @php
                                $totalpinjaman += $item['listsag'][0]['pinjaman'];

                                if ($item['listnotis'][0]['tarikhcall1'] == null) {
                                $status = 'Belum';
                                } else {
                                $status = 'Selesai';
                                }
                                @endphp

                                <tr>
                                    <td>@foreach($item['listsag'] as $item1){{ $loop->iteration }}<br>@endforeach</td>
                                    <!-- <td style="white-space: nowrap">{{ $status ?? '-' }}</td> -->
                                    <td style="white-space: nowrap;">@foreach($item['listsag'] as $item1){{ $item1['norujukan']}}<br>@endforeach</td>
                                    <td>@foreach($item['listsag'] as $item1){{ $item1['jbg']}}<br>@endforeach</td>
                                    <td align='right' style="white-space: nowrap;">@foreach($item['listsag'] as $item1){{number_format($item1['bakipjm'], 2, '.', ',')}}<br>@endforeach</td>
                                    <td align='right' style="white-space: nowrap;">@foreach($item['listsag'] as $item1){{number_format($item1['bakiupah'], 2, '.', ',')}}<br>@endforeach</td>
                                    <td align='right' style="white-space: nowrap;">@foreach($item['listsag'] as $item1){{number_format($item1['hargajualan']-$item1['bakihargajualan'], 2, '.', ',')}}<br>@endforeach</td>
                                    <td align='right' style="white-space: nowrap;">@foreach($item['listsag'] as $item1)
                                    @if($item1['tagbaru'] == '0')
                                    {{number_format($item1['keuntungan'], 2, '.', ',')}}
                                    @elseif($item1['tagbaru'] == '1')
                                    {{number_format($item1['keuntungan']['ujrah']+$item1['keuntungan']['onepercent'], 2, '.', ',')}}
                                    @endif
                                    <br>@endforeach<br></td>
                                    <td align='right' style="white-space: nowrap;"><b> @foreach($item['listsag'] as $item1)
                                            @if($item1['t_matang'] == null)

                                            @else
                                            @php
                                            $myDateTime = DateTime::createFromFormat('Y-m-d', $item1['t_matang']);
                                            $formattedtmatang = $myDateTime->format('d-m-Y');
                                            echo($formattedtmatang);
                                            @endphp
                                            @endif
                                            <br>
                                            @endforeach
                                            <br>
                                        </b></td>
                                    <td align='right' style="white-space: nowrap;"><b> @foreach($item['listsag'] as $item1)
                                            @if($item1['t_matang1'] == null)

                                            @else
                                            @php
                                            $myDateTime = DateTime::createFromFormat('Y-m-d', $item1['t_matang1']);
                                            $formattedtmatang1 = $myDateTime->format('d-m-Y');
                                            echo($formattedtmatang1);
                                            @endphp
                                            @endif
                                            <br>
                                            @endforeach
                                            <br>
                                        </b></td>
                                    <td align='right' style="white-space: nowrap;"><b> @foreach($item['listnotis'] as $item1)
                                            @if($item1['tarikhcall1'] == null)

                                            @else
                                            @php
                                            $myDateTime = DateTime::createFromFormat('Y-m-d', $item1['tarikhcall1']);
                                            $formattedweddingdate = $myDateTime->format('d-m-Y');
                                            echo($formattedweddingdate);
                                            @endphp
                                            @endif
                                            <br>
                                            @endforeach
                                            <br>
                                        </b></td>
                                    <td align="right">@foreach($item['listnotis'] as $item1){{ $item1['remark1']}}<br>@endforeach</td>

                                </tr>
                                @php $counter++; @endphp
                                @endforeach
                            </table>                      
                        </div>
                    </div>
                </div>
            </form>
            <div class="mr-5 form-group float-right">
                    <button type="button" class="btn btn-primary" onclick="kemaskinihubungi()" id="btnsave">Kemaskini</button>
            </div>    
        </div>
    </div>
</div>



<script>
    var api_url = "{{ $api_url }}";
    // var kodcaw = "{{ $kodcaw }}";
    // var notisid = "{{ $notisid }}";
    //var nokp = "{{ $noic }}";
    var staf = "{{ Auth::user()->name }}";

$("#kemaskiniform").submit(function(event) {
        event.preventDefault();

        var nokp = $('#nokp').val();
        var telefon = $('#telefon').val();
        var nota = $('#remark').val();
        var nama = $('#nama').val();
        var alamat = $('#alamat').val();
        var notisid = $('#notisid').val();
        var kodcaw = $('#kodcaw').val();
        var statuscall = $('#statuscall').val();
        let caller = "{{ Auth::user()->name }}";

        $.ajax({
            method:"put",
            url: api_url + "/arcc/updatecall/"+ kodcaw + "/" + notisid + "/" + nokp,
            data:{
                
                'nota':nota,
                'statuscall':statuscall,
                'caller':caller
                // 'notisid':notisid,
                // 'kodcaw':kodcaw,
                // 'nokp':nokp
            },
            success: function(success){
                var urlroute = "{{ route('arcc.bycawnotisjbg2', ['kodcaw' => ':kodcaw', 'notisid' => ':notisid']) }}";
                        urlroute = urlroute.replace(':kodcaw', kodcaw);
                        urlroute = urlroute.replace(':notisid', notisid);
                
                if(success){
                    Swal.fire({
                        icon: 'success',
                        title: 'Berjaya',
                        text: 'Berjaya mengemaskini maklumat notis JBG'
                    }).then((result) => {
                        if (result.isConfirmed) {
                            //location.reload();
                            window.location.href = urlroute;
                        }
                    });
                     
                }else{
                    Swal.fire({
                        icon: 'error',
                        title: 'Tidak Berjaya',
                        text: 'Tidak berjaya mengemaskini maklumat notis JBG',
                        allowEscapeKey : false,
                        allowOutsideClick: false,
                    });
                }
            },
            error: function (request, status, error) {
                alert(request.responseText);
            }

        });
    });

    function kemaskinihubungi(){
        
        $('#telefon').attr('readonly', true);
        $('#telefon2').attr('readonly', true);
        // $('#nota').attr('readonly', true);
        $('#nama').attr('readonly', true);
        $('#alamat').attr('readonly', true);

        $('#kemaskiniform').trigger('submit');    
    }    
</script>
@endsection