<!--{template 'head'}-->
<!--{template 'collect/head'}-->
<script type="text/javascript">
var      data = new Array();
var     index = 0;
var  htmlFlag = 0;
var    thread = 1;
var     pages = 1;
var   curPage = 0;
var    tdFlag = 0;
var channelid = '{$channelid}';
var     rules = '{$rules}';
var threadGet = 1;
var  tryTimes = 0;

function startMe()
{
	$('submit').disabled = true;
	$('submit').value    = " 采集中.... ";
	curPage = 1;
	htmlGetList();
}

//采集链接列表信息
function htmlGetList()
{
	$('message').innerHTML    = "数据源列表采集....";
	var link = "system.php?sys=collect&work=link_work&channelid="+channelid+"&rules="+rules+"&pageno="+curPage+"&threadGet=1";
	AJAX.request( link,
	{
		method		: "get",
		onComplete	: function(resp)
		{
			var json = evaljson(resp);
			if( json.status == 1 ||  json.status == 4)
			{
				index++;
				curPage++;
				if(typeof json.data == 'object' && json.data != null){
					for(var i=0; i<json.data.length; i++){
						showInfoList( json.data[i][0], json.data[i][1]);
					}
				}
				if( json.over == 1){
					getUrl();
					return true;
				}else{
					if(index <= json.total){
						var w  = (index/json.total) * 100;
						if(w <= 100) $('loadpage').style.width = (index/json.total) * 100 + '%';
						$('loadpage').innerHTML = '<font color=green>'+(index)+'</font>/<font color=green>'+json.total+'</font>';
					}
					htmlGetList();
				}
			}else{
				if( json.over == 1){
					getUrl();
				}
				return false;
			}
			return true;
		}
	});
}

var getContentIndex = 0;
function getContent(){
	if(data.length == 0){
		$('submit').disabled = false;
		$('submit').value    = " 开始采集 ";
		return;
	}
	var id = data.shift();
	var link = "system.php?sys=collect&work=content&rules="+rules+"&channelid="+channelid+"&id="+id;
	AJAX.request( link,
	{
		method		: "get",
		onComplete	: function(resp){
			getContentIndex++;
			if(!resp){
				getContent();
				return;
			}
			var json = evaljson(resp);
			if(json != null) showInfo(json);
			var w  = (getContentIndex/(data.length+getContentIndex)) * 100;
			if(w <= 100) $('loadhtml').style.width = Math.min(100, ((getContentIndex+1)/(data.length+getContentIndex)) * 100) + '%';
			$('loadhtml').innerHTML = '<font color=green>'+getContentIndex+'</font>/<font color=green>'+(data.length+getContentIndex)+'</font>';
			getContent();
			return true;
		}
	});
}
function getUrl()
{
	index = 0;
	$('message').innerHTML    = "数据源内容采集并且入库....";
	$('submit').disabled = true;
	$('submit').value    = "采集中....";
	var link = "system.php?sys=collect&work=geturl&rules="+rules+"&channelid="+channelid;
	AJAX.request( link,
	{
		method		: "get",
		onComplete	: function(resp)
		{
			var json = evaljson(resp);
			if( json.status == 200){
				data = json.data;
				clearInfo();
			}else{
				alert("没有可加载的链接数据");
				$('submit').disabled = false;
				$('submit').value    = " 开始采集 ";
			}
			return true;
		}
	});
	return true;
}

function htmlGetContent()
{
	clearInfo();
	return true;
}

function showInfo( json)
{
	var obj = $('myTable');
	var alt = '';
	if(json == null ||  typeof json.status == 'undefined') return;
	switch( json.status)
	{
		case 1:
			alt = '<font color=red>数据源错误</font>';
			url = '###';
		break;

		case 2:
			alt = '<font color=red>没有采集到文章内容</font>';
			url = '###';
		break;

		case 3:
			alt = '<font color=red>数据库存在相同标题的数据</font>';
			url = '###';
		break;

		case 4:
			alt = '<font color=green>该数据采集成功</font>';
			url = '{$_SET[webpath]}admin/module.php?mod=article&channelid={$channelid}&action=edit&id='+json.id;
		break;
	}
	var _tr = obj.insertRow(0);
	var _td = _tr.insertCell(0);
		_td.className = 'tablerowl';
		_td.width = '50%';
		_td.style.textAlign = 'right';
		_td.style.paddingRight = '25px';
		_td.innerHTML = "<a href='"+url+"' "+(url == '###' ? '' : 'target=_blank')+">"+json.title+"</a>";
	var _td = _tr.insertCell(1);
		_td.className = 'tablerow';
		_td.innerHTML = alt;
		_td.style.textAlign = 'left';
		_td.width = '50%';
		_td.style.paddingLeft = '25px';
	return true;
}
function showInfoList( title, url)
{
	var obj = $('myTable');
	var _tr = obj.insertRow(0);
	var _td = _tr.insertCell(0);
		_td.className = 'tablerowl';
		_td.innerHTML = "<a href='"+url+"'>"+title+"</a>";
	  _td = _tr.insertCell(1);
		_td.className = 'tablerow';
		_td.innerHTML =  url;
	return true;
}
var ad ;
function clearInfo()
{
	ad = setInterval("clearTr()",20);
	return true;
}
function clearTr()
{
	var obj = $('myTable');
	if( obj.rows.length > 0)
	{
		obj.deleteRow(0);
	}
	else
	{
		clearInterval(ad);
		getContentIndex = 0;
		getContent();
	}
	return true;
}
</script>
	<style type="text/css">
	p.loading{height:30px;background:red;width:0px;text-align:center;line-height:30px;}
	</style>
	<table cellpadding="1" cellspacing="1" border="0" align=center width=100% class="tableborder">
	<tr>
		<th colspan=2>采集信息<font color=red><span id='message'></span></font></th>
	</tr>
	</table>
	<div style="width:100%;height:60px;background:#F1F3F5 none repeat scroll 0 0">
		<p id='loadpage' class="loading"></p>
		<p id='loadhtml' class="loading" style="margin-top:1px;"></p>
	</div>
	<table cellpadding="0" cellspacing="0" border="0" align=center width=100% >
	<tr>
		<td align=center class="tablerowl">
			<input class="f-button"  type=button onclick="startMe();" id='submit' value=" 开始采集 ">
		</td>
	</tr>
	</table>
	<table cellpadding="0" cellspacing="1" border="0" align=center width=100% class="tableborder border" id="myTable">

	</table>
<!--{template 'foot'}-->