$(function(){


	fetchPost = function(){
		$.ajax({
			url: HttpRest+"advertpost/findAll.shtml", 
			type: "get", 
			dataType: "json",
			contentType:"application/json",
			success: function(data) { 
				if(data.code=='01'){
					var vhtml = template('postHtml', data);
					$("#postId").html(vhtml);
				}else{
					layer.msg('广告位查询失败',{icon: 5});
				}
			}
		});
	};

	fetchPost();

	/*initPost = function(){
		var _postId = $("#_postId").val();
		if(_postId!=''){
			$("#postId").val(_postId);
		}
	};

	initPost();*/

	$("#submitBtn").click(function(){
		var advId = $("#advId").val();
		var name = $("#name").val();
		var type = $("#type").val();
		var status = $("#status").val();
		var payAmt = $("#payAmt").val();
		var postId = $("#postId").val();

		/*var beginTime =$("#beginTime").val();
		var endTime =$("#endTime").val();*/
		var fpath =$("#fpath").val();

		if(advId=='' || advId=='0'){
			if(name==''||fpath==''){
				layer.msg('名称及图片路径不能为空！',{icon: 5});
				return;
			}
		}
		var params = {
				advId:advId,
				postId:postId,
				name:name,
				type:type,
				status:status,
				payAmt:payAmt,
				/*beginTime:beginTime,
				endTime:endTime,*/
				fpath:fpath
		};

		var _httpURL = "";
		if(advId=='' || advId=='0'){
			_httpURL = HttpRest+"advertment/create.shtml";
		}else{
			_httpURL = HttpRest+"advertment/save.shtml";
		}

		$.ajax({
			url: _httpURL, 
			type: "post", 
			dataType: "json",
			data:$.toJSON(params),   
			contentType:"application/json",
			success: function(data) { 
				if(data.code=='01'){
					layer.confirm('保存成功！是否关闭窗口?', function(index) {
						parent.grid.loadData();
						parent.layer.close(parent.pageii);
						return false;
					});
				}else{
					layer.msg('保存失败！',{icon: 5});
				}
			}
		});
	});

	onloadurl();
});