// 获取表格数据
function getList(page){
	page = page || 1;
	var pageSize = 10, data = {
		"title" : $("#title").val(),
		"status" : $("#status option:selected").val(),
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
	$("#page, #list,#count").empty();
	$.post("ajax_notice_list.do", data, function(res){
		if (res.code==1 && res.list.length > 0){
			$.each(res.list, function(index, item){
				html += '<tr>'+
					'<td><input type="checkbox" class="checkbox {click:itemCheck}" name="chk" value="'+item.id+'" /><label></label></td>'+
					'<td>'+item.title+'</td>'+
					'<td>'+item.publisher+'</td>'+
    				'<td>'+(item.crtTime ? new Date(item.crtTime.time).Format("yyyy-MM-dd hh:mm:ss"):"")+'</td>'+
    				'<td>'+item.rank+'</td>'+
    				'<td>'+ (item.status == 2 ? '显示': '隐藏')+'</td>'+
					'<td class="edit">'+
						(update_status_flag == 1?(item.status==2 ? 
							'<a href="javascript:setStatus('+item.id+');">设为隐藏</a>&nbsp;&nbsp;' : 
							'<a href="javascript:setStatus('+item.id+');">设为显示</a>&nbsp;&nbsp;'
						):'') +
						(update_flag == 1 ? '<a href="javascript:platformModfiy(\'edit\',\'公告信息\','+item.id+');">编辑</a>&nbsp;&nbsp;':'') +
						(delete_flag == 1 ? '<a href="javascript:delRecord('+item.id+');">删除</a>':'') +
					'</td>'+
				'</tr>';
			});
			$("#list").html(html);
			$.checkbox();
			$("#count").text(res.count);
			$("#curPage").val(page);
			var newpage = new createPage(res.count, page, pageSize, 3);
			$("#page").html(newpage.pageHtml);
		} else {
			$("#list").info("暂无数据。");
		}
	});
}
pageGo = getList;

// 删除单个
function delRecord(id){
	confirm("确定删除该条记录吗？", function(){
        ajax("delete_notice.do", {"id" : id}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				top.getList(1);
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

// 删除多个
function delRecordBatch(){
	var arr = document.getElementsByName("chk");
	var ids = "";
	for (i=0; i<arr.length; i++){
		if (arr[i].checked){
			ids += ("," + arr[i].value);
		}
	}
	if (ids.isEmpty()){
		alert("请至少选择一条记录");
		return false;
	}
	confirm("确定删除这些记录吗？", function(){
        $.post("batch_del_for_notice.do", {"ids" : ids.substring(1,ids.length)}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				top.getList(1);
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

// 保存公告信息
function save(){
	// 得到数据
    var data = $('#dataForm').serializeJSON();
    data.id = $.getParam("id");
    data.content = editor.txt.html();
    data.status = $("select[name='status']>option:selected").val();
    console.log(data);
    // 数据是否完整
    if (data['title'].isEmpty()){
    	top.alert("请填写标题", 0, function(){
            $("input[name='title']").focus();
        });
        return false;
    }
    if (data['publisher'].isEmpty()){
    	top.alert("请填写发布人", 0, function(){
            $("input[name='publisher']").focus();
        });
        return false;
    }
    if (data['content'].isEmpty()){
    	top.alert("请填写公告内容", 0, function(){
            $("#content").html("");
        });
        return false;
    }
    // 提交数据
    top.ajax("save_notice_info.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功。", 1, function(){
            	top.getList(1);
            	top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });
}

// 弹窗
function platformModfiy(act, title, id){
	title = title || "";
	id = id || "";
	$.openWindow(act.decode("add","新增","edit","修改","")+title, "notice_info.do?id="+id, 580, 550, true);
}

function setStatus(id){
	confirm("确定更改状态吗？", function(){
        $.post("update_notice_status.do", {"id" : id}, function(res){
            if (res.code == 1){
    			alert("更改成功。", 1, function(){
    				top.getList($("#curPage").val());
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}
