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

    function prepareForm()
    {
        $("#registrasifinal_form")[0].reset();
        // $("#{{ $route[0] }}_div_catatan").hide();
        // $("#{{ $route[0] }}_div_region").hide();
        // prepare data
        setData();
    }

    function setData()
    {
        // id => penawaran.id
        let id = $("#{{$route[0]}}_id").val();
        let token =  "{{ csrf_token() }}";
        // alert('token = ' + token);
        var useURL = "{{ route('registrasifinal.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) {
                
                if ('success' == response.status)
                {
                    $("#textReg").text(response.datas.nomor_registrasi);
                    $("#textCodePenawaran").text(response.penawaran.code);
                    $("#textStatusPenawaran").text(response.penawaran.status);
                    setRegionList(response.regions,0);
                   //  alert(response.datas.dokumen);
                    $("#pdfSPK").attr("href", response.datas.dokumen); 

                    // success
                    // var message = response.message;
                    // toastrku("success", message);
					
                }
                else if('error' == response.status)
                {
                    var message = response.message;
                    toastrku("error", message);
                }
            },
            error: function(xhr) {
            },
            complete: function() {
            }
        });
    }

    function setRegionList(datas, cid)
    {
        $('#{{$route[0]}}_region').empty().append('<option value="0"> - Pilih Region - </option>');

        $.each(datas, function(key, value){

            $('#{{$route[0]}}_region').append(new Option(value, key));
        });
    }

    $("#toEdit").click(function(e) {
        e.preventDefault();

        //define variable
        let token   = "{{ csrf_token() }}";

        let _method   = $('input[name=_method]').val();
         // id => penawaran.id
        let id   = $('#{{$route[0]}}_id').val();
        let region = $("#{{$route[0]}}_region").val();
        let catatan = $("#{{$route[0]}}_catatan").val();

        if(region==0)
            region='';

        var input_data = new Object();
        input_data._token= token;
        input_data._method= _method;
        input_data.id= id;
        input_data.region= region;
        input_data.catatan = catatan;

        let useURL= '{{ route($route[0].'.update', $id) }}';
        $.ajax({
            url: useURL,
            type: "PUT",
            cache: false,
            data: input_data,
            dataType: "json",
            beforeSend: function() {
            },
            success: function(response) {

                if ('error' == response.status) {

                    $.each(response.message, function(index, value) {

                        if ("catatan" === index) {
                            $("#{{$route[0]}}_catatan").addClass(" border-danger");
                            $("#{{$route[0]}}_catatan_msg").text(value);
                        }

                        if ("region" === index) {
                            $("#{{$route[0]}}_region").addClass(" border-danger");
                            $("#{{$route[0]}}_region_msg").text(value);
                        }
                    });
                }
                else
                {
                    //toastr.success(response.message);
                    var message = response.message;
                    toastrku("success", message);

                    setTimeout(function () {						
                        var url = "{{ route('registrasifinal.index') }}";
                        $(location).attr('href',url);
                        // window.location.href = "https://www.newurl.com";
                    }, 2000);
                }

            },
            error: function(response, textStatus, errorThrown) {
                // var errors = response.responseJSON.errors;
                // console.log(errors);
                console.log(response);

            },
            complete: function() {
                // pleaseStopLoader();
            }

        });
        //

    });

   
    </script>
@endpush
