@push('scripts')
    @include('lpj::assetsku.includenya')
    @include('lpj::prosespenawaran.js.editeulangxtjs')
    <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.prosespenawaran.setDataUlang') }}";
        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);
                    setKJPPList1(response.kjpps);
                }
                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 no_proposal = (value.no_proposal)??'';// alert(no_proposal);
            var tgl_proposal = (value.tgl_proposal)??'';// alert(tgl_proposal);
            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;<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>';
            // No Proposal
            markup +='<td valign="top">';
                markup +='<input type="hidden" id="{{$route[1]}}_penawarandetail_id_'+value.id+'" name="{{$route[1]}}_penawarandetail_id[]" value="'+value.id+'">';
                markup +='<input disabled="" type="text" value="'+no_proposal+'" class="inputku file-input" id="{{$route[1]}}_no_proposal_'+value.id+'" name="{{$route[1]}}_no_proposal[]">';
                markup +='<em id="{{$route[1]}}_no_proposal_msg_'+value.id+'" class="alert text-danger text-sm"></em>';
            markup +='</td>';
            // No Proposal
            // Tanggal Proposal
            markup +='<td valign="top">';
                markup +='<input disabled="" type="date" value="'+tgl_proposal+'" class="inputku file-input" id="{{$route[1]}}_tgl_proposal_'+value.id+'" name="{{$route[1]}}_tgl_proposal[]" >';
                markup +='<em id="{{$route[1]}}_tgl_proposal_msg_'+value.id+'" class="alert text-danger text-sm"></em>';
            markup +='</td>';
            // Tanggal Proposal

            // Biaya Penawaran
            markup +='<td valign="top">';
                markup +='<div class="input-group"><span class="inputku btn btn-input" id="{{$route[1]}}_rp_'+value.id+'">Rp.</span><input disabled="" type="text" 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[]"></div>';
                markup +='<em id="{{$route[1]}}_biayaPenawaran_msg_'+value.id+'" class="alert text-danger text-sm"></em>';
            markup +='</td>';
            // Biaya Penawaran
            // Upload Penawaran
            markup +='<td valign="top">';
                markup +='<input disabled="" type="file" class="inputku file-input" id="{{$route[1]}}_dokumenPersetujuan_'+value.id+'" name="{{$route[1]}}_dokumenPersetujuan[]" accept="application/pdf" />';
                markup +='<em id="{{$route[1]}}_dokumenPersetujuan_msg_'+value.id+'" class="alert text-danger text-sm"></em>'+htmlDokumenPersetujuanDownload;
            markup +='</td>';
            // Upload Penawaran
            markup +='<td valign="top"><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="updateDataUlang('+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="deleteDataUlang('+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>';
                markup +='</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("");
        }
    });

    function setKJPPList1(datas)
    {
        $('#{{$route[1]}}_kjpps1').empty().append('<option value="0"> - Pilih KJPP - </option>');

        $.each(datas, function(key, value){
            var stringVal = value.code+' - '+value.name;
            $('#{{$route[1]}}_kjpps1').append(new Option(stringVal, value.id));
        });
    }

     // 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('otorisasitender.penawaran.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('tender.prosespenawaran.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
