// 获取表格数据-电视硬广
function getList(page){
    page = page || 1;
    var pageSize = 10, data = {
        "name" : $("#name").val(),
        "type" : $("#type option:selected").val(),
        "showTime" : $("#showTime").val(),
        "saleStatus" : $("#saleStatus option:selected").val(),
        "pageNo" : page,
        "pageSize" : pageSize
    }, html = "";
        
    $("#page, #list, #count").empty();
    ajax("ajax_resource_list_by_advertise.do", data, function(res){
        if (res.code == 1 && res.list.length > 0){
            $.each(res.list, function(index, item){
                var length = item.theaterAttribute.toNull().length;
                html += '<tr>'+
					'<td><input type="checkbox" class="checkbox {click:itemCheck}" name="chk" value="'+item.id+'" /><label></label></td>'+
                    '<td>'+item.name.toNull()+'</td>'+
                    '<td>'+item.showTime.toNull()+'</td>'+
                    '<td>'+item.type.toString().decode("1","央视", "2","卫视","3","省级地面","4","其它","") +'</td>'+
                    ((length>25)?'<td title="'+item.theaterAttribute+'">'+item.theaterAttribute.substring(0,22)+'……</td>':'<td>'+item.theaterAttribute.toNull()+'</td>')+
                    '<td>'+(item.ratings ? item.ratings+'%' : '')+'</td>'+
                    '<td>'+(item.share ? item.share+'%' : '')+'</td>'+
                    '<td>'+item.versions+'</td>'+
                    '<td>'+item.price+'</td>'+
                    '<td>'+(item.discount ? item.discount+'%' : '')+'</td>'+
                    '<td>'+item.saleStatus.toString().decode("0","在售", "1","已售","")+'</td>'+
                    '<td>'+item.customer.toNull()+'</td>'+
                    '<td>'+item.remarks.toNull()+'</td>'+
                    '<td class="edit">'+
                        (update_flag == 1 ? '<a href="javascript:platformModfiy(\'edit\',\'电视硬广\','+item.id+');">编辑</a>&nbsp;&nbsp;' : '') +
                        (delete_flag == 1 ? '<a href="javascript:delAdvertise('+item.id+');">删除</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 delAdvertise(id){
	confirm("确定删除该条记录吗？", function(){
		ajax("del_resource_by_advertise.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_advertise.do", {"ids" : ids.substring(1,ids.length)}, function(res){
         if (res.code == 1){
 			alert("删除成功。", 1, function(){
 				window.location.href = "to_advertise_list.do";
 			});
         } else {
         	alert(res.msg);
         }
     });
	});
}

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

// “importExcel”的执行结果
function excelCallback(code, msg) {
	if (code == 1) {
	    // 导入成功
		alert("导入成功。", 1, function(){
	        window.location.href = "to_advertise_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_advertise.do?id="+id, 500, 470, true, null, true);
}

// 保存信息
function saveAdvertise(){
	// 得到数据
    var data = $('#infoForm').serializeJSON();
    data.id = $.getParam("id");
    // 数据是否完整
    if (data['name'].isEmpty()){
    	top.alert("请输入媒体名称", 0, function(){
            $("input[name='name']").focus();
        });
        return false;
    }
    if (data['showTime'].isEmpty()){
    	top.alert("请输入时间", 0, function(){
            $("input[name='showTime']").focus();
        });
        return false;
    }
    if (data['type'].isEmpty()){
    	top.alert("请选择频道类型");
    	return false;
    }
    if (data['theaterAttribute'].isEmpty()){
    	top.alert("请输入时段", 0, function(){
            $("input[name='theaterAttribute']").focus();
        });
        return false;
    }
    if (data['ratings'].isEmpty()){
    	top.alert("请输入收视率", 0, function(){
            $("input[name='ratings']").focus();
        });
        return false;
    }
    if (data['share'].isEmpty()){
    	top.alert("请输入收视份额", 0, function(){
            $("input[name='share']").focus();
        });
        return false;
    }
    if (data['versions'].isEmpty()){
    	top.alert("请输入版本", 0, function(){
            $("input[name='versions']").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;
    }
    data.saleStatus = $('input:radio[name="saleStatus"]:checked').val() || -1;
    if (data['saleStatus'] == -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_advertise.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.$("#fileForm").submit();
        });
    }
}