// 获取表格数据
function getLongList(page){
	page = page || 1;
	var pageSize = 10, data = {
		"name" : $("#longName").val(),
		"networkTv":$("#networkTv").val(),
		"sellStatus":$("#sellStatus").val(),
		"showTime":$("#showTime").val(),
        "showInterval" : $("#showInterval").val(),
        "customer" : $("#customer").val(),
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
		
	$("#longPage, #longList,#longCount").empty();
	ajax("ajax_resource_long_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.networkTv.toNull()+'</td>'+
					'<td class="edit"><a href="to_resource_by_long_info.do?id='+item.id+'" target="_blank">'+item.name.toString().decode("","-",item.name)+'</a></td>'+
					'<td>'+item.type.toString().decode("1","电视剧","2","综艺","3","栏目","4","其他","-")+'</td>'+
					'<td>'+retIp(item.ip)+'</td>'+
					'<td>'+retForm(item.form)+'</td>'+
					'<td>'+item.showTime.toString().decode("","-",item.showTime)+'</td>'+
					'<td>'+item.category.toString().decode("","-",item.category)+'</td>'+
					'<td>'+item.sellMode.toString().decode("","-",item.sellMode)+'</td>'+
					'<td>'+item.price.toString().decode("","-",item.price)+'</td>'+
					'<td>'+item.discount.toString().decode("0","-",item.discount+"%")+'</td>'+
					'<td>'+item.showInterval.toString().decode("","-",item.showInterval)+'</td>'+
					'<td>'+item.sellStatus.toString().decode("0","在售", "1","已售","")+'</td>'+
					'<td>'+item.customer.toString().decode("","-",item.customer)+'</td>'+
					'<td>'+item.remarks.toString().decode("","-",item.remarks)+'</td>'+
					'<td class="edit">'+
    					(update_flag == 1 ? '<a href="javascript:platformModfiy(\'edit\',\'长视频资源信息\','+item.id+');">编辑</a>&nbsp;&nbsp;' : '') +
    					(delete_flag == 1 ? '<a href="javascript:delRecord('+item.id+');">删除</a>' : '' ) +
    					(score_flag == 1&& 1 == item.type? '<a href="javascript:platformModfiyFormScore(\''+item.name+'\');">'+item.scoreId.toString().decode("0","评价", "已评")+'</a>': '') +
					'</td>'+
				'</tr>';
			});
			$("#longList").html(html);
			$.checkbox();
			$("#longCount").text(res.count);
			$("#curLongPage").val(page);
			var newpage = new createPage(res.count, page, pageSize, 3);
			$("#longPage").html(newpage.pageHtml);
		} else {
			$("#longList").info("暂无数据。");
		}
	});
}
pageGo = function(page){
	resetCheckAll();
	getLongList(page);
}
//ip
function retIp(ip){
	return ip == undefined ? "-" : ip.toString().replace(/1/,"版权").replace(/2/,"自制").replace(/3/,"合作").replace(/0/,"-");
}

