<?php
/*
	[Phpup.Net!] (C)2009-2011 Phpup.net.
	This is NOT a freeware, use is subject to license terms

	$Id: admin.class.php 2010-08-24 10:42 $
*/

if(!defined('IN_PHPUP')) {
	exit('Access Denied');
}
?>
<?php include ROOT_PATH.'/views/admin/header.php';?>
<script language="javascript" src="<?php echo STATIC_ROOT;?>/views/js/cookie.js"></script>
<body>
<STYLE TYPE="text/css">
	select{width:200px;height:400px;}
	.movebutton{width:80px;}
</STYLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
	var tlen=1;
	var success=failed=0;
	function addallsite()
	{
		$('#sel2').html($('#sel3').html());
		$('#sel3').html('');
		
	}
	function checksite(id)
	{
		var ok=1;
		$('#sel2 option').each(function(i){if($(this).val()==id){ok=0;return;}});
		return ok;
	}
	function addsite()
	{
		var limit=10;
		
		if($('#sel1').val())
		{
			$('#sel1').find('option:selected').each(function(i){
				if(i<limit && checksite($(this).val()))
				{
					$(this).remove();
					attachurl($(this).val());
				}
			});
		}
		
	}
	function movesite()
	{
		if($('#sel2').val())
		{
		$('#sel2').find('option:selected').remove();
		}
		
	}
	function attachurl(id)
	{
		$.ajax(
				{
				type: "GET",
				url:site_root+"/index.php?con="+adminpath+"&act=attachurl&siteid="+id,
				timeout:30000,
				success:function(data){
					eval('var dataobj='+data);
					if(dataobj.status=='success')
					{
						
						for(i in dataobj.data)
						{
							$('#sel2').append('<option title="'+dataobj.data[i].repurl+'" value="'+dataobj.data[i].siteid+'">'+dataobj.data[i].sitename+':'+dataobj.data[i].cityname+'</option>');
						}
					}
				}
				}
				);
		
	}
	function moveallsite()
	{
		$('#sel2').html('');
		
	}
	
	function selectall()
	{
		if($('#sel2').html()!='')
		{
			$('#startbutton').attr('disabled','true');
			$('#addall').attr('disabled','true');
			$('#temp').show();
			$('#sel3').show();
			tlen=$('#sel2 option').length;
			if(tlen>60)
			{
				mt=Math.floor(tlen/360)+'小时';
			}
			else
			{
				mt=tlen*10+'秒';
			}
			$('#errorsite').html('采集失败网站(双击可修改api),预计时长：'+mt);
			
			postval();
		}
		else
		{
			alert('未选择网站');
		}
	}
	function savexml(id,regurl)
	{
		url=site_root+"/models/savexml.php?id="+id+"&regurl="+encodeURIComponent(regurl);
		$.get(url,{},
		function(data){
			if(data=='success')
			{
				alert('文件保存成功,请重新采集');
			}
			else
			{
				alert('文件保存失败');
			}
		});
	}
	function showapi(type,obj)
	{
		if(type==1)
		{
			window.open($("#localapi").val());
		}
		else
		{
			savexml(obj,$("#localapi").val());
		}
	}
	function showsite(obj)
	{
		var myurl=$(obj).find('option:selected').attr('title').replace('limengqi2','?');
		myurl=myurl.replace('limengqiurl1','&');
		$('#dialog').html('<textarea id="localapi" style="width:280px;height:80px;">'+myurl+'</textarea><h4>在保存数据前请查看相关api数据是否为空</h4><INPUT TYPE="button" VALUE="查看" ONCLICK="showapi(1);"><INPUT TYPE="button" VALUE="数据保存到本地" ONCLICK="showapi(2,\''+$(obj).find('option:selected').val()+'\')">');
		$('#blurdiv').show();
		$('#blurdiv').height($(document).height());
		$('#dialog').dialog('open');
		$('#dialog').dialog({
			title:'网站信息',
			width: 400,
			height:200,
			buttons: {},
			close:function(){$('#blurdiv').hide();$('#dialog').html('');}
		});
	}
	function postval()
	{
		var len=$('#sel2 option').length;
		if(len>0)
		{
			var current=$('#sel2 option').eq(0).val();
			var repurl=$('#sel2 option').eq(0).attr('title');
			if(current)
			{
				$.ajax(
				{
				type: "GET",
				url:site_root+"/index.php?con="+adminpath+"&act=attach&siteid="+current+"&repurl="+repurl,
				timeout:30000,
				success:function(data){
					progress(tlen,data);
					setTimeout(function(){postval();},300);
					
				},
				error:function()
				{
					progress(tlen,'');
					setTimeout(function(){postval();},300);
				}
				});
				
			}
			
		}
	}
	function progress(tlen,data)
	{
		var current=$('#sel2 option').eq(0).val();
		var len=$('#sel2 option').length;
		var d=parseInt(((tlen-len+1)*100)/tlen);
		var px=600/tlen;
		if(d>10)
		{
			$('#tempid').html(d+'%');
		}
		$('#tempid').css('width',px*(tlen-len+1)+'px');
		if(data)
		{
			data=data.substr(data.indexOf('{status'));
			
			eval('var objdata='+data);
			if(objdata.lasttime && objdata.starttime)
			{
				lasttime=objdata.lasttime.split(' ');
				starttime=objdata.starttime.split(' ');
				l=lasttime[1]*1+lasttime[0]*1;
				s=starttime[1]*1+starttime[0]*1;
			}
			if(objdata.status=='success' && objdata.siteid>0)
			{
				//$.cookie('success_cookie', $.cookie('success_cookie')+','+$('#currentsite').val()); //设置cookie的值
				success++;
			}
			else
			{
				//$.cookie('failed_cookie', $.cookie('failed_cookie')+','+$('#currentsite').val()); //设置cookie的值
				$('#sel3').append('<option value="'+current+'" title="'+$('#sel2 option').eq(0).attr('title')+'" id="site-'+current+'">'+$('#sel2 option').eq(0).text()+'</option>');
				failed++;
			}
			if($('#sel2 option').eq(1).text())
			{
				var myl=l-s>=1?Math.round(l-s)+'S':Math.round((l-s)*1000)+'MS';
				$('#loading').html($('#sel2 option').eq(0).text()+objdata.result+'用时('+myl+'),正在采集'+$('#sel2 option').eq(1).text());
			}
			else
			{
				$('#loading').html('<a href="index.php?con='+adminpath+'&act=listtempsite">采集完成，共采集'+tlen+'个网站，点击进入入库操作</a>');
				$('#startbutton').removeAttr('disabled');
				$('#addone').attr('disabled','true');
				$('#addall').removeAttr('disabled');
			}
		}
		else
		{
			if($('#sel2 option').eq(1).text())
			{
				$('#loading').html($('#sel2 option').eq(0).text()+'采集超时,正在采集'+$('#sel2 option').eq(1).text());
			}
			else
			{
				$('#loading').html('<a href="index.php?con='+adminpath+'&act=listtempsite">采集完成，共采集'+tlen+'个网站，点击进入入库操作</a>');
				$('#startbutton').removeAttr('disabled');
				$('#addone').attr('disabled','true');
				$('#addall').removeAttr('disabled');
			}
			$('#sel3').append('<option value="'+current+'" style="background:#ff0000;color:#fff;"  title="'+$('#sel2 option').eq(0).attr('title')+'" id="site-'+current+'">'+$('#sel2 option').eq(0).text()+'</option>');
			failed++;
		}
		
		$('#sel2 option').eq(0).remove();
		$('#currentsite').val(current);
		$('#tempi').val('0');
		$('#sf').html('成功：'+success+'<br/>失败：'+failed);
	}
				
