$(function () {
    setTimeout(fileToUploads,1000);
    function fileToUploads() {
        var $fileToUploads = $("#fileToUploads"),
            $uploaderFiles = $("#uploaderFiles");
        $fileToUploads.on("change", function (e) {
            console.log('000000file')
            files = e.target.files;
            // 如果没有选中文件，直接返回
            if (files.length === 0) {
                return;
            }
            for (var i = 0, len = files.length; i < len; ++i) {
                var file = files[i];
                var imgID = genGUID();
                var reader = new FileReader();
                var fileType = file.type;
                // console.log('file.type',file.type)
                // // 如果类型不在允许的类型范围内
                // if (allowTypes.indexOf(file.type) === -1) {
                //     console.log(allowTypes.indexOf(file.type)) ;
                //     $.toast('该类型不允许上传' + fileType);
                //     continue;
                // }
                var base64;
                reader.onload = function (e) {
                    $.showLoading("上传中");
                    // 插入到预览区
                    // $uploaderFiles.find('.upload_imgs').before($(tmpl.replace('#url#', base64).replace('#ImgID#', imgID)));
                    // var num = $('.weui-uploader__file').length;
                    // $('#uploadCount').text(num);
                    // 模拟上传进度
                    // var progress = 0;
                    // function uploading() {
                    //     $uploaderFiles.find('.weui_uploader_status_content').text(++progress + '%');
                    //     if (progress < 100) {
                    //         setTimeout(uploading, 30);
                    //     } else {
                    //         $uploaderFiles.removeClass('weui_uploader_status').find('.weui_uploader_status_content').remove();//清除上传进度图标
                    //     }
                    // }
                    // setTimeout(uploading, 30);
                    var FormDatas = new FormData();
                    FormDatas.append("file", file);
                    //这里实现上传
                    $.ajax({
                        url: uploadUrl,
                        type: 'POST',
                        data: FormDatas,
                        contentType: false,
                        processData: false,
                        success: function (res) {
                            if (res.code === "200") {
                                if (res.data.fileExt == 'zip' || res.data.fileExt == 'rar') {
                                    var str = `
                                        <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
                                           <img src="image/zip.png" alt="">
                                           <span>${res.data.fileName}</span>
                                           <i class="close_s delete_file weui-icon-cancel"></i>
                                         </li>
                                      `;
                                    $('.fileList .lastUpload').before(str);
                                } else if (res.data.fileExt == 'pdf') {
                                    var str = `
                                        <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
                                           <img src="image/pdf.png" alt="">
                                           <span>${res.data.fileName}</span>
                                           <i class="close_s delete_file weui-icon-cancel"></i>
                                         </li>
                                      `;
                                    $('.fileList .lastUpload').before(str);
                                } else if (res.data.fileExt == 'pptx' || res.data.fileExt == 'ppt') {
                                    var str = `
                                        <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
                                           <img src="image/ppt.png" alt="">
                                           <span>${res.data.fileName}</span>
                                           <i class="close_s delete_file weui-icon-cancel"></i>
                                 </li>
                              `;
                                    $('.fileList .lastUpload').before(str);
                                } else if (res.data.fileExt == 'doc') {
                                    var str = `
                                        <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
                                           <img src="image/doc.png" alt="">
                                           <span>${res.data.fileName}</span>
                                           <i class="close_s delete_file weui-icon-cancel"></i>
                                       </li>
                                  `;
                                    $('.fileList .lastUpload').before(str);
                                } else if (res.data.fileExt == 'xlsx') {
                                    var str = `
                                        <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
                                           <img src="image/lsx.png" alt="">
                                           <span>${res.data.fileName}</span>
                                           <i class="close_s delete_file weui-icon-cancel"></i>
                                       </li>
                                 `;
                                    $('.fileList .lastUpload').before(str);
                                } else if (res.data.fileExt == 'png' || res.data.fileExt == 'jpg' || res.data.fileExt == 'jpeg' || res.data.fileExt == 'JPG' || res.data.fileExt == 'JPEG' || res.data.fileExt == 'PNG') {
                                    var str = `
                                        <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
                                           <img src="${res.data.fullURL}" alt="">
                                           <i class="close_s delete_img weui-icon-cancel"></i>
                                       </li>
                                 `;
                                    $('.fileList .lastUploadimg').before(str);
                                } else if (res.data.fileExt) {

                                }
                                $.toast('上传成功');
                                $.hideLoading();
                            } else {
                                $.toast('上传失败，请检查网络','forbidden');
                                $.hideLoading();
                            }
                        },
                        error:function () {
                            $.toast("上传失败,请检查网络！",'forbidden');
                            $.hideLoading();
                        }
                    })

                };
                reader.readAsDataURL(file);
            }
        });
        function genGUID() {
            var G1 = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            var G2 = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            return (G1 + G2);
        }
    }
});