//查询
function getList(page){
	page = page || 1;	
	var pageSize = 10, data = $("#projectForm").serializeJSON(), html = "";
	data['pageSize'] = pageSize;
	data['pageNo'] = page;

  $("#list").empty();
  $("#count").text(0);
  $("#totalCount").text(0);
  $("#totalPrice").text(0);
  $("#extraPrice").text(0);
  $("#equity").text(0);
  $("#totalEquity").text(0);
  ajax("get_project_list.do",data,function(res){
		  if(res.code == 1 && res.data.length > 0){
			  $.each(res.data, function(index, item){
      			html += '<tr>'+
      		        '<td>'+item.projectName.toNull()+'</td>'+
                      '<td>'+item.medium.toNull() +'</td>'+
                      '<td>'+item.businessNo.toNull() +'</td>'+
                      '<td>'+item.salesman.toNull() +'</td>'+
                      '<td>'+ item.tvNumber +'</td>'+
                      '<td>'+ item.totalCount+'</td>'+
                      '<td>'+ item.totalPrice +'</td>'+ 
                      '<td>'+ item.extraPrice+'</td>'+ 
                      '<td>'+ item.equity +'</td>'+                     
                      '<td>'+ item.totalEquity + '</td>'+  
                      '<td>' + (item.uploadTime ? new Date(item.uploadTime.time).Format("yyyy-MM-dd hh:mm:ss"):"")  + '</td>' + 
                  	'<td class="edit"> '+
                  	(update_flag == 1 ? '<a href="resource_project_info.do?projectId=' + item.id +'">编辑</a>&nbsp;&nbsp;' :'') +
                  	(delete_flag == 1 ? '<a href="javascript:delProject('+ item.id  + ')">删除</a>' :'') +
                  	'</td>' +
      			'</tr>';
      		});
			$("#list").html(html);
			$("#count").text(res.total.count);
			$("#totalCount").text(res.total.totalCount);
			$("#totalPrice").text(res.total.price);
			$("#extraPrice").text(res.total.extraPrice);
			$("#equity").text(res.total.equity);
			$("#totalEquity").text(res.total.totalEquity);
			var newpage = new createPage(res.total.count, page, pageSize, 3);
			$("#page").html(newpage.pageHtml);
			$(".table").hoverTable(true);
			$.checkbox();
		  }else{
			  $("#list").info("暂无信息。");
		  }
  });
}

pageGo = function(page){
	getList(page);
}
function addProject(){
	top.$.openWindow("新增电视台下单", "resource_project_add.do", 600, 300, true);
}

function saveProject(){
	var data  = $('#addProjectFrom').serializeJSON();
	if(data['projectName'].isEmpty()){
		top.alert("请输入项目名称");
		$("input[name='projectName']").focus();
		return false;
	}
	if(data['salesman'].isEmpty()){
		top.alert("请输入业务员");
		$("input[name='salesman']").focus();
		return false;
	}
	if(data['businessNo'].isEmpty()){
		top.alert("请输入单号");
		$("input[name='businessNo']").focus();
		return false;
	}
	if(data['medium'].isEmpty()){
		top.alert("请输入媒介");
		$("input[name='medium']").focus();
		return false;
	}
	if(data['mark'].isEmpty()){
		top.alert("请输入备注");
		$("input[name='mark']").focus();
		return false;
	}
	
	ajax("save_project.do",data,function(res){
		if(res.code == 1){
			top.alert("保存成功。", 1 , function(){
				top.getList(1);
				top.$.closeWindow();
			});
		}else{
			top.aler("保存失败");
		}
	})	
}

function delProject(projectId){
	confirm("您确定需要删除该项目记录吗？",function(){
		top.ajax("del_project.do",{"projectId":projectId},function(res){
			if(res.code == 1){
				alert("项目记录删除成功。", 1, function(){
					getList(1);
				});
			}else{
				alert("删除失败");
			}
		});
	});
}

