<div class="sg-upload-image-container-sg" id="sg-{{ $name }}-sg">
    <input id="sg-{{ $name }}-input-sg" class="layui-input" name="data[{{ $name }}]" readonly value="{{ $value }}" placeholder="选择文件上传">
    <div class="sg-upload-hint-sg">文件小于2M,支持xls,pdf,doc/docx,xls/xlsx格式<span></span></div>
</div>

<script type="text/javascript">
    $(function () {
        var formContainerHtml;
        formContainerHtml = '<div class="sg-upload-image-input-sg" id="sg-{{ $name }}-form-container-sg">\
                                    <form id="sg-{{ $name }}-form-sg" method="POST" enctype="multipart/form-data">\
                                        <input id="sg-{{ $name }}-form-file-sg" type="file"  name="files[]" multiple="multiple">\
                                        <input type="text" name="class" value="{{ isset($class) ? $class : "未分类" }}">\
                                    </form>\
                                 </div>';
        $('body').append(formContainerHtml);
        var id = '#sg-{{ $name }}-sg';
        var input = '#sg-{{ $name }}-input-sg';
        var form = '#sg-{{ $name }}-form-sg';
        var file_input = '#sg-{{ $name }}-form-file-sg';


        $(input).on('click', function () {
            $(file_input).trigger("click");
        });

        $(file_input).change(function () {
            var formData = new FormData($(form)[0]);
            var files = $(file_input)[0].files;
            if(files.length !== 0) {
                var maxSize = files[0].size;
                for(var i = 0; i < files.length; ++i) {
                    maxSize = Math.max(maxSize, files[i].size);
                }
                if(maxSize < 200 * 1024 * 1024) {
                    $(id + ' .sg-upload-hint-sg span').text('上传中...');
                    $.ajax({
                        url: '/uploadFile/uploadFiles',
                        method: 'POST',
                        processData: false,
                        contentType: false,
                        cache: false,
                        headers: {
                            'X-CSRF-TOKEN': "{{ csrf_token() }}"
                        },
                        data: formData,
                        success: function (data) {
                            if (data.status === 'success') {
                                $(input).val(data['fileList'].join(','));
                                $(id + ' .sg-upload-hint-sg span').text('上传成功');
                            } else {
                                $(id + ' .sg-upload-hint-sg span').text('上传失败');
                            }
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            $(id + ' .sg-upload-hint-sg span').text('上传失败');
                        },
                        complete: function () {
                            $(id + ' .sg-upload-image-btn-sg').removeClass('sg-uploading');
                        }
                    });
                }
            }
        });
    });
</script>