// 获取表格数据
function getShortList(page){
	page = page || 1;
	var pageSize = 10, data = {
		"name" : $("#shortName").val(),
		"saleStatus":$("#saleStatus").val(),
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
		
	$("#shortPage, #shortList,#shortCount").empty();
	ajax("ajax_resource_short_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 class="edit"><a href="to_resource_by_short_info.do?id='+item.id+'" target="_blank">'+item.name.toNull()+'</a></td>'+
					'<td>'+item.showTime.toNull()+'</td>'+
					'<td>'+item.registeredUser.toString().decode("0","-",item.registeredUser)+'</td>'+
					'<td>'+item.downloads.toString().decode("0","-",item.downloads)+'</td>'+
					'<td>'+item.dailyActivity.toString().decode("0","-",item.dailyActivity)+'</td>'+
					'<td>'+item.monthlyActivity.toString().decode("0","-",item.monthlyActivity)+'</td>'+
					'<td>'+item.mediaLabel.toString().decode("","-",item.mediaLabel)+'</td>'+
					'<td>'+item.advertisementForm.toString().decode("","-",item.advertisementForm)+'</td>'+
					'<td>'+item.systemPlatform.toString().decode("","-",item.systemPlatform)+'</td>'+
					'<td>'+item.displayMode.toString().decode("","-",item.displayMode)+'</td>'+
					'<td>'+item.cpx.toString().decode("","-",item.cpx)+'</td>'+
					'<td>'+item.price+'</td>'+
					'<td>'+item.saleStatus.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>' : '' ) +
					'</td>'+
				'</tr>';
			});
			$("#shortList").html(html);
			$.checkbox();
			$("#shortCount").text(res.count);
			$("#curShortPage").val(page);
			var newpage = new createPage(res.count, page, pageSize, 3);
			$("#shortPage").html(newpage.pageHtml);
		} else {
			$("#shortCount").text(0);
			$("#curShortPage").val(page);
			var newpage = new createPage(0, page, pageSize, 3);
			$("#shortPage").html(newpage.pageHtml);
			$("#shortList").info("暂无数据");
		}
	});
}
pageGo = function(page){
	resetCheckAll();
	getShortList(page);
}

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

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

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

// 保存短视频资源信息
function saveShort(){
	// 得到数据
    var data = $('#dataForm').serializeJSON();
    data.id = $.getParam("id");
    // 数据是否完整
    if (data['name'].isEmpty()){
    	top.alert("请输入资源名称", 0, function(){
            $("input[name='name']").focus();
        });
        return;
    }
    if (data['showTime'].isEmpty()){
    	top.alert("请输入时间", 0, function(){
            $("input[name='showTime']").focus();
        });
        return;
    }
    if (!isNaN(data['registeredUser'])){
    	if (parseInt(data['registeredUser']) < 0 || parseInt(data['registeredUser']) > 99999.999){
    		top.alert("数据范围错误！注册用户应为 0~99999.999");
    		return;
    	}
    }else if(isNaN(data['registeredUser'])){
    	top.alert("注册用户请输入数字");
	 	return;
    }
    if (!isNaN(data['downloads'])){
    	if (parseInt(data['downloads']) < 0 || parseInt(data['downloads']) > 99999.999){
    		top.alert("数据范围错误！下载量应为 0~99999.999");
    		return;
    	}
    }else if(isNaN(data['downloads'])){
    	top.alert("下载量请输入数字");
	 	return;
    }
    if (!isNaN(data['dailyActivity'])){
    	if (parseInt(data['dailyActivity']) < 0 || parseInt(data['dailyActivity']) > 99999.999){
    		top.alert("数据范围错误！日活应为 0~99999.999");
    		return;
    	}
    }else if(isNaN(data['dailyActivity'])){
    	top.alert("日活请输入数字");
	 	return;
    }
    if (!isNaN(data['monthlyActivity'])){
    	if (parseInt(data['monthlyActivity']) < 0 || parseInt(data['monthlyActivity']) > 99999.999){
    		top.alert("数据范围错误！月活应为 0~99999.999");
    		return;
    	}
    }else if(isNaN(data['monthlyActivity'])){
    	top.alert("月活请输入数字");
	 	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("saleStatus1").checked&&
  	       !document.getElementById("saleStatus2").checked){
 	 	top.alert("请选择售卖状态")
 	 	return;
    }
    // 提交数据
    top.ajax("save_resource_short_info.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功", 1, function(){
            	if(res.curPage==1){
            		top.getShortList(1);
            	}else {
            		top.getShortList($.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_short_info.do?id="+id+"&curPage="+$("#curShortPage").val(), 500, 470, true, null, true);
}

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

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