var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		id : 'paging',
		l_column : [ {
			colkey : "eventId",
			width : "100px",
			hide : true
		}, {
			colkey : "name",
			name : "活动名称"
		}
		, {
			colkey : "createTime",
			name : "发布时间",
			/*renderData : function( rowindex ,data, rowdata, colkeyn) {
				return new Date(rowdata.spendTime).format("yyyy-MM-dd hh:mm:ss");
			}*/
		}, {
			colkey : "status",
			name : "状态",
			renderData : function( rowindex ,data, rowdata, colkeyn) {
				if(rowdata.status=='0'){
					return "未审核";
				}else if(rowdata.status=='1'){
					return "进行中";
				}else if(rowdata.status=='2'){
					return "已结束";
				}else{
					return "-";
				}
			}
		}, {
			colkey : "hits",
			name : "点击量",
		}, {
			colkey : "carnum",
			name : "关注度",
		}, {
			colkey : "signable",
			name : "是否接受报名",
			renderData : function( rowindex ,data, rowdata, colkeyn) {
				if(rowdata.signable=='1'){
					return "是";
				}else{
					return "否";
				}
			}
		}, {
			colkey : "signnum",
			name : "报名人数",
		}],
		jsonUrl : rootPath + '/event/find.shtml',
		checkbox : true,
		serNumber : false,
		checkValue:'eventId'
	});

	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addBtn").click("click", function() {
		createEvent();
	});
	$("#editBtn").click("click", function() {
		updateEvent();
	});
	$("#delBtn").click("click", function() {
		deleteEvent();
	});

	$("#playerBtn").click(function(){
		var cbox = grid.getSelectedCheckbox();
		if (cbox.length > 1 || cbox == "") {
			layer.msg("只能选中一个");
			return;
		}
		pageii = layer.open({
			title : "活动选手",
			type : 2,
			area : [ "860", "70%" ],
			content : rootPath + '/eventplayer/list.shtml?id=' + cbox
		});
	});

	$("#signBtn").click(function(){
		var cbox = grid.getSelectedCheckbox();
		if (cbox.length > 1 || cbox == "") {
			layer.msg("请选择一个活动");
			return;
		}
		pageii = layer.open({
			title : "预约顾客",
			type : 2,
			area : [ "860", "70%" ],
			content : rootPath + '/eventplayer/sign.shtml?id=' + cbox
		});
	});
});

function updateEvent() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("至少选择一项");
		return;
	}
	if(cbox.length > 1){
		layer.msg("只能选择一项");
		return;
	}
	
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : [ "860px", "75%" ],
		content : rootPath + '/event/editUI.shtml?id=' + cbox
	});
}
function createEvent() {
	pageii = layer.open({
		title : "添加",
		type : 2,
		area : [ "860px", "75%" ],
		content : rootPath + '/event/addUI.shtml'
	});
}
function deleteEvent() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('确认删除吗？', function(index) {
		var _URL = rootPath + '/event/delete.shtml';
		var resp = CommnUtil.ajax(_URL, {
			ids : cbox.join(",")
		}, "json");
		layer.msg(resp.code,{icon: 5});
		if (resp.code == "01") {
			layer.msg(resp.msg,{icon: 1});
			grid.loadData();
		} else {
			layer.msg('删除失败',{icon: 5});
		}
	});
}