//查询
function getList(page){
	page = page || 1;	
	var pageSize = 10, data = $("#tvFrom").serializeJSON(), html = "";
	data['tvPlatform'] = $("#tv").val();
	data['projectId'] =  $("#projectId").val();
	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_info_list.do",data,function(res){
		  if(res.code == 1 && res.data.length > 0){
			  $.each(res.data, function(index, item){
      			html += '<tr>'+
      		        '<td>'+item.tvPlatform.toNull()+'</td>'+
						'<td>'+item.periodName.toNull() +'</td>'+
						'<td>'+item.periodTime.toNull() +'</td>'+
						'<td>'+item.adVersion.toNull() +'</td>'+
						'<td>'+ item.adLength+'</td>'+
						'<td>'+ item.adPosition +'</td>'+
						'<td>'+ item.month+'</td>'+
						'<td>'+ '<a href="javascript:void(0);" onclick="showNumber(\''+(item.month+'-'+item.periodName.toNull())+'\','+item.id+')"><span class="orange">' + item.count + '</span></a>' +'</td>'+
						'<td>'+ item.price + '</td>'+
						'<td>'+ item.rebate + '</td>'+
						'<td>'+ item.extraPrice + '</td>'+
						'<td>'+ item.equity + '</td>'+
						'<td>'+ item.totalEquity + '</td>'+
						'<td class="edit">'+
							'<a href="javascript:projectInfoModfiy(\'edit\',\'电视台下单\','+item.id+');">编辑</a>&nbsp;&nbsp;' +
							'<a href="javascript:delProjectInfo('+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);
            $("#curPage").val(page);
            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 showNumber(month, id){
	$.openWindow(month+" 广告明细","project_ad_detail.do?id="+id, 790, 540, true);
}

function editProject(){
	var data = $("#projectDetailFrom").serializeJSON();
	
	ajax("save_project.do", data, function(res){
		if(res.code == 1){
			top.alert("修改成功", 1, function(){
				getList(1);
			})
		}
	})
}

//导入表格数据
function importExcel(obj){
	var url = "import_excel_for_project.do";
	$("#excelInfo").attr("action",url).submit();
}

function excelCallback(code, msg, projectId) {
    if (code == 1) {
    	alert("导入成功。", 1, function(){
    		location.href = "resource_project_info.do?projectId=" + projectId;
		});    	
    } else {
    	alert(msg, 0, function(){
    		location.reload();
    	});	  	
    }
}

//确认覆盖
function confirmCallback(repeat, msg) {
    if (repeat == 1){
        confirm(msg, function(){
        	  $("#isSure").val("sure");
        	  var url = "import_excel_for_project.do";
        	  $("#excelInfo").attr("action",url).submit();
        });
    }
}

function delTvPlatform(){
	var data = $("#tvFrom").serializeJSON();
	data['tvPlatform'] = $("#tv").val();
	data['projectId'] =  $("#projectId").val();
	
	confirm("您确定需要删除该电视台记录吗？",function(){
		top.ajax("del_tv_platform.do",data,function(res){
			if(res.code == 1){
				alert("电视台删除成功。", 1, function(){
					location.href = "resource_project_info.do?projectId=" + data.projectId; 
				});
			}else{
				alert("删除失败");
			}
		});
	});
}

// 弹窗
function projectInfoModfiy(act, title, id){
    var projectId =  $("#projectId").val() || "";
    title = title || "";
    id = id || "";
    $.openWindow(act.decode("add","新增","edit","修改","")+title, "open_project_info.do?id="+id+"&projectId="+projectId+"&curPage="+$("#curPage").val(), 900, 540, true, null, true);
}

// 新增修改
function saveProjectDetailInfo() {
    var data = $('#infoForm').serializeJSON();
    data.id = $.getParam("id");
    data.projectId = $.getParam("projectId");
    data.curPage = $.getParam("curPage");
    var name = document.getElementsByName("day_");
    for (var i in name) {
        if (!isNaN(parseInt(i))) {
            data["day_" + (parseInt(i) + 1)] = name[i].value;
        }
    }
    // 必填验证
    if (data['tvPlatform'].isEmpty()){
        top.alert("请输入电视台名称", 0, function(){
            $("input[name='tvPlatform']").focus();
        });
        return ;
    }
    if (data['periodName'].isEmpty()){
        top.alert("请输入时段名称", 0, function(){
            $("input[name='periodName']").focus();
        });
        return ;
    }
    if (data['month'].isEmpty()){
        top.alert("请输入月份", 0, function(){
            $("input[name='month']").focus();
        });
        return ;
    }
    if (!isNaN(data['count'])){
        if (parseInt(data['count']) < 0 || parseInt(data['count']) > 99999.999){
            top.alert("数据范围错误！累计次数应为 0~99999");
            return ;
        }
    }else if(isNaN(data['count'])){
        top.alert("请输入累计次数");
        return ;
    }
    if (!isNaN(data['price'])){
        if (parseInt(data['price']) < 0 || parseInt(data['price']) > 99999.999){
            top.alert("数据范围错误！刊例价应为 0~99999");
            return ;
        }
    }
    if (!isNaN(data['rebate'])){
        if (parseInt(data['rebate']) < 0 || parseInt(data['rebate']) > 99.999){
            top.alert("数据范围错误！折扣应为 0~100");
            return ;
        }
    }
    if (!isNaN(data['extraPrice'])){
        if (parseInt(data['extraPrice']) < 0 || parseInt(data['extraPrice']) > 99999.999){
            top.alert("数据范围错误！加价应为 0~99999");
            return ;
        }
    }
    if (!isNaN(data['equity'])){
        if (parseInt(data['equity']) < 0 || parseInt(data['equity']) > 99999.999){
            top.alert("数据范围错误！净价应为 0~99999");
            return ;
        }
    }
    if (!isNaN(data['totalEquity'])){
        if (parseInt(data['totalEquity']) < 0 || parseInt(data['totalEquity']) > 99999.999){
            top.alert("数据范围错误！总净价应为 0~99999");
            return ;
        }
    }
    top.ajax("save_project_info.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功。", 1, function(){
                if(res.curPage==1){
                    top.getList(1);
                }else {
                    top.getList($.getParam("curPage"));
                }
                top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });
}

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