@push('scripts')
    @include('lpj::assetsku.includenya')
    @include('lpj::prosespenawaranulang.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('tender.prosespenawaranulang.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;
                    $("#namaDebitur").text(response.penawaran.permohonan.debiture.name);
                    $("#textReg").text(response.penawaran.nomor_registrasi);
                    $("#textCodePenawaran").text(response.penawaran.code);
                    $("#textStatusPenawaran").text(statusText);
                    setTablesKJPP1(response.penawrandetails);
                }
                else
                {
                    // toastr.error(response.message);
                    var message = response.message;
					toastrku("error", 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 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+'<i class="ki-filled ki-cloud-download"></i></a></div>';
            }

            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"><div class="input-group"><span class="inputku btn btn-input" id="{{$route[1]}}_rp_'+value.id+'">Rp.</span><input type="text" disabled="" style="text-align: right;" onkeydown="return numbersonly(this, event);" onkeyup="javascript:tandaPemisahTitik(this);" class="inputku input" id="{{$route[1]}}_biayaPenawaran_'+value.id+'" name="{{$route[1]}}_biayaPenawaran_'+value.id+'"></div><em id="{{$route[1]}}_biayaPenawaran_msg_'+value.id+'" class="alert text-danger text-sm"></em></td>';
            markup +='<td><input type="file" disabled="" class="inputku file-input" id="{{$route[1]}}_dokumenPersetujuan_'+value.id+'" name="{{$route[1]}}_dokumenPersetujuan_'+value.id+'" accept="application/pdf" /><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 disabled="" class="btn btn-sm btn-icon btn-clear btn-info" href="javascript:void(0)" id="{{$route[1]}}_icon_update_'+value.id+'" title="Proses Penawaran '+kjppName+'" onclick="updateData('+value.id+','+value.kjpp_rekanan_id+',\''+kjppName+'\')"><i class="ki-outline ki-bookmark"></i></a>';
                markup +='<a disabled="" class="delete btn btn-sm btn-icon btn-clear btn-danger" id="{{$route[1]}}_icon_delete_'+value.id+'" onclick="deleteData('+value.id+',\''+kjppName+'\')" title="Hapus Proses Penawaran '+kjppName+'"><i class="ki-outline ki-trash"></i></a>';
                markup +='<label class="switch"><input name="{{$route[1]}}_check_'+value.id+'" id="{{$route[1]}}_check_'+value.id+'" onclick="switchProses('+value.id+')" type="checkbox" value="0"/><span class="switch-label">Proses</span></label></div></td>';
            markup += '</tr>';

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

            // pengecekan kondisi format number
            var biaya_penawaran_format = "";
            if(biaya_penawaran)
                biaya_penawaran_format=tandaPemisahTitik(biaya_penawaran);
            $("#{{$route[1]}}_biayaPenawaran_"+value.id).val(biaya_penawaran_format);
            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("");
        }
    });
    </script>
@endpush
