// 获取表格数据-电视硬广
function getList(page){
    page = page || 1;
    var pageSize = 10, data = {
        "name" : $("#name").val(),
        "channel" : $("#channel").val(),
        "showTime" : $("#showTime").val(),
        "showInterval" : $("#showInterval").val(),
        "customer" : $("#customer").val(),
        "form" : $("#form").val(),
        "sellStatus" : $("#sellStatus option:selected").val(),
        "pageNo" : page,
        "pageSize" : pageSize
    }, html = "";
        
    $("#page, #list, #count").empty();
    ajax("ajax_resource_list_by_play.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.channel.toNull()+'</td>'+
                    '<td class="edit"><a href="to_resource_by_play_info.do?id='+item.id+'" target="_blank">'+item.name.toNull()+'</a></td>'+
                    '<td>'+item.type.toString().decode("1","电视剧", "2","综艺","3","栏目","4","其他","") +'</td>'+
                    '<td>'+item.form.toNull()+'</td>'+
                    '<td>'+item.showTime.toNull()+'</td>'+
                    '<td>'+(item.category ? item.category.toNull() : "")+'</td>'+
                    '<td>'+item.sellMode.toNull()+'</td>'+
                    '<td>'+(item.price ? item.price : "")+'</td>'+
                    '<td>'+(item.discount ? item.discount+'%' : '')+'</td>'+
                    '<td>'+item.showInterval+'</td>'+
                    '<td>'+item.sellStatus.toString().decode("0","在售", "1","已售","")+'</td>'+
                    '<td>'+(item.customer ? item.customer : "")+'</td>'+
                    '<td>'+(item.remarks ? 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><br / >' : '' ) +
                        (score_flag == 1 && 1 == item.type ? '<a href="javascript:platformModfiyFormScore(\''+item.name+'\');">'+item.scoreId.toString().decode("0","评价", "已评")+'</a>' : '') +
                    '</td>'+
                '</tr>';
            });
            $("#list").html(html);
            $("#count").text(res.count);
            var newpage = new createPage(res.count, page, pageSize, 3);
            $("#page").html(newpage.pageHtml);
            $(".table").hoverTable(true);
            $.checkbox();
        } else {
            $("#list").info("暂无数据。");
        }
    });
}
pageGo = function(page){
    resetCheckAll();
    getList(page);
}

//广告形式
function retForm(form){
	return form == undefined ? "" : form.replace(/1/,"节目冠名").replace(/2/,"特约播映").replace(/3/,"互动支持").replace(/4/,"合作伙伴").replace(/5/,"指定产品").replace(/6/,"指定用车").replace(/7/,"其他").replace(/\,/g,"、");
}