//广告形式
function retForm(form){
	return form == undefined ? "-" : form.toString().replace(/1/,"节目冠名").replace(/2/,"特约播映").replace(/3/,"互动支持").replace(/4/,"合作伙伴").replace(/5/,"指定产品").replace(/6/,"指定用车").replace(/7/,"中插").replace(/8/,"创可贴").replace(/9/,"贴片").replace(/10/,"其他").replace(/\,/g,"、").replace(/''/,"-");
}
// 删除单个
function delRecord(id){
	confirm("确定删除该条记录吗？", function(){
        ajax("delete_resource_long.do", {"id" : id}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
                    window.location.reload();
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

// 导入表格数据
function importLongExcel(obj){
    $("#longFileForm").submit();
}

// 导入结果
function excelCallback(code, msg) {
    if (code == 1) {
        // 导入成功
    	alert("导入成功。", 1, function(){
    		location.reload();
		});
    } else {
    	// 导入失败
    	alert(msg, 0, function(){
        	location.reload();
		});
    }
}

// 保存长视频资源信息
function saveLong(){
	// 得到数据
    var data = $('#infoForm').serializeJSON();
    console.log(data);
    data.id = $.getParam("id");
    // 数据是否完整
    if (data['networkTv'].isEmpty()){
    	top.alert("请输入播出频道", 0, function(){
            $("input[name='networkTv']").focus();
        });
        return;
    }
    if (data['name'].isEmpty()){
    	top.alert("请输入资源名称", 0, function(){
            $("input[name='name']").focus();
        });
        return;
    }
    if(!document.getElementById("type1").checked&&
       !document.getElementById("type2").checked&&
       !document.getElementById("type3").checked&&
       !document.getElementById("type4").checked){
    	top.alert("请选择资源类型")
    	return;
    }
    if(!document.getElementById("ip1").checked&&
    	       !document.getElementById("ip2").checked&&
    	       !document.getElementById("ip3").checked){
    	top.alert("请选择IP归属")
    	return;
    }
    if(!document.getElementById("form1").checked&&
    	       !document.getElementById("form2").checked&&
    	       !document.getElementById("form3").checked&&
    	       !document.getElementById("form4").checked&&
    	       !document.getElementById("form5").checked&&
    	       !document.getElementById("form6").checked&&
    	       !document.getElementById("form7").checked&&
    	       !document.getElementById("form8").checked&&
    	       !document.getElementById("form9").checked&&
    	       !document.getElementById("form10").checked){
    	top.alert("请选择广告形式")
    	return;
    }
    if (data['showTime'].isEmpty()){
    	top.alert("请输入时间", 0, function(){
            $("input[name='showTime']").focus();
        });
        return;
    }
    if (data['sellMode'].isEmpty()){
    	top.alert("请输入售卖方式", 0, function(){
            $("input[name='sellMode']").focus();
        });
        return;
    }
    if (data['price'].isEmpty()){
    	top.alert("请输入刊例价格", 0, function(){
            $("input[name='price']").focus();
        });
        return;
    }
    if(isNaN(data['price'])){
    	top.alert("刊例价铬请输入数字");
	 	return;
    }
    if (!isNaN(data['price'])){
    	if (parseInt(data['price']) < 0 || parseInt(data['price']) > 99999999.99){
    		top.alert("数据范围错误！刊例价格应为 0~99999999.99");
    		return;
    	}
    }
    if(!document.getElementById("sellStatus1").checked&&
 	       !document.getElementById("sellStatus2").checked){
	 	top.alert("请选择售卖状态")
	 	return;
    }
    
    if(!data['discount'].isEmpty()){
    	if(isNaN(data['discount'])){
    		top.alert("折扣请输入数字");
    		return;
    	}
    }
    // 提交数据
    top.ajax("save_resource_long_info.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功", 1, function(){
            	if(res.curPage==1){
            		top.getLongList(1);
            	}else {
            		top.getLongList($.getParam("curPage"));
            	}
            	top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });
}

// 弹窗
function platformModfiy(act, title, id){
	title = title || "";
	id = id || "";
	$.openWindow(act.decode("add","新增","edit","修改","")+title, "ad_resources_long_info.do?id="+id+"&curPage="+$("#curLongPage").val(), 500, 470, true, null, true);
}


//数据验证
function dataVerificationFormScore(value, text, maxScore){
	if (value == null || value === ""){
		top.alert("请给"+ text +"打分。");
		return false;
	}
	console.log(text+":"+value);
	if (isNaN(value)){
		top.alert("数据类型错误！"+ text +"的评分只能是数字，且为 0~"+ maxScore);
		return false;
	}
	if (0 > parseInt(value) || parseInt(value) > maxScore){
		top.alert("数据范围错误！"+ text +"的评分应为 0~"+ maxScore);
		return false;
	}
	return true;
}

//保存资源评价
function saveScore(id,resourceName){
	// 得到数据
    var data = $('#scoreForm').serializeJSON();
    data.id = id || "";
    data.resourceName = resourceName;
    // 数据验证
    if(!dataVerificationFormScore(data['mIp'], 'IP', 12)){
    	return;
    }
    if(!dataVerificationFormScore(data['mTheme'], '题材', 5)){
    	return;
    }
    if(!dataVerificationFormScore(data['mEpisode'], '集数', 3)){
    	return;
    }
    if(!dataVerificationFormScore(data['mSchedule'], '档期', 4)){
    	return;
    }
    if(!dataVerificationFormScore(data['mPlatform'], '平台', 15)){
    	return;
    }
    if(!dataVerificationFormScore(data['mFlow'], '预估流量', 10)){
    	return;
    }
    if(!dataVerificationFormScore(data['pActor'], '演员', 15)){
    	return;
    }
    if(!dataVerificationFormScore(data['pDirector'], '导演', 8)){
    	return;
    }
    if(!dataVerificationFormScore(data['pScriptwritter'], '编剧', 7)){
    	return;
    }
    if(!dataVerificationFormScore(data['pProduced'], '出品公司', 5)){
    	return;
    }
    if(!dataVerificationFormScore(data['pIssue'], '发行公司', 10)){
    	return;
    }
    if(!dataVerificationFormScore(data['sSimilar'], '同期竞剧', 3)){
    	return;
    }
    if(!dataVerificationFormScore(data['sDissem'], '前期宣发', 3)){
    	return;
    }
	// 提交数据
	top.ajax("save_resource_by_long_score.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功。", 1, function(){
            	top.getLongList(1);
            	top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });

}

//to资源评分
function platformModfiyFormScore(resourceName){
	resourceName = resourceName || "";
	console.log(resourceName);
	ajax("edit_permit_form_longscore.do", {"resourceName":resourceName}, function(res){
		if (res.code == 1){
			var uri = "to_resource_by_long_score.do?resourceName="+resourceName;
			uri = encodeURI(encodeURI(uri));
			$.openWindow("资源评价", uri, 500, 470, true, null, true);
		} else {
			alert(res.msg);
		}
	});
}
//确认覆盖
function confirmCallback(repeat, msg) {
    if (repeat == 1){
        confirm(msg, function(){
            top.$("#isSure").val("sure");
            top.$("#longFileForm").submit();
        });
    }
}

//删除多个
function delLongBatch(){
	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(){
	   ajax("del_batch_resource_by_long.do", {"ids" : ids.substring(1,ids.length)}, function(res){
	       if (res.code == 1){
				alert("删除成功。", 1, function(){
					top.getLongList(1);
				});
	       } else {
	       	alert(res.msg);
	       }
	   });
	});
}