/**
 * 上传资源
 */

$(document).ready(function() {
    getMangType();
	$("#upload").click(function(){
		var name=$("#name").val();
		if(name==''){
			layer.tips('资源名称不能为空',
					'#name', {
						tips : [ 1, 'red' ]
					//还可配置颜色
					});
		}else{
			var i;
			var uploadEventFile=$("#inputfile").val();
            /**
             * xuyang
             * 前端对所选文件的大小做个简单判断
             */
            var uploadFile = document.getElementById("inputfile").files;
            //文件大小
            var size = uploadFile[0].size;
            if (Math.floor(size/1024) <= 3072){
                if(uploadEventFile == ''){
                    layer.msg("请选择文件，再上传！",{icon:5,time:2000},function(){
                        layer.closeAll()});
                }else{
                    var description=$("#description").val();
                    if(description==''){
                        layer.tips('资源描述不能为空',
                            '#description', {
                                tips : [ 1, 'red' ]
                                //还可配置颜色
                            });
                    }else{
                        var type = $("#type").val();
                        if(type==0){
                            layer.tips('请选择资源类目',
                                '#type', {
                                    tips : [ 1, 'red' ]
                                    //还可配置颜色
                                });
                        }else {
                            var formData = new FormData($('#uploadForm')[0]);
                            formData.append("name",name);
                            formData.append("type",type);
                            formData.append("description",description);
                            $.ajax({
                                method : "post",
                                url : "/pqs9900/manage/uploadManageList",
                                data :formData,
                                beforeSend: function () {
                                    i = ityzl_SHOW_LOAD_LAYER();
                                },
                                success : function(data) {
                                    if(data.code==200){
                                        layer.msg(data.message,{icon:1,time:2000},function(){
                                            parent.load();
                                            parent.layer.closeAll()});
                                    }
                                    else {
                                        layer.msg(data.message,{icon:5,time:2000},function(){
                                            parent.layer.closeAll()});
                                    }
                                },
                                cache: false,
                                contentType: false,
                                processData: false
                            });
                            //***********关闭loading
                            ityzl_CLOSE_LOAD_LAYER(i);
                        }
                    }
                }
            }else {
                layer.msg("上传失败,文件大小超过3M!",{icon:5,time:2000},function(){
                    layer.closeAll()});
            }
		}
	})
	
});
function getMangType(){
    $.ajax({
        method: "post",
        url: "/pqs9900/dic/getMangType",
        data: "",
        success: function (data) {
            $.each(data, function (i, item) {
                var option = $("<option value='"
                    + item.dicIndex + "'>"
                    + item.dicName + "</option>");
                $("#type").append(option);
            })
        }
    })
}
$("#close").click(function () {
    parent.layer.closeAll();
})