@push('scripts')
    @include('lpj::assetsku.includenya')
    @include('lpj::otorisasipenawaran.js.editextjs')    
    <script type="module">    
    
    $(document).ready(function() {
        prepareForm();
    });
      
    function prepareForm() 
    {
        setData();
    }

    function setData() 
    { 
        let id = $("#id").val();
        let token =  "{{ csrf_token() }}";
        // alert('token = ' + token);
        var useURL = "{{ route('otorisasitender.penawaran.setData') }}";
        var input_data = new Object();
        input_data._token = token;
        input_data.id = id;

        $.ajax({
            url: useURL,
            type: "POST",
            data: input_data,
            dataType: "json",
            beforeSend: function() {
                // if ($("#myLoader").hasClass("pre-loader hidden")) {
                //     pleaseStartLoader();
                // }
            },
            success: function(response) {
                console.log(response);

                if("success"==response.status)
                {
                    var statusText = response.penawaran.status;
                    $("#textReg").text(response.penawaran.nomor_registrasi);
                    $("#textCodePenawaran").text(response.penawaran.code);
                    $("#textStatusPenawaran").text(statusText);
                    setTablesKJPP1(response.penawrandetails);
                    setTablesKJPPLog(response.penawarandetailLogs);
                }
                else
                {
                    toastrError(response.message);
                }
                
            },
            error: function(xhr) {
            },
            complete: function() {
            }
        });
    }
    
    function setTablesKJPP1(datas)
    {
        let i=1;    
        $.each(datas, function(key, value){

            var kjppName = value.kjpp_code+' - '+value.kjpp_name;
            var no_proposal = (value.no_proposal)??'';// alert(no_proposal);
            var tgl_proposal = (value.tgl_proposal)??'';// alert(tgl_proposal);
            var biaya_penawaran = value.biaya_penawaran;// alert(biaya_penawaran);
            var htmlDokumenPersetujuanDownload='';
            var dokumenPersetujuanDownload = value.dokumen_persetujuan;
            if(dokumenPersetujuanDownload)
            {
                htmlDokumenPersetujuanDownload='<div class="flex items-center justify-between flex-wrap my-2.5 gap-2"><a href="'+value.dokumen_persetujuan+'" class="badge badge-sm badge-outline" download="'+value.attachment+'">'+value.attachment+' &nbsp;&nbsp;<i class="ki-filled ki-cloud-download"></i></a></div>';
            }

            // pengecekan kondisi format number
            var biaya_penawaran_format = "";
            if(biaya_penawaran)
                biaya_penawaran_format=tandaPemisahTitik(biaya_penawaran);

            var markup = '<tr>';
            markup +='<td valign="top">'+i+'</td>';
            markup +='<td valign="top"><label id="{{$route[1]}}_kjppName_'+value.id+'">'+kjppName+'</label></td>';
            markup +='<td valign="top">'+no_proposal+'</td>';
            markup +='<td valign="top">'+tgl_proposal+'</td>';
            markup +='<td valign="top" align="right">Rp.'+biaya_penawaran_format+'</td>';
            markup +='<td><em id="{{$route[1]}}_dokumenPersetujuan_msg_'+value.id+'" class="alert text-danger text-sm"></em>'+htmlDokumenPersetujuanDownload+'</td>';
            markup +='<td><div class="flex flex-nowrap justify-center">';
                markup +='<a class="btn btn-sm btn-icon btn-clear btn-success" href="javascript:void(0)" id="{{$route[1]}}_icon_update_'+value.id+'" title="Otorisasi Penawaran '+kjppName+'" onclick="otorisasiKJPP('+value.penawaran_id+','+value.id+','+value.kjpp_rekanan_id+',\''+kjppName+'\',\''+biaya_penawaran+'\')"><i class="ki-outline ki-notepad-edit"></i></a>';
            markup += '</tr>';

            $('#tbodyKJPP1').append(markup);

            i++;
        });
    }
    
    $(document).on("input", "input:file", function(e) {
        let fileName = e.target.files[0].name;
        let inputFile = e.target.id;
        const myArray = inputFile.split("_");
        let penawaranID = myArray[myArray.length-1];
        let kjppName = $("#{{$route[1]}}_kjppName_"+penawaranID).text();
        let upld = fileName.split(".").pop();
        if(upld == "pdf" || upld =="PDF"){}
        else{
            removeErrorCssMsg();
            $("#{{$route[1]}}_dokumenPersetujuan_"+penawaranID).addClass(" border-danger");
            $("#{{$route[1]}}_dokumenPersetujuan_msg_"+penawaranID).text("Silahkan Masukan tipe file PDF Mas");
            $("#"+inputFile).val("");
        }
    });
    
    function setTablesKJPPLog(datas)
    {
        let i=1;    
        $.each(datas, function(key, value){

            var kjppName = value.kjpp_code+' - '+value.kjpp_name;
            var no_proposal = (value.no_proposal)??'';// alert(no_proposal);
            var tgl_proposal = (value.tgl_proposal)??'';// alert(tgl_proposal);
            var biaya_penawaran = value.biaya_penawaran;// alert(biaya_penawaran);
            var htmlDokumenPersetujuanDownload='';
            var dokumenPersetujuanDownload = value.dokumen_persetujuan;
            if(dokumenPersetujuanDownload)
            {
                htmlDokumenPersetujuanDownload='<div class="flex items-center justify-between flex-wrap my-2.5 gap-2"><a href="'+value.dokumen_persetujuan+'" class="badge badge-sm badge-outline" download="'+value.attachment+'">'+value.attachment+' &nbsp;&nbsp;<i class="ki-filled ki-cloud-download"></i></a></div>';
            }

            // pengecekan kondisi format number
            var biaya_penawaran_format = "";
            if(biaya_penawaran)
                biaya_penawaran_format=tandaPemisahTitik(biaya_penawaran);

            var markup = '<tr>';
            markup +='<td valign="top">'+i+'</td>';
            markup +='<td valign="top"><label id="{{$route[1]}}_kjppName_'+value.id+'">'+kjppName+'</label></td>';
            markup +='<td valign="top">'+no_proposal+'</td>';
            markup +='<td valign="top">'+tgl_proposal+'</td>';
            markup +='<td valign="top" align="right">Rp.'+biaya_penawaran_format+'</td>';
            markup +='<td><em id="{{$route[1]}}_dokumenPersetujuan_msg_'+value.id+'" class="alert text-danger text-sm"></em>'+htmlDokumenPersetujuanDownload+'</td>';
            markup +='<td valign="top"><label id="{{$route[1]}}_kjppName_'+value.id+'">'+value.created_at2+'</label></td>';

            $('#tbodyKJPPLog').append(markup);

            i++;
        });
    }
    
    // update status
    $("#{{$route[1]}}_toPenawaranUlang").click(function(e) {
        e.preventDefault();
        
        Swal.fire({
            title: ' ',
            text: "Yakin akan Penawaran ulang?",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes'
        }).then((result) => {
            if (result.isConfirmed) 
            {
                //define variable
                // $id ==> penawaran.id
                let token = "{{ csrf_token() }}";
                let noReg = $("#textReg").text();
                let useURL = "{{ route($route[0].'.'.$route[1].'.penawaranulang',$id) }}";
                
                var input_data = new Object();
                input_data._token = token;
                input_data.id = "{{ $id }}";
                input_data.noReg =noReg;
                // alert('url = ' + useURL);
                $.ajax({
                    url: useURL,
                    type: "PUT",
                    cache: false,
                    data: input_data,
                    dataType: "json",
                    success: function(response) {
                        console.log(response);
                        if('success' == response.status)
                        {
                            swal.fire('Sukses Penawaran ulang!', response.message.message_success[0], 'success').then(() => {
                                var url = "{{ route('otorisasitender.penawaran.index') }}";    
                                $(location).attr('href',url);
                            });
                        }
                        else
                        {
                            Swal.fire('Error!', response.message.message_error[0], 'error');
                        }
                        
                    },
                    error: function(response, textStatus, errorThrown) {
                        // var errors = response.responseJSON.errors;
                        // console.log(errors);
                        console.log(response);

                    }
                });  
            }
        })    
    });      
    

    </script>
@endpush  