require([
    'jquery',
    'prototype',
], function(jQuery){
    function importpreselect() {
        var csv = jQuery('#preselectcsvfile');
        var csvFile = csv[0].files[0];
        var ext = csv.val().split(".").pop().toLowerCase();
        jQuery("#preselectcsvfile-error").css("display","none");
        
        if(ext!=""){
            if(ext=="csv"){

                var fileelement = jQuery("#preselectcsvfile");//element not element.id
                var form = fileelement.closest('form');
                var data = new FormData(jQuery("#"+form.attr("id"))[0]);

                jQuery.ajax({
                    showLoader: true,
                    url: jQuery("#importurl").val(),
                    data: data,
                    processData: false,
                    contentType: false,
                    type: "POST",
                    dataType: "json"
                }).done(function (data) {
                    jQuery.each(data, function(index, element) {
                        if(index=="success"){
                            jQuery(".preselectmessage").html(element);
                            jQuery(".preselectmessage").removeClass("error");
                            jQuery(".preselectmessage").addClass("success");
                            jQuery(".preselecterrortable").html("");
                        }
                        else if(index=="multierror"){
                            if(element.length>1){
                                var html="<table>";

                                jQuery.each(element, function(key,value) {
                                    
                                    html = html+"<tr>";

                                    jQuery.each(value, function(columnkey,columnvalue) {
                                        if(key==0){
                                            html = html+"<th>"+columnvalue+"</th>";
                                        }
                                        else{
                                            html = html+"<td>"+columnvalue+"</td>";
                                        }
                                    });

                                    html = html+"</tr>";
                                });

                                html = html+"</table>";

                                jQuery(".preselecterrortable").html(html);
                            }
                        }
                        else{
                            jQuery(".preselectmessage").html(element);
                            jQuery(".preselecterrortable").html("");
                            jQuery(".preselectmessage").removeClass("success");
                            jQuery(".preselectmessage").addClass("error");
                        }
                    });
                });
            }
            else{
                jQuery("#preselectcsvfile-error").text("Invalid file format");
                jQuery("#preselectcsvfile-error").css("display","block");
            }

            return false;
        }
        else{
            jQuery("#preselectcsvfile-error").text("This is a required field.");
            jQuery("#preselectcsvfile-error").css("display","block");
            return false;
        }
    }
 
    jQuery('#importbtn').click(function () {
        return importpreselect();
    });
});