let loadingMsg;
var form;
var beanArr = [];

layui.use(['upload','laydate','form'], function () {
    var upload = layui.upload;
    var element = layui.element;
    var $ = layui.$;
	var form = layui.form;
	var laydate = layui.laydate;
	
	laydate.render({
	    elem: '#dataTime'
	  });
	
    // 制作多文件上传表格
    var uploadListIns = upload.render({
        elem: '#ID-upload-demo-files',
        elemList: $('#ID-upload-demo-files-list'), // 列表元素对象
        url: "", // 实际使用时改成您自己的上传接口即可。
        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: '#ID-upload-demo-files-action',
        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) {
            this.data = {
                photoType: "trainBank"
            };
        },
        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]; // 删除文件队列已经上传成功的文件
            return;
            this.error(index, upload);
        },
        allDone: function(obj){ // 多文件上传完毕后的状态回调
			top.layer.close(loadingMsg);
			top.layer.msg("上传成功", { icon: 1 });
            reloading();

        },
        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 即为返回的进度百分比
        }
    });
});


function addFile() {
    loadingMsg = layer.msg('数据请求中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});
	
	var json = {
		"proId": $("#proId").val(),
		"dataTime": $("#dataTime").val()
	}
	
    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: '', // 请求地址
        async : true,
        data: JSON.stringify(json), //获取提交的表单字段
        success: function (data) {
            if (data.code == 200) {
				$("#ID-upload-demo-files-action").trigger("click");
            } else {
                layer.alert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, e) {
            top.layer.close(loadingMsg);
            layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false});
        }
    });
}

function setData(data) {
    $("#companyId").val(data.companyId);
    $("#proId").val(data.proId);
    $("#month").val(data.month);
}


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