//-->
</SCRIPT>
<INPUT TYPE="hidden" id="currentsite" value="0">
<div class="subnav">
    <div class="content-menu ib-a blue line-x">
        <A HREF="index.php?con=<?php echo $GLOBALS['setting']['adminpath'];?>&act=site"class="on"><em>采集信息</em></A>
	</div>
</div>
<div class="explain-col">注意：由于在采集过程中可能会碰到僵尸网站，最长会延迟30S的时间，请耐心等待。</div>
<br/>
<div class="table-list">
<DIV ID="temp" style="display:none;width:600px;text-align:center;margin:auto;padding:10px;">
	<div style="background:#ff0000;height:20px;width:1px;text-align:center;color:#fff;" id="tempid"></div>
	<div id="loading">开始采集...</div>
</DIV>
<table width="500px" border="0" align="center"  cellpadding="3" cellspacing="1">
<thead><tr><th>网站列表&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="errorsite"></span></th><th id="sf"></th><th>待采集网站</th></tr></thead>
<tbody>
<tr><td>
<TABLE>
<TR>
<TD><SELECT NAME="siteid" multiple="multiple" id="sel1">
<?php if($sites){foreach($sites as $k=>$v){?>
<OPTION VALUE="<?php echo $v['id'];?>"  id="site-<?php echo $v['id'];?>"><?php echo $v['pinyin'];?> <?php echo $v['sitename'];?>
<?php }}?>
	
</SELECT></TD>
	<TD><SELECT NAME="siteid" multiple="multiple"  id="sel3" style="display:none;" ondblclick="showsite(this)">
</SELECT></TD>
</TR>
</TABLE>


</td>
<td style="widht:100px;text-align:center;">
<INPUT TYPE="button" class="movebutton" VALUE=">"  id="addone" ONCLICK="addsite();"><br/><INPUT TYPE="button" id="addall" VALUE=">>" class="movebutton"  ONCLICK="addallsite();" disabled="ture">
<br/><INPUT TYPE="button" VALUE="<<" ONCLICK="moveallsite();" class="movebutton" ><br/><INPUT TYPE="button" class="movebutton"  VALUE="<" ONCLICK="movesite();"></td>
<td>
<SELECT NAME="siteid[]" multiple="multiple"  id="sel2"></SELECT>
</td></tr>
<tr><td></td><td><INPUT TYPE="button" onclick="selectall();" value="开始采集" class="button" id="startbutton"></td><td></td></tr>
</tbody>
</table>
</div>
<div id="dialog" style="display:none;"><IMG SRC="<?php echo STATIC_ROOT;?>/views/js/i/032.gif" WIDTH="32" HEIGHT="32" BORDER="0" ALT=""></div>
<div style="z-index: 1000; background-color: rgb(0, 0, 0); visibility: visible; display: none; position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; width:100%;filter:alpha(opacity=50); opacity: 0.5; -moz-opacity:0.5;" id="blurdiv"></div>

    </body>
</html>