// 获取表格数据
function getList(page){
	page = page || 1;
	var resourceType = $("#resourceType option:selected").val();
	var status = $("#status option:selected").val();
	var pageSize = 10, data = {
		"resourceName" : $("#resourceName").val(),
		"resourceType" : ( resourceType == "" ? "" : resourceType),
		"status" : ( status == "" ? "" : status),
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
	$("#page, #list,#count").empty();
	ajax("ajax_resource_recom_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.resourceName+'</td>'+
					'<td>'+
					(item.resourceType == 1 ? "电视节目" :item.resourceType == 2 ? "电视硬广" :
					 item.resourceType == 3 ? "网络长视频" : item.resourceType == 4 ? "网络短视频" : 
					 item.resourceType == 5 ? "APP" :"未知类型") +
					'</td>'+
					'<td>'+item.platform+'</td>'+
					'<td class="nop">'+(item.imgUrl ? '<img id="'+item.imgUrl+'" src="'+item.imgUrl.toNull()+'" onclick="showimg(\''+item.imgUrl+'\')" width="98" height="58" valign="middle" onerror="this.src=\'static/img/noimg.jpg\';" />':'&nbsp;')+'</td>'+
					'<td>'+(item.recomType == 1 ? "头部轮播区" : item.recomType == 2 ? "非头部" : "未知")+'</td>'+
					'<td>'+item.rank+'</td>'+
					'<td>'+(item.status == 2 ? "已显示" : item.status == 0 ? "已隐藏" : "未知")+'</td>'+
					'<td class="edit">'+
						(update_status_flag == 1 ? 
							(item.status==2 ? '<a href="javascript:setStatus('+item.id+');">设为隐藏</a><br />' :
							 item.status==0 ? '<a href="javascript:setStatus('+item.id+');">设为显示</a><br />' : '' ):'<br />') + 
    					(update_flag == 1 ? '<a href="javascript:platformModfiy(\'edit\',\'资源推荐\','+item.id+');">编辑</a>&nbsp;&nbsp;' : '<br />') +
    					(delete_flag == 1 ? '<a href="javascript:delRecord('+item.id+');">删除</a>' : '<br />' ) +
					'</td>'+
				'</tr>';
			});
			$("#list").html(html);
			$.checkbox();
			$("#count").text(res.count);
			$("#curPage").val(page);
			var newpage = new createPage(res.count, page, pageSize, 3);
			$("#page").html(newpage.pageHtml);
		} else {
			$("#list").info("暂无数据。");
		}
	});
}
pageGo = getList
// 删除单个
function delRecord(id){
	confirm("确定删除该条记录吗？", function(){
        ajax("delete_resource_recom.do", {"id" : id}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				window.location.href = "resource_recom_list.do";
    			});
            } 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("batch_del_for_resource_recom.do", {"ids" : ids.substring(1,ids.length)}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				window.location.href = "resource_recom_list.do";
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

// 保存资源信息
function save(){
	// 得到数据
    var data = $('#dataForm').serializeJSON();
    data.id = $.getParam("id");
    data.resourceType = $("select[name='resourceType']>option:selected").val();
    data.imgUrl = $("#imgUrl")[0].src;
    console.log(data);
    // 数据是否完整
    if (data['resourceType'].isEmpty()){
    	top.alert("请选择资源类型", 0, function(){
            $("input[name='resourceType']").focus();
        });
        return false;
    }
    if (data['imgUrl'].isEmpty() || data['imgUrl'].indexOf("ylcm_sys") > 1){
    	top.alert("请上传推荐图");
        return false;
    }
    if (data['resourceName'].isEmpty()){
    	top.alert("请填写资源名称", 0, function(){
            $("input[name='resourceName']").focus();
        });
        return false;
    }
    if (data['rank'].isEmpty()){
    	top.alert("请填写推荐顺序", 0, function(){
            $("input[name='rank']").focus();
        });
        return false;
    }
    if (data['platform'].isEmpty()){
    	top.alert("请填写播出频道", 0, function(){
            $("input[name='platform']").focus();
        });
        return false;
    }
    if (data['recomType'].isEmpty()){
    	top.alert("请选择投放类型");
        return false;
    }
    // 提交数据
    top.ajax("save_resource_recom_info.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功。", 1, function(){
            	top.getList(1);
            	top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });
}

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

function setStatus(id){
	var page = $("#curPage").val();
	confirm("确定更改状态吗？", function(){
        $.post("update_resource_recom_status.do", {"id" : id}, function(res){
            if (res.code == 1){
    			alert("更改成功。", 1, function(){
    				top.getList(page);
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

function showimg(imgurl){
	var url = document.getElementById(imgurl).src;
	if(url!='' && url!='http://localhost:8080/ylcm_sys/static/img/noimg.jpg'){
		var html = '<img src='+url+' width="480px" height="380px" />';
		$.openWindow(" ",html,500,380,false);
	}
}
