//全局的页面遮盖
let loadingMsg;
var startWorkFiles = {};
var upload;
var id;
var formData;
var gtId;
var month;
var numTimes;
var userId;
var makeType;
var proId;
layui.use(['form', 'table', 'upload'], function () {
    let form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
    form.render();
    upload = layui.upload;
	gtId=$("#gtId").val();
	month=$("#month").val();
	numTimes=$("#numTimes").val();
	userId=$("#userId").val();
	makeType=$("#makeType").val();
	proId=$("#proId").val();
    uploadFile();

    form.on('submit(formDemo)', function (data) {
		$("#hideUpload").trigger("click");
    });
});
function setGtId(gtId,month,numTimes,resultUserId,makeType,proId){
	console.log(gtId,"gtId");
	console.log(month,"month");
	$("#gtId").val(gtId);
	$("#month").val(month);
	$("#numTimes").val(numTimes);
	$("#userId").val(resultUserId);
	$("#makeType").val(makeType);
	$("#proId").val(proId);
}
var ysUrls="";
var urls="";
function uploadFile(){
    var uploadcom = upload.render({
        elem: '#test',
        url: PATH_URL + "/makeInfo/upload", //改成您自己的上传接口
        multiple: true,   //是否允许多文件上传，默认未false
        dataType: "json",
        exts: 'jpg|png',
        //acceptMime: 'image/jpg,image/png,image/jpeg,file/pdf,file/xlsx',
        auto: false,  //是否自动上传 ，默认为true
        number: 9,  //最大上传数量
        // size: 1024*5, //最大文件大小，单位k
        bindAction: '#hideUpload', //绑定的按钮
        field: 'file', //传到后台的字段名,默认file
        choose: function (obj) {
            flag = false;
            var suffixType = ''; //后缀类型
            uploadcom.config.elem.next()[0].value = '';
            obj.files = obj.pushFile();
            startWorkFiles = obj.pushFile();
            obj.preview(function (index, file, result) {
                console.log(index); //得到文件索引
                console.log(file.name); //得到文件对象
                var files = $('#demo img')
                if(files.length <= 2){
                    $('#demo').append(
                        '<div id="prove_' + index + '" style="display: inline-block">' +
                        '<div class="item">' +
                        '<div class="handle" style="margin-top: -28px" data-idx="prove_' + index + '">' +
                        '<i class="layui-icon layui-icon-delete" style="position: relative;top: 30px;left: 72px; font-size: 20px;color: red"  data-idx="prove_' + index + '" ></i>' +
                        '</div>' +
                        '<img src="' + result + '" alt="' + file.name + '" title="' + file.name + '" data-index="prove_' + index + '" data-name="' + file.name + '" style="height: 100px;width: 100px;margin-right: 10px" class="layui-upload-img ">' +
                        '<div><span>' + (file.name.length > 8 ? (file.name.substr(0, 8) + "..") : file.name) + '</span></div>' +
                        '</div>'+
                        '</div>'
                    );
                }else{
                    layer.msg('最多上传2张照片',{icon:2})
                }
                addDelete(startWorkFiles);
            });
        },
        before: function (obj) {
			 ysUrls="";
			 urls="";
            this.data = {
                "gtId": gtId,uploadType:"one",photoType:"hxy",month:month,numTimes:numTimes
            };
        },
        done: function (data) {
			console.log(data,"donedata");
			urls=data.data.url;
			ysUrls=data.data.ysUrl;
        },
        allDone: function (data) {
			console.log(data,"allDonedata");
			if(ysUrls!=null&&urls!=null){
				insertMakeGt();
				var iframeWindow = parent.window['layui-layer-iframe' + localStorage.getItem("resultindex")];
				iframeWindow.reloadData();
				var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
				parent.layer.close(index);
			}else{
				layer.msg("上传错误！")
			}
			
        },
        error: function (e) {
            //请求异常回调
            console.log(e)
        }
    });
}
function insertMakeGt(){
	if(ysUrls.length == 0){
		layer.msg('请上传文件', {icon: 2});
		return;
	}
	$.ajax({
	     type:"post",
	     url : PATH_URL + "/makeInfo/insertMakeGt", 
		 async:false,
	     data:{
			"gtId": gtId,
			userId:userId,
			makeMonth:month,
			sFilePath:ysUrls,
			filePath:urls,makeType:makeType,proId:proId,
			numTimes:numTimes
	     },
	     dataType:"json",
	     success:function(res){
				console.log(res,"res")
				if(res.code==500){
					parent.layer.msg(res.msg)
					return;
				}
	     },
	     error:function(data){
	     }
	 });
}




var deletePhoto = [];
function delPhoto(i,id) {
    var parentcolor = document.getElementById("demo" + i);
    var childcolor = document.getElementById(id + "");
    var path = childcolor.name;
    deletePhoto.push(path);
    parentcolor.removeChild(childcolor);
    var photoNum = $("#demo" + i).children().length;
    if (photoNum == 0) {
        $("#preview" + i).css("display", "none");
    }
}
function addDelete(arr) {
    $('#demo').click(function (e) {
        var target = $(e.target)
        if (target.hasClass('handle') || target.hasClass('layui-icon')) {
            var dataIdx = target.attr('data-idx')
            var filePath = $('#' + dataIdx).children().children().next().attr('file')
            delete arr[dataIdx]; // 通过获取绑定在标签上的index，删除files对象中对应的key属性即可
            target.parents('.item').remove() // 页面文件标签也删除
        }
    });
}
//设置弹窗关闭
function reloading() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
    parent.layer.close(index); //再执行关闭
    window.parent.location.reload();
}


