//dom加载完成后执行的js
;$(function(){

	//ajax get请求
    $('.ajax-get').click(function(){
		var url = $(this).attr('href');

		$.get(url).success(function(data) {
			if (data.status == 1) {
				if (data.url) {
					layer.msg(data.info, {
						icon : 1,
						time : 1500
					});
				} else {
					layer.msg(data.info, {
						icon : 1,
						time : 1500
					});
				}
				setTimeout(function() {
					if (data.url) {
						location.href = data.url;
					} else {
						location.reload();
					}
				}, 1500);
			} else {
				layer.msg(data.info, {
					icon : 2,
					time : 1500
				});
			}
		});


		return false;
    });

    //ajax get请求 显示确认框
    $('.ajax-get-confirm').click(function(){
		var url = $(this).attr('href');
		var title = "Confirm this operation？";
		layer.confirm(title, {
			btn : [ 'comfirm', 'cancel' ]
		// 按钮
		}, function() {
			$.get(url).success(function(data) {
				if (data.status == 1) {
					if (data.url) {
						layer.msg(data.info, {
							icon : 1,
							time : 1500
						});
					} else {
						layer.msg(data.info, {
							icon : 1,
							time : 1500
						});
					}
					setTimeout(function() {
						if (data.url) {
							location.href = data.url;
						} else {
							location.reload();
						}
					}, 1500);
				} else {
					layer.msg(data.info, {
						icon : 2,
						time : 1500
					});
				}
			});

		});

		return false;
    });


 // ajax 表单提交
	$(".ajax-post").click(function(){
		var target,query,form,url;
        target = $(this).attr('target-form');
     	form = $('#'+target);// 获取表单
     	form.Validform({	 // 表单验证
			tiptype:2,
			callback : function(form) {
				url = form.attr('action');
				query = form.serialize();
				$.post(url, query).success(function(data) {

					if (data.status == 1) {
						if (data.url) {
							layer.msg(data.info, {
								icon : 1,
								time : 1500
							});
						} else {
							layer.msg(data.info, {
								icon : 1,
								time : 1500
							});
						}
						setTimeout(function() {
							var index = parent.layer.getFrameIndex(window.name);
							if (data.url) {
								if (index > 0)
									parent.location.href = data.url;
								else
									location.href = data.url;
							} else {
								if (index > 0)
									parent.location.reload();
								else
									location.reload();
							}
						}, 1500);
					} else {
						layer.msg(data.info, {
							icon : 2,
							time : 1500
						});
					}
				});
				return false;
			}
		});

	});



    // 批量删除
	$(".ajax-doSelect").click(function(event){
		var ids = "";
		var url = $(this).attr('href');
		var title = "Confirm this operation？";
		layer.confirm(title, {
			btn : [ 'comfirm', 'cancel' ]
		// 按钮
		}, function() {
			$("input.ids").each(function() {
				// 获取选中值
				if ($(this).prop('checked')) {
					ids += $(this).val() + ",";
				}
			});
			if (ids == "") {
				layer.msg("Sorry, you haven't selected the record yet！", {
					'icon' : 5
				});
				return false;
			}
			ids = ids.substring(0, ids.length - 1);
			// ajax 提交 return json(status )
			$.get(url, {
				'ids' : ids
			}).success(function(data) {
				if (data.status == 1) {
					if (data.url) {
						parent.layer.msg(data.info, {
							icon : 1,
							time : 1500
						});
					} else {
						parent.layer.msg(data.info, {
							icon : 1,
							time : 1500
						});
					}
					setTimeout(function() {
						if (data.url) {
							location.href = data.url;
						} else {
							location.reload();
						}
					}, 1500);
				} else {
					layer.msg(data.info, {
						icon : 2,
						time : 1500
					});
				}
			});
		});
		return false;
	});


	//弹出层
	$("a[target='dialog']").click(function(event){
		var href = $(this).attr("href"),
			width = $(this).attr("width"),
			height = $(this).attr("height"),
			title =  $(this).attr("title"),
			 isFull = $(this).attr("isFull");
		if(width == "" || width == null) width = '500px';
		if(height == "" || height == null) height = '500px';
		if(title == "" || title == null) title =  $(this).html();

		var index = layer.open({
		    type: 2,
		    title: title,
		    area: [width, height],
		    //skin: 'layui-layer-rim', //加上边框
		    content: [href, '']
		});
		if (isFull == "1") {
            layer.full(index);
        }
		return false;
	});


	$(document).delegate('.upload_images', 'mouseenter', function() {
        $(this).find(".upload_images_del").show(500);
    });
    $(document).delegate('.upload_images', 'mouseleave', function() {
         $(this).find(".upload_images_del").hide(500);

    });
     $(document).delegate('.upload_images_del', 'click', function() {
        if(confirm('Sure to delete it？')){
           var id = $(this).attr('ids');
           var idsObj = $(this).closest(".formControls").find("input[type=hidden]");
           var ids = ","+idsObj.val();
           ids = ids.replace(","+id,'');
           if (ids.substr(0,1)==',') ids=ids.substr(1);
           idsObj.val(ids);
           $(this).parent().hide(500);
        }

    });


});

/* 上传图片预览弹出层 */


