let loadingMsg;
var form;
var beanArr = [];
var urls="";
var ysurls="";
var gtIds="";
var gtNames="";
layui.use(['upload','laydate','form'], function () {
    var upload = layui.upload;
    var element = layui.element;
    var $ = layui.$;
	form = layui.form;
	var laydate = layui.laydate;
	initProList();
	laydate.render({
	    elem: '#dataTime'
	  });
	
    // 制作多文件上传表格
    var uploadListIns = upload.render({
        elem: '#ID-upload-demo-files',
        elemList: $('#ID-upload-demo-files-list'), // 列表元素对象
        url: PATH_URL + "/makeInfo/upload", // 实际使用时改成您自己的上传接口即可。
        multiple: true, //是否允许多文件上传，默认未false
        dataType: "json",
        // exts: 'jpg|png|jpeg|txt|pdf|xlsx|xls|docx|doc|ppt|pptx|mp4|avi|flv',
        //'avi','mpg','wmv','3gp','mov','asf','asx','flv','mkv'
        exts: 'jpg|png',
        number: 10, //最大上传数量
        auto: false, //是否自动上传 ，默认为true
        // size: 1024 * 30, //最大文件大小，单位k
        bindAction: '#hideUpload',
        choose: function(obj){
            var that = this;
            var files = this.files = obj.pushFile(); // 将每次选择的文件追加到文件队列
            // 读取本地文件
            obj.preview(function(index, file, result){
                var tr = $(['<tr id="upload-'+ index +'">',
                    '<td>'+ file.name +'</td>',
                    '<td>'+ (file.size/1024).toFixed(1) +'kb</td>',
                    '<td><div class="layui-progress" lay-filter="progress-demo-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>',
                    '<td>',
                    '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>',
                    '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>',
                    '</td>',
                    '</tr>'].join(''));

                // 单个重传
                tr.find('.demo-reload').on('click', function(){
                    obj.upload(index, file);
                });
                // 删除
                tr.find('.demo-delete').on('click', function(){
                    //双击删除指定预上传图片
                    delete files[index]; // 删除对应的文件
                    tr.remove(); // 删除表格行
                    // 清空 input file 值，以免删除后出现同名文件不可选
                    uploadListIns.config.elem.next()[0].value = '';
                });
                that.elemList.append(tr);
                element.render('progress'); // 渲染新加的进度条组件
            });
        },
        before: function (obj) {
			 urls="";
			 ysurls="";
			 gtIds="";
			 gtNames="";
            this.data = {
               uploadType:"1",photoType:"hxy"
            };
        },
        done: function(res, index, upload){ // 成功的回调
            console.log("resresresres:", res);
            var json = {
                // "companyId": $("#companyId").val()
            }
            beanArr.push(json);

            var that = this;
            // if(res.code == 0){ // 上传成功
            var tr = that.elemList.find('tr#upload-'+ index)
            var tds = tr.children();
            tds.eq(3).html(''); // 清空操作

            delete this.files[index]; // 删除文件队列已经上传成功的文件
			
			urls=urls+res.data.url+",";
			ysurls=ysurls+res.data.ysUrl+",";
			gtIds=gtIds+res.data.gtId+",";
			gtNames=gtNames+res.data.gtName+",";
            return;
            this.error(index, upload);
			
        },
        allDone: function(obj){ // 多文件上传完毕后的状态回调
			insertBatch();
			var iframeWindow = parent.window['layui-layer-iframe' + localStorage.getItem("resultindex")];
			iframeWindow.reloadData();
			var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
			parent.layer.close(index);
        },
        error: function(index, upload){ // 错误回调
            var that = this;
            var tr = that.elemList.find('tr#upload-'+ index);
            var tds = tr.children();
            // 显示重传
            tds.eq(3).find('.demo-reload').removeClass('layui-hide');
        },
        progress: function(n, elem, e, index){ // 注意：index 参数为 layui 2.6.6 新增
            element.progress('progress-demo-'+ index, n + '%'); // 执行进度条。n 即为返回的进度百分比
        }
    });
	form.on('submit(formDemo)', function (data) {
		$("#hideUpload").trigger("click");
	});
});

function initProList(){
	$.ajax({//id获取详情打开弹窗传值子页面
	    type: 'post',
	    url: PATH_URL + "/makeInfo/getProList",
	    dataType: 'json',
		async:false,
	    data: {
			userId:localStorage.getItem("resultUserId")
	    },
	    success: function (res) {
			console.log(res,"getProList");
			 $("#proId").empty();
			 var html="<option value='' selected>请选择</option>";
			 for(var i=0;i<res.data.length;i++){
				 html+="<option value='"+res.data[i].proId+"'>"+ res.data[i].proName +"</option>";
			 }
			 $("#proId").append(html);
			form.render();
	    },
		error:function(e){
		}
	})
}

function insertBatch(){
	if(urls.length == 0){
		layer.msg('请上传文件', {icon: 2});
		return;
	}
	$.ajax({
	     type:"post",
	     url : PATH_URL + "/makeInfo/insertBatchMakeGt", 
		 async:false,
	     data:{
			"gtId": gtIds,
			gtName:gtNames,
			userId:localStorage.getItem("resultUserId"),
			makeMonth:localStorage.getItem("resultMakeMonth"),
			proId:localStorage.getItem("resultProId"),
			makeTime:$("#dataTime").val(),
			sFilePath:ysurls,
			filePath:urls,
			makeType:0,
			numTimes:$("#numTimes").val()
	     },
	     dataType:"json",
	     success:function(res){
				console.log(res,"res")
				if(res.code==500){
					parent.layer.msg(res.msg)
					return;
				}
	     },
	     error:function(data){
	     }
	 });
}

function reloading() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
    parent.layer.close(index); //再执行关闭
    // window.parent.location.reload();
}