@push('scripts')
    @include('lpj::assetsku.includenya')
    <script type="module">

    $(document).ready(function() {
        prepareForm();
    });

    function prepareForm()
    {
        $("#registrasi_form")[0].reset();
        $(":radio[name='{{ $route[0] }}_tindakan'][value='0']").attr('checked', 'checked');
        $("#{{ $route[0] }}_div_jenis_pilihan").show();
        $("#{{ $route[0] }}_div_catatan").hide();
        $("#{{ $route[0] }}_div_region").hide();
        $("#{{ $route[0] }}_div_catatan2").show();
        // prepare data
        setData();
    }

    function setData()
    {
        let id = $("#id").val();
        let token =  "{{ csrf_token() }}";
        // alert('token = ' + token);
        var useURL = "{{ route('registrasi.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);
                    setJenisPenilaianList(response.jenisPenilaians,0);
                    setRegionList(response.regions,0);

                    // 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));
        });
    }

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

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

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

    $('input[type=radio][name={{ $route[0] }}_tindakan]').change(function() {

        removeErrorCssMsg();
        $("#{{ $route[0] }}_div_region").hide();
        if($(this).val()==0)
        {
            // show jenis pilihan
            // hide catatan
            $("#{{ $route[0] }}_div_jenis_pilihan").show();
            $("#{{ $route[0] }}_catatan").val('');
            $("#{{ $route[0] }}_div_catatan").hide();

            $("#{{ $route[0] }}_div_catatan2").show();
        }
        else
        {
            $("#{{$route[0]}}_jenis_penilaian option[value=0]").prop('selected', true);
            $("#{{ $route[0] }}_div_jenis_pilihan").hide();
            $("#{{ $route[0] }}_div_catatan").show();
            $("#{{ $route[0] }}_div_catatan2").hide();
        }
    });

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

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

        let _method   = $('input[name=_method]').val();
        let id   = $('#{{$route[0]}}_id').val();
        let tindakan = $('input[name="{{$route[0]}}_tindakan"]:checked').val();
        let jenis_penilaian = $("#{{$route[0]}}_jenis_penilaian").val();
        let region = $("#{{$route[0]}}_region").val();
        let catatan = $("#{{$route[0]}}_catatan").val();
        let catatan2 = $("#{{$route[0]}}_catatan2").val();
        let jenis_laporan = $("#jenis_laporan").val();

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

        var input_data = new Object();
        input_data._token= token;
        input_data._method= _method;
        input_data.id= id;
        input_data.tindakan= tindakan;
        input_data.jenis_penilaian= jenis_penilaian;
        input_data.region= region;
        input_data.catatan = catatan;
        input_data.catatan2 = catatan2;
        input_data.jenis_laporan = jenis_laporan;

        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 ("jenis_penilaian" === index) {
                            $("#{{$route[0]}}_jenis_penilaian").addClass(" border-danger");
                            $("#{{$route[0]}}_jenis_penilaian_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('registrasi.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();
            }

        });
        //

    });

    $('#{{$route[0]}}_jenis_penilaian').on('change', function (e) {
        var jenis_penilaian_id = $(this).val();

        if (jenis_penilaian_id === '1') {
            // INTERNAL, show region
            $("#{{ $route[0] }}_div_region").show();
            $("#jenis_laporan").val('sederhana');
        } else {
            // For all other cases, hide region
            $("#{{ $route[0] }}_div_region").hide();
            $("#jenis_laporan").val('standar');
        }
    });

    </script>
@endpush