// 删除单个
function delRecord(id){
	confirm("确定删除该条记录吗？", function(){
		ajax("del_resource_by_play.do", {"id" : id}, function(res){
			if (res.code == 1){
				alert("删除成功。", 1, function(){
					window.location.reload();
				});
			} 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(){
	   ajax("del_batch_resource_by_play.do", {"ids" : ids.substring(1,ids.length)}, function(res){
	       if (res.code == 1){
				alert("删除成功。", 1, function(){
					window.location.href = "to_play_list.do";
				});
	       } else {
	       	alert(res.msg);
	       }
	   });
	});
}

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

// “importExcel”的执行结果
function excelCallback(code, msg) {
	if (code == 1) {
	    // 导入成功
		alert("导入成功。", 1, function(){
	        window.location.href = "to_play_list.do";
	    });
	} else {
	    // 导入失败
	    alert(msg, 0, function(){
	        location.reload();
	    });
	}
}

// to详情弹窗
function platformModfiy(act, title, id){
	title = title || "";
	id = id || "";
	$.openWindow(act.decode("add","新增","edit","修改","")+title, "to_resource_by_play.do?id="+id, 500, 470, true, null, true);
}

// to资源评分
function platformModfiyFormScore(resourceName){
	console.log("..."+resourceName);
	resourceName = resourceName || "";
	ajax("edit_permit_form_playscore.do", {"resourceName":resourceName}, function(res){
		if (res.code == 1){
			var uri = "to_resource_by_play_score.do?resourceName="+resourceName;
			uri = encodeURI(encodeURI(uri));
			$.openWindow("资源评价",uri , 500, 470, true, null, true);
		} else {
			alert(res.msg);
		}
	});
}

// 保存基本信息
function saveInfo(){
	// 得到数据
    var data = $('#infoForm').serializeJSON();
    data.id = $.getParam("id");
    // 数据是否完整
    if (data['channel'].isEmpty()){
    	top.alert("请输入播出频道", 0, function(){
            $("input[name='channel']").focus();
        });
        return false;
    }
    if (data['name'].isEmpty()){
    	top.alert("请输入资源名称", 0, function(){
            $("input[name='name']").focus();
        });
        return false;
    }
    if (data['type'].isEmpty()){
    	top.alert("请选择频道类型");
    	return false;
    }
    if (data['form'].isEmpty()){
        top.alert("请选择广告形式");
        return false;
    }
    if (data['showTime'].isEmpty()){
    	top.alert("请输入时间", 0, function(){
            $("input[name='showTime']").focus();
        });
        return false;
    }
    if (data['category'].isEmpty()){
    	top.alert("请输入节目体裁", 0, function(){
            $("input[name='category']").focus();
        });
        return false;
    }
    if (data['sellMode'].isEmpty()){
    	top.alert("请输入售卖状态", 0, function(){
            $("input[name='sellMode']").focus();
        });
        return false;
    }
    if (data['price'].isEmpty()){
    	top.alert("请输入刊例价格", 0, function(){
            $("input[name='price']").focus();
        });
        return false;
    }
    if (data['discount'].isEmpty()){
    	top.alert("请输入折扣", 0, function(){
            $("input[name='discount']").focus();
        });
        return false;
    }
    if (data['showInterval'].isEmpty()){
    	top.alert("请输入客户类别", 0, function(){
            $("input[name='showInterval']").focus();
        });
        return false;
    }
    data.sellStatus = $('input:radio[name="sellStatus"]:checked').val() || -1;
    if (data['sellStatus'] == -1){
    	top.alert("请选择售卖状态");
    	return false;
    }
    if (!data['customer'].isEmpty() && data['customer'].length > 20){
    	top.alert("“已售客户”长度不可超过20");
    	return false;
    }
    if (!data['remarks'].isEmpty() && data['remarks'].length > 20){
    	top.alert("“备注”长度不可超过20");
    	return false;
    }
    // 提交数据
    top.ajax("save_resource_by_play.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功。", 1, function(){
            	top.getList(1);
            	top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });
}

// 数据验证（验证通过返回 true）
function dataVerificationFormScore(value2, text2, maxScore2){
	if (value2 == null || value2 === ""){
		top.alert("请给"+ text2 +"打分。");
		return false;
	}
	if (isNaN(value2)){
		top.alert("数据类型错误！"+ text2 +"的评分只能是数字，且为 0~"+ maxScore2);
		return false;
	}
	if (0 > parseInt(value2) || parseInt(value2) > maxScore2){
		top.alert("数据范围错误！"+ text2 +"的评分应为 0~"+ maxScore2);
		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 false;
    }
    if (!dataVerificationFormScore(data['mTheme'], "题材", 5)){return false;}
    if (!dataVerificationFormScore(data['mEpisode'], "集数", 3)){return false;}
    if (!dataVerificationFormScore(data['mSchedule'], "档期", 4)){return false;}
    if (!dataVerificationFormScore(data['mPlatform'], "平台", 15)){return false;}
    if (!dataVerificationFormScore(data['mFlow'], "预估流量", 10)){return false;}
    if (!dataVerificationFormScore(data['pActor'], "演员", 15)){return false;}
    if (!dataVerificationFormScore(data['pDirector'], "导演", 8)){return false;}
    if (!dataVerificationFormScore(data['pScriptwritter'], "编剧", 7)){return false;}
    if (!dataVerificationFormScore(data['pProduced'], "出品公司", 5)){return false;}
    if (!dataVerificationFormScore(data['pIssue'], "发行公司", 10)){return false;}
    if (!dataVerificationFormScore(data['sSimilar'], "同期竞剧", 3)){return false;}
    if (!dataVerificationFormScore(data['sDissem'], "前期宣发", 3)){return false;}
    // 提交数据
    top.ajax("save_resource_by_play_score.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功。", 1, function(){
            	top.getList(1);
            	top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });
}

//确认覆盖
function confirmCallback(repeat, msg) {
    if (repeat == 1){
        confirm(msg, function(){
            top.$("#isSure").val("sure");
            top.$("#excelfileForm").submit();
        });
    }
}